Skip to content

Can't use const fn directly as inline assembly constant input #41337

Closed
@parched

Description

@parched

I hoped this would work

const fn get_3() -> u32
{
    3
}

pub unsafe fn use_get_3()
{
    asm!("" : : "i"(get_3()))
}

but it generates (without optimizations)

  %0 = call i32 @_ZN8rust_out5get_317hccb90954bf88bf3cE()
  call void asm "", "i,~{dirflag},~{fpsr},~{flags}"(i32 %0)

giving error: invalid operand for inline asm constraint 'i'.

The current solution seems to be

pub unsafe fn use_three()
{
    const THREE: u32 = get_3();
    asm!("" : : "i"(THREE))
}

which generates

  call void asm "", "i,~{dirflag},~{fpsr},~{flags}"(i32 3)

like I hoped the first case would.

Could we make the first case guaranteed to work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions