Skip to content

Some( NULL ) returns None #17120

Closed
Closed
@kaseyc

Description

@kaseyc

If you try to take Some of a null pointer, it will be interpreted as None. This seems to occur for any two element enum with the same signature as option,

Example:

enum Foo<T> {
    Bar(T),
    Baz
}

impl<T> Foo<T> {
    fn as_string(&self) -> String {
        match *self {
            Bar(_) => "Bar(_)".to_string(),
            Baz => "Baz".to_string(),
        }
    }

    fn is_bar(&self) -> bool {
        match *self {
            Bar(_) => true,
            _ => false
        }
    }

    fn is_baz(&self) -> bool {
        match *self {
            Baz => true,
            _ => false
        }
    }
}


fn main() {
        println!("Should be Some: {}", Some( 0u8 as *const u8))
        let foo = Bar( 0u8 as *const u8 );
        println!("Should be Bar: {}\nBar: {}, Baz: {}", foo.as_string(), foo.is_bar(), foo.is_baz() );
}

Which prints:

Should be Some: None
Should be Bar: Baz
Bar: false, Baz: true

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-DSTsArea: Dynamically-sized types (DSTs)A-codegenArea: Code generation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions