Skip to content

Broken MIR, bad field access when matching constants #41808

Closed
@cpichard

Description

@cpichard

Hi,
I tried to match constant strings inside a struct and wasn't able to compile the following code:

use std::ffi::*;

#[derive(PartialEq, Eq)]
pub struct MyStruct<'a>(&'a [u8]);

pub const CONSTANT: MyStruct<'static> =
    MyStruct(b"ConstantString\0");

fn main() {
    let cstr = CString::new("ConstantString").unwrap();
    match MyStruct(cstr.to_bytes_with_nul()) {
        CONSTANT => println!("compilation error here"),
        _ => println!("not found"),
    }
}

The compiler returns an internal compilation error.

I managed to compile using:
ref v @ MyStruct(_) if *v == CONSTANT => println!("it compiles"),

The first version, matching directly the CONSTANT would be easier to read and understand.

META

rustc 1.17.0 (56124ba 2017-04-24)
binary: rustc
commit-hash: 56124ba
commit-date: 2017-04-24
host: x86_64-apple-darwin
release: 1.17.0
LLVM version: 3.9

Thanks !

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlI-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions