Skip to content

promotion: don't promote extern statics#157641

Open
sjwang05 wants to merge 1 commit into
rust-lang:mainfrom
sjwang05:fix-143174-extern-static-promotion
Open

promotion: don't promote extern statics#157641
sjwang05 wants to merge 1 commit into
rust-lang:mainfrom
sjwang05:fix-143174-extern-static-promotion

Conversation

@sjwang05

@sjwang05 sjwang05 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Even though we're allowed to reference extern statics, it's impossible to know the value of an extern static during CTFE when it's used by-value, so we shouldn't try promoting them.

fixes #143174

@rustbot

rustbot commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 9, 2026
@rustbot

rustbot commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 20 candidates

@rust-log-analyzer

This comment has been minimized.

@sjwang05 sjwang05 force-pushed the fix-143174-extern-static-promotion branch from 6257168 to 80ac224 Compare June 9, 2026 07:39
@rust-log-analyzer

This comment has been minimized.

@sjwang05 sjwang05 force-pushed the fix-143174-extern-static-promotion branch from 80ac224 to 8877f4c Compare June 9, 2026 08:01
Comment on lines +363 to +371
if let Some((place_base, ProjectionElem::Deref)) =
place.as_ref().last_projection()
&& let Some(local) = place_base.as_local()
&& let TempState::Defined { location: loc, .. } = self.temps[local]
&& let Left(stmt) = self.body.stmt_at(loc)
&& let Some((_, Rvalue::Use(Operand::Constant(c), _))) =
stmt.kind.as_assign()
&& let Some(did) = c.check_static_ptr(self.tcx)
&& self.tcx.is_foreign_item(did)

@oli-obk oli-obk Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having to take this apart during promotion is kind of annoying. I wonder if we have enough information during MIR building to just reject creating reads from foreign statics in const contexts entirely.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[TIMING:end] tool::Tidy { compiler: Compiler { stage: 0, host: x86_64-unknown-linux-gnu, forced_compiler: false }, target: x86_64-unknown-linux-gnu } -- 0.000
fmt check
Diff in /checkout/compiler/rustc_mir_transform/src/promote_consts.rs:360:
         match operand {
             Operand::Copy(place) | Operand::Move(place) => {
                 self.validate_place(place.as_ref())?;
-                if let Some((place_base, ProjectionElem::Deref)) =
-                    place.as_ref().last_projection()
+                if let Some((place_base, ProjectionElem::Deref)) = place.as_ref().last_projection()
                     && let Some(local) = place_base.as_local()
                     && let TempState::Defined { location: loc, .. } = self.temps[local]
                     && let Left(stmt) = self.body.stmt_at(loc)
Diff in /checkout/compiler/rustc_mir_transform/src/promote_consts.rs:368:
-                    && let Some((_, Rvalue::Use(Operand::Constant(c), _))) =
-                        stmt.kind.as_assign()
+                    && let Some((_, Rvalue::Use(Operand::Constant(c), _))) = stmt.kind.as_assign()
                     && let Some(did) = c.check_static_ptr(self.tcx)
                     && self.tcx.is_foreign_item(did)
                 {
Diff in /checkout/compiler/rustc_mir_transform/src/promote_consts.rs:404:
                     if is_thread_local {
                         return Err(Unpromotable);
                     }

@oli-obk oli-obk added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE when trying to create a static reference to a structure enclosing an external static value

4 participants