Skip to content

"encountered dangling pointer in final value of constant" solved by nested const { const {} } #137021

@m-ou-se

Description

@m-ou-se
Member
    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

Activity

added
C-bugCategory: This is a bug.
F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Feb 14, 2025
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Feb 14, 2025
theemathas

theemathas commented on Feb 14, 2025

@theemathas
Contributor

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

compiler-errors commented on Feb 14, 2025

@compiler-errors
Member

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:

const { let s = String::new(); &s }
added
C-discussionCategory: Discussion or questions that doesn't represent real issues.
and removed
C-bugCategory: This is a bug.
on Feb 14, 2025
m-ou-se

m-ou-se commented on Feb 14, 2025

@m-ou-se
MemberAuthor

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

saethlin commented on Feb 15, 2025

@saethlin
Member

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?

added
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
on Feb 15, 2025
removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-discussionCategory: Discussion or questions that doesn't represent real issues.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @m-ou-se@compiler-errors@theemathas@saethlin@fmease

        Issue actions

          "encountered dangling pointer in final value of constant" solved by nested const { const {} } · Issue #137021 · rust-lang/rust