-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)Inline constants (aka: const blocks, const expressions, anonymous constants)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
let a = const { [ "asdf".as_ptr() ].as_ptr() }; // error
let a = const { [const { "asdf".as_ptr() }].as_ptr() }; // okay
I think either both of these should pass, or both of these should error.
However, only the first line results in:
error: encountered dangling pointer in final value of constant
Fwiw, inlining as_ptr()
results in both lines being accepted:
let a = const { [ "asdf" as *const str as *const u8 ].as_ptr() }; // okay
let a = const { [const { "asdf" as *const str as *const u8 }].as_ptr() }; // okay
Meta
rustc --version --verbose
:
rustc 1.86.0-nightly (a567209da 2025-02-13)
binary: rustc
commit-hash: a567209daab72b7ea59eac533278064396bb0534
commit-date: 2025-02-13
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7
Metadata
Metadata
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)Inline constants (aka: const blocks, const expressions, anonymous constants)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
theemathas commentedon Feb 14, 2025
I suspect that, in the second version of the code, the array is being static-promoted, and the method call outside a const block prevents the array from being static-promoted. If this is the case, the difference in behavior would be expected behavior.
compiler-errors commentedon Feb 14, 2025
Yeah, I don't think it's intuitive to expect a const block to act differently w.r.t. promoting things that are nested into the body but aren't subject to normal promotion rules. Like, I wouldn't expect this code to work:
m-ou-se commentedon Feb 14, 2025
I thought it was unexpected that nesting a const block in a const block had any effect. But if that's expected, feel free to close this.
saethlin commentedon Feb 15, 2025
This is at least the second time I have seen someone surprised that putting a const block in a const block makes this error go away.
Do we have documentation for how promotion works? Or documentation that at least confirms that promotion has made this pointer not dangling as opposed to just hiding the dangling from the compiler?