Skip to content

Unfortunate optimization with Option<bool> vs Option<uint> #20149

Closed
@alexcrichton

Description

@alexcrichton

Currently, given this program:

#[inline(never)]
fn get_bool(slot: &mut Option<bool>) -> &mut bool {
    if slot.is_none() {
        *slot = Some(true);
    }
    slot.as_mut().unwrap()
}

#[inline(never)]
fn get_uint(slot: &mut Option<uint>) -> &mut uint {
    if slot.is_none() {
        *slot = Some(1);
    }
    slot.as_mut().unwrap()
}

fn main() {
    println!("{}", get_bool(&mut None));
    println!("{}", get_uint(&mut None));
}

It generates this IR.

The "unfortunate" part here is that a call to failure is generated for the get_bool function where no failure is generated for the get_uint function.

There's a lot going on here so I'm not sure if it's our codegen or LLVM's codegen, but I would personally expect both cases to optimize down to never unwinding because it's known that neither Option can ever be None.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions