Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unconditionally fully initialized variable errors with possibly uninitialized #56159

Closed
gnzlbg opened this issue Nov 22, 2018 · 2 comments
Closed

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Nov 22, 2018

This works (Playground):

pub unsafe fn foo() -> u32 {
    let r: u32;
    asm!("cpuid\n"
             : "={eax}"(r)
             : "{eax}"(0), "{ecx}"(0)
             : "rbx" :);
    r
}

but this fails (Playground):

pub struct A { pub eax: u32 }
pub unsafe fn bar() -> A {
    let r: A;
    asm!("cpuid\n"
             : "={eax}"(r.eax)
             : "{eax}"(0), "{ecx}"(0)
             : "rbx" :);
    r
}

with

error[E0594]: cannot assign to field `r.eax` of immutable binding
 --> src/lib.rs:7:25
  |
5 |     let r: A;
  |         - help: make this binding mutable: `mut r`
6 |     asm!("cpuid\n"
7 |              : "={eax}"(r.eax)
  |                         ^^^^^ cannot mutably borrow field of immutable binding

error[E0381]: use of possibly uninitialized variable: `r`
  --> src/lib.rs:10:5
   |
10 |     r
   |     ^ use of possibly uninitialized `r`

In particular, making r mut (Playground):

pub struct A { pub eax: u32 }
pub unsafe fn bar() -> A {
    let mut r: A;
    asm!("cpuid\n"
             : "={eax}"(r.eax)
             : "{eax}"(0), "{ecx}"(0)
             : "rbx" :);
    r
}

still errors with:

error[E0381]: use of possibly uninitialized variable: `r`
  --> src/lib.rs:10:5
   |
10 |     r
   |     ^ use of possibly uninitialized `r`

even though r is fully initialized. However this works (Playground):

pub struct A { pub eax: u32 }
pub unsafe fn bar() -> A {
    let r: u32;
    asm!("cpuid\n"
             : "={eax}"(r)
             : "{eax}"(0), "{ecx}"(0)
             : "rbx" :);
    A{ eax: r }
}

I expected that all of these would either work, or not work. They all look the same to me.

@hanna-kruppe
Copy link
Contributor

See #54987

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Nov 22, 2018

This is indeed a duplicate of #54987 .

@gnzlbg gnzlbg closed this as completed Nov 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants