Skip to content

Drop the dereferenceable attribute when a reference is passed in an enum #131834

Description

Considering the following code, it should print 0 with a release build, but I get a segmentation violation.

#![feature(rustc_attrs)]
#![allow(internal_features)]
#![allow(dead_code)]

#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_layout_scalar_valid_range_end(0xfffffffffffffff0)]
pub struct RestrictedAddress(&'static i8);

enum E {
    A(RestrictedAddress),
    B,
    C,
}

#[no_mangle]
#[inline(never)]
fn foo(a: E) -> i8 {
    match a {
        E::A(v) => *v.0,
        _ => 0,
    }
}

fn main() {
    println!("{}", foo(std::hint::black_box(E::B)));
}

This is because we are adding the dereferenceable attribute, which allows LLVM to hoist the load instruction. In this case, we need to drop the dereferenceable attribute.

@rustbot label +requires-nightly +I-miscompile +A-codegen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions