Skip to content

Garbage value when accessing a reference into a field/element of a const value. #49955

Closed
@shelvacu

Description

@shelvacu

All of the below code seems to be neccesary to reproduce the bug; If ALL_THE_NUMS is accessed directly within main, the program works as expected.

Example:

fn get_element() -> &'static u32 {
    return &ALL_THE_NUMS[0];
}

const ALL_THE_NUMS : [u32; 1] = [
    1
];

fn main(){
    println!("Num is {}", get_element());
}

I expected to see this happen: Num is 1 as the output, or a compiler error if I'm doing something wrong here (am I?)

Instead, this happened: Num is 32766 or some other garbage value is output

Meta

rustc --version --verbose:

rustc 1.25.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.25.0
LLVM version: 6.0

Also tested on master-branch version: (same result)

rustc 1.27.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.27.0-dev
LLVM version: 6.0

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-NLLBugs fixed, but only when NLL is enabled.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions