-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
adds better error message for temporary value does not live long enough #154810
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
Open
Hiryxx
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
Hiryxx:fix-inline-const-e0492
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
64 changes: 64 additions & 0 deletions
64
tests/ui/inline-const/interior-mutable-borrow-issue-154382.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,64 @@ | ||
| // Verify that `&const { expr }` where `expr` has interior mutability | ||
| // gets E0492 instead of E0716. | ||
| // https://github.com/rust-lang/rust/issues/154382 | ||
|
|
||
| use std::cell::Cell; | ||
| use std::sync::atomic::AtomicUsize; | ||
|
|
||
| struct Mutable(Cell<u32>); | ||
| impl Mutable { | ||
| const fn new(a: u32) -> Self { Self(Cell::new(a)) } | ||
| } | ||
|
|
||
| fn foo() -> &'static Mutable { | ||
| &const { Mutable::new(0) } | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
| } | ||
|
|
||
| struct Holder { | ||
| val: &'static Mutable, | ||
| } | ||
|
|
||
| fn takes_static(_: &'static Mutable) {} | ||
|
|
||
| fn via_closure() { | ||
| let _f: fn() -> &'static Mutable = || &const { Mutable::new(0) }; | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
| } | ||
|
|
||
| fn via_struct() { | ||
| let _h = Holder { val: &const { Mutable::new(0) } }; | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
| } | ||
|
|
||
| fn via_argument() { | ||
| takes_static(&const { Mutable::new(0) }); | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
| } | ||
|
|
||
| // The `let` binding suggestion should still be offered alongside E0492. | ||
| fn deferred_assignment() { | ||
| let r; | ||
| r = &const { Cell::new(0) }; | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
| r.set(1); | ||
| } | ||
|
|
||
| // A `&mut` borrow is not a shared borrow, so it keeps the E0716 diagnostic. | ||
| fn mut_borrow() { | ||
| let _: &'static mut _ = &mut const { Cell::new(0) }; | ||
| //~^ ERROR temporary value dropped while borrowed | ||
| } | ||
|
|
||
| fn main() { | ||
| let _: &'static _ = &const { 0u32 }; | ||
|
|
||
| let _: &'static _ = &const { Mutable::new(0u32) }; | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
|
|
||
| let _: &'static _ = const { &Mutable::new(0u32) }; | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
|
|
||
| let _: &'static _ = &const { AtomicUsize::new(0) }; | ||
| //~^ ERROR interior mutable shared borrows of temporaries | ||
| } |
120 changes: 120 additions & 0 deletions
120
tests/ui/inline-const/interior-mutable-borrow-issue-154382.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,120 @@ | ||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:14:14 | ||
| | | ||
| LL | &const { Mutable::new(0) } | ||
| | ---------^^^^^^^^^^^^^^^-- | ||
| | | | | ||
| | | this borrow of an interior mutable value refers to such a temporary | ||
| | returning this value requires that borrow lasts for `'static` | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
|
|
||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:25:52 | ||
| | | ||
| LL | let _f: fn() -> &'static Mutable = || &const { Mutable::new(0) }; | ||
| | - ---------^^^^^^^^^^^^^^^-- | ||
| | | | | | ||
| | | | this borrow of an interior mutable value refers to such a temporary | ||
| | | returning this value requires that borrow lasts for `'1` | ||
| | return type of closure is &'1 Mutable | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
|
|
||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:30:37 | ||
| | | ||
| LL | let _h = Holder { val: &const { Mutable::new(0) } }; | ||
| | ---------^^^^^^^^^^^^^^^-- | ||
| | | | | ||
| | | this borrow of an interior mutable value refers to such a temporary | ||
| | this usage requires that borrow lasts for `'static` | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
|
|
||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:35:27 | ||
| | | ||
| LL | takes_static(&const { Mutable::new(0) }); | ||
| | ----------------------^^^^^^^^^^^^^^^--- | ||
| | | | | ||
| | | this borrow of an interior mutable value refers to such a temporary | ||
| | argument requires that borrow lasts for `'static` | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
|
|
||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:42:18 | ||
| | | ||
| LL | r = &const { Cell::new(0) }; | ||
| | ^^^^^^^^^^^^ this borrow of an interior mutable value refers to such a temporary | ||
| LL | | ||
| LL | r.set(1); | ||
| | - borrow later used here | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
| help: consider using a `let` binding to create a longer lived value | ||
| | | ||
| LL ~ let binding = const { Cell::new(0) }; | ||
| LL ~ r = &binding; | ||
| | | ||
|
|
||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:49:34 | ||
| | | ||
| LL | let _: &'static mut _ = &mut const { Cell::new(0) }; | ||
| | -------------- ^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use | ||
| | | | ||
| | type annotation requires that borrow lasts for `'static` | ||
| LL | | ||
| LL | } | ||
| | - temporary value is freed at the end of this statement | ||
|
|
||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:59:33 | ||
| | | ||
| LL | let _: &'static _ = const { &Mutable::new(0u32) }; | ||
| | ^^^^^^^^^^^^^^^^^^^ this borrow of an interior mutable value refers to such a temporary | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
|
|
||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:56:34 | ||
| | | ||
| LL | let _: &'static _ = &const { Mutable::new(0u32) }; | ||
| | ---------- ^^^^^^^^^^^^^^^^^^ this borrow of an interior mutable value refers to such a temporary | ||
| | | | ||
| | type annotation requires that borrow lasts for `'static` | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
|
|
||
| error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed | ||
| --> $DIR/interior-mutable-borrow-issue-154382.rs:62:34 | ||
| | | ||
| LL | let _: &'static _ = &const { AtomicUsize::new(0) }; | ||
| | ---------- ^^^^^^^^^^^^^^^^^^^ this borrow of an interior mutable value refers to such a temporary | ||
| | | | ||
| | type annotation requires that borrow lasts for `'static` | ||
| | | ||
| = note: temporaries in constants and statics can have their lifetime extended until the end of the program | ||
| = note: to avoid accidentally creating global mutable state, such temporaries must be immutable | ||
| = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut` | ||
|
|
||
| error: aborting due to 9 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0492, E0716. | ||
| For more information about an error, try `rustc --explain E0492`. |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Checking interior mutability with
!Freezecan be false-positive, it isn't equal that promotion failure is due to it.Maybe we should use the existing mechanism like https://doc.rust-lang.org/nightly/nightly-rustc/rustc_const_eval/check_consts/qualifs/struct.HasMutInterior.html to harden the checker.
View changes since the review