Skip to content

Returning MaybeUninit consts generates code to zero out the return value. #83657

Closed
@rodrimati1992

Description

@rodrimati1992

I tried this code:

use std::mem::MaybeUninit;

pub const fn foo() -> MaybeUninit<[u8; 10]> {
    const M: MaybeUninit<[u8; 10]> = MaybeUninit::uninit();
    M
}

pub const fn bar() -> MaybeUninit<[u8; 10]> {
    MaybeUninit::uninit()
}

I expected to see this happen: Both functions to compile to just a return instruction

Instead, this happened: Only bar did, foo zeros the return value in release builds.

playground::foo: # @playground::foo
# %bb.0:
	xorl	%eax, %eax
	xorl	%edx, %edx
	retq
                                        # -- End function

playground::bar: # @playground::bar
# %bb.0:
	retq
                                        # -- End function

Meta

rustc --version --verbose:

1.53.0-nightly (2021-03-28 4a20eb6a9da36c88ee92)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions