Skip to content

Memory corruption when matching on an Option of a vector slice #8498

Closed
@catamorphism

Description

@catamorphism
use std::io;

fn main() {
// This is ok
    match &[(~5,~7)] {
        ps => {
           let (ref y, _) = ps[0];
           io::println(fmt!("1. y = %d", **y));
           assert!(**y == 5);
        }
    }

// This is not entirely ok
    match Some(&[(~5,)]) {
        Some(ps) => {
           let (ref y,) = ps[0];
           io::println(fmt!("2. y = %d", **y));
           if **y != 5 { io::println("sadness"); }
        }
        None => ()
    }

// This is not ok
    match Some(&[(~5,~7)]) {
        Some(ps) => {
           let (ref y, ref z) = ps[0];
           io::println(fmt!("3. y = %d z = %d", **y, **z));
           assert!(**y == 5);
        }
        None => ()
    }
}

The output:

1. y = 5
2. y = 5
sadness
3. y = 2 z = 3458764513820540928
task <unnamed> failed at 'assertion failed: **y == 5', /Users/tjc/rust/src/test/run-pass/spawn-env-2.rs:28

In the second match, the first print statement prints out the right answer, but reading **y for the second time yields garbage (perhaps y is wrongly getting moved out of here even though it's a ref binding?). The third match just prints out garbage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions