Skip to content

Danger with stack-allocated structs/initializer syntax #9259

Closed
@alexcrichton

Description

@alexcrichton

This code produces some disturbing results:

struct A<'self> {                                  
    a: &'self [~str],                              
    b: Option<&'self [~str]>,                      
}                                                  

fn main() {                                        
    let b = Some(&[~"foo"]);                       
    let a = A {                                    
        a: [~"test"],                              
        b: b,                                      
    };                                             
    assert_eq!(a.b.get_ref()[0].as_slice(), "foo");
}                                                  
$ rust run bar.rs
warning: no debug symbols in executable (-arch x86_64)
task <unnamed> failed at 'assertion failed: `(left == right) && (right == left)` (left: `"test"`, right: `"foo"`)', bar.rs:12

Somehow the string ~"foo" is getting overwritten with ~"test"...

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationI-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.P-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions