Skip to content

Miscompilation of mutable reference to const temporary #31234

Closed
@jethrogb

Description

@jethrogb
trait XLen {
    fn xlen(&mut self) -> usize;
}

impl<'a> XLen for &'a [u8] {
    fn xlen(&mut self) -> usize {
        self.len()
    }
}

fn main() {
    const A: &'static [u8] = b"x";
    println!("{} {}",A.len(),A.xlen());
}

This code compiles, but yields:

1 2097865012304223517

The second number may vary.

This used to lead to segfaults (in 1.5.0 and earlier) in code such as:

use std::io::Read;
fn main() {
    const A: &'static [u8] = b"x";
    let mut a=[0u8;32];
    A.read(&mut a);
}

This doesn't segfault in 1.6.0 anymore, but that is due to an unrelated change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-lowLow priorityT-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