adds better error message for temporary value does not live long enough#154810
adds better error message for temporary value does not live long enough#154810Hiryxx wants to merge 4 commits into
Conversation
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| self.dcx(), | ||
| inner_span, | ||
| E0492, | ||
| "interior mutable shared borrows of temporaries that have their \ |
There was a problem hiding this comment.
I don't think it's a good idea to duplicate the message with
rust/compiler/rustc_const_eval/src/errors.rs
Line 296 in 1948ee1
There was a problem hiding this comment.
Yes, I totally agree. Though the borrow checker does not depend on rust_const_eval, what do you do in these situations? Should I move that under rustc_middle so that both can see that?
There was a problem hiding this comment.
@JohnTitor I was trying with that but rustc_middle has some errors definitions that are only used inside that module, so I am not sure it would be a good fit. I could also make the borrowchecker depend on const_evals but it seems an overkill for a single diagnostic structure, what do you think? Do you have a place in mind where you would put that struct without needing an "invasive" change.
There was a problem hiding this comment.
Could you put FIXME on both sides so that we don't forget to sync and can clean up later?
There was a problem hiding this comment.
Please put the issue number in a file title and the link in the comment for reference.
|
|
||
| // Emit E0492 for `&const { expr }` when `expr` has | ||
| // interior mutability, since that's what actually prevents promotion. | ||
| if let Some(expr) = self.find_expr(proper_span) |
There was a problem hiding this comment.
I guess we could expand the fix for a similar case where promotion failed because the value is not Freeze, like:
use std::cell::Cell;
struct Mutable(Cell<u32>);
impl Mutable {
const fn new(a: u32) -> Self { Self(Cell::new(a)) }
}
fn foo() -> &'static Mutable {
&const { Mutable::new(0) }
}
fn main() {}The current only addresses the case issue mentioned, I guess it's kinda ad hoc. These cases should have the same diagnostics for consistency.
There was a problem hiding this comment.
@Hiryxx I don't think this was addressed properly, consider a case like:
fn main() {
let _: &'static mut _ = &mut const { Cell::new(0) };
}
This comment has been minimized.
This comment has been minimized.
| "if you really want global mutable state, try replacing the \ | ||
| temporary by an interior mutable `static` or a `static mut`", | ||
| ); | ||
| return err; |
There was a problem hiding this comment.
Because of this return, it could drop a suggestion for a case like:
fn main() {
let r;
r = &const { Cell::new(0) };
r.set(1);
}I'd keep it.
This comment has been minimized.
This comment has been minimized.
089360f to
a91dbb5
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
I had to pull the detection into a span variable up top and reuse it as a flag, this makes it a little verbose for the error output but I couldn't find a better since the alternatives where worse. Let me know what you think @JohnTitor. |
|
☔ The latest upstream changes (presumably #159170) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
When &const { expr } is borrowed for 'static but expr has interior mutability (is not Freeze), emit E0492 instead
of E0716
Closes #154382