Skip to content

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

Open
@m-ou-se

Description

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

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

    Issue actions