Skip to content

adds better error message for temporary value does not live long enough#154810

Open
Hiryxx wants to merge 1 commit intorust-lang:mainfrom
Hiryxx:fix-inline-const-e0492
Open

adds better error message for temporary value does not live long enough#154810
Hiryxx wants to merge 1 commit intorust-lang:mainfrom
Hiryxx:fix-inline-const-e0492

Conversation

@Hiryxx
Copy link
Copy Markdown

@Hiryxx Hiryxx commented Apr 4, 2026

When &const { expr } is borrowed for 'static but expr has interior mutability (is not Freeze), emit E0492 instead
of E0716

Closes #154382

@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 Apr 4, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 4, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
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 69 candidates
  • Random selection from 9 candidates

self.dcx(),
inner_span,
E0492,
"interior mutable shared borrows of temporaries that have their \
Copy link
Copy Markdown
Member

@JohnTitor JohnTitor Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

I don't think it's a good idea to duplicate the message with

#[diag("interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed", code = E0492)]

Copy link
Copy Markdown
Member

@JohnTitor JohnTitor Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

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)
Copy link
Copy Markdown
Member

@JohnTitor JohnTitor Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

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.

@JohnTitor JohnTitor added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Confusing error when trying to promote a const to a static item

3 participants