-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Don't try to evaluate const blocks during constant promotion #150557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
dianne:no-const-block-eval-in-promotion
Jan 27, 2026
+142
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
tests/ui/inline-const/dont-eval-const-block-during-promotion.fail.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/dont-eval-const-block-during-promotion.rs:48:14 | ||
| | | ||
| LL | x = &([0][const { 0 }] & 0); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| ... | ||
| LL | (x, y, z); | ||
| | - borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
|
|
||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/dont-eval-const-block-during-promotion.rs:50:14 | ||
| | | ||
| LL | y = &(1 / const { 1 }); | ||
| | ^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| ... | ||
| LL | (x, y, z); | ||
| | - borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
|
|
||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/dont-eval-const-block-during-promotion.rs:52:14 | ||
| | | ||
| LL | z = &(const { 1 } / -1); | ||
| | ^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| LL | | ||
| LL | (x, y, z); | ||
| | - borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0716`. |
65 changes: 65 additions & 0 deletions
65
tests/ui/inline-const/dont-eval-const-block-during-promotion.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| //! Test for #150464: as of #138499, trying to evaluate const blocks during constant promotion will | ||
| //! result in a query cycle, so we shouldn't do it. Evaluation can happen when trying to promote | ||
| //! integer division and array indexing, where it's necessary for the operation to succeed to be | ||
| //! able to use it in a promoted constant. | ||
| //@ revisions: pass fail | ||
| //@[pass] check-pass | ||
|
|
||
| use std::mem::offset_of; | ||
|
|
||
| struct Thing(i32); | ||
|
|
||
| fn main() { | ||
| // For a temporary involving array indexing to be promoted, we evaluate the index to make sure | ||
| // it's in-bounds. As of #150557 we treat inline constants as maybe-out-of-bounds to avoid the | ||
| // query cycle from evaluating them. That allows this to compile: | ||
| let x = &([0][const { 0 }] & 0); | ||
| // Likewise, integer divisors must be nonzero. Avoiding the query cycle allows this to compile: | ||
| let y = &(1 / const { 1 }); | ||
| // Likewise, signed integer dividends can't be the integer minimum when the divisor is -1. | ||
| let z = &(const { 1 } / -1); | ||
| // These temporaries are all lifetime-extended, so they don't need to be promoted for references | ||
| // to them to be live later in the block. Generally, code with const blocks in these positions | ||
| // should compile as long as being promoted isn't necessary for borrow-checking to succeed. | ||
| (x, y, z); | ||
|
|
||
| // A reduced example from real code (#150464): this can't be promoted since the array is a local | ||
| // variable, but it still resulted in a query cycle because the index was evaluated for the | ||
| // bounds-check before checking that. By not evaluating the const block, we avoid the cycle. | ||
| // Since this doesn't rely on promotion, it should borrow-check successfully. | ||
| let temp = [0u8]; | ||
| let _ = &(temp[const { 0usize }] & 0u8); | ||
| // #150464 was reported because `offset_of!` started desugaring to a const block in #148151. | ||
| let _ = &(temp[offset_of!(Thing, 0)] & 0u8); | ||
|
|
||
| // Similarly, at the time #150464 was reported, the index here was evaluated before checking | ||
| // that the indexed expression is an array. As above, this can't be promoted, but still resulted | ||
| // in a query cycle. By not evaluating the const block, we avoid the cycle. Since this doesn't | ||
| // rely on promotion, it should borrow-check successfully. | ||
| let temp: &[u8] = &[0u8]; | ||
| let _ = &(temp[const { 0usize }] & 0u8); | ||
|
|
||
| // By no longer promoting these temporaries, they're dropped at the ends of their respective | ||
| // statements, so we can't refer to them thereafter. This code no longer query-cycles, but it | ||
| // fails to borrow-check instead. | ||
| #[cfg(fail)] | ||
| { | ||
| let (x, y, z); | ||
| x = &([0][const { 0 }] & 0); | ||
| //[fail]~^ ERROR: temporary value dropped while borrowed | ||
| y = &(1 / const { 1 }); | ||
| //[fail]~^ ERROR: temporary value dropped while borrowed | ||
| z = &(const { 1 } / -1); | ||
| //[fail]~^ ERROR: temporary value dropped while borrowed | ||
| (x, y, z); | ||
| } | ||
|
|
||
| // Sanity check: those temporaries do promote if the const blocks are removed. | ||
| // If constant promotion is changed so that these are no longer implicitly promoted, the | ||
| // comments on this test file should be reworded to reflect that. | ||
| let (x, y, z); | ||
| x = &([0][0] & 0); | ||
| y = &(1 / 1); | ||
| z = &(1 / -1); | ||
| (x, y, z); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one more thought, this test currently has some statements which should compile and others which should error, can you split them into 2 revisions or tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opted for 2 revisions to keep the context for the tests all in one place