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

Unsafe block is overlooked #61365

Closed
DutchGhost opened this issue May 30, 2019 · 8 comments
Closed

Unsafe block is overlooked #61365

DutchGhost opened this issue May 30, 2019 · 8 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@DutchGhost
Copy link
Contributor

DutchGhost commented May 30, 2019

I know this code horribly wrong, and it doesnt compile. Even when I expect it to compile:

#![feature(const_fn_union)]
#![feature(const_generics)]

union Transmute {
    example: Example,
    num: usize,
}
    
#[derive(Copy, Clone)]
#[repr(C)]
enum Example {
    field = 1,
}

const fn transpose(n: usize) -> Example {
    unsafe {
        Transmute { num: n }.example
    }
}


fn main() {
}

It complains that we need an unsafe block:

error[E0133]: access to union field is unsafe and requires unsafe function or block
  --> src/main.rs:17:9
   |
17 |         Transmute { num: n }.example
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ access to union field
   |
   = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error: aborting due to previous error

but we wrapped it in an unsafe block!

Changing the function signature to const *unsafe* fn transpose(n: usize) -> Example does compile, and then we can leave out the inner unsafe block.

@jonas-schievink jonas-schievink added A-const-fn C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2019
@Centril
Copy link
Contributor

Centril commented May 30, 2019

cc @oli-obk

@hellow554
Copy link
Contributor

Slightly reduced:

#![feature(const_fn_union)]

union Transmute {
    e: u32,
}

pub const fn transpose() -> u32 {
    unsafe { Transmute { e: 0 }.e }
}

note that pub const unsafe fn transpose does work as expected

@oli-obk
Copy link
Contributor

oli-obk commented May 31, 2019

Changing the function signature to const unsafe fn transpose(n: usize) -> Example does compile, and then we can leave out the inner unsafe block.

oh oh. That's bad. That shouldn't happen. (As unions are not ok in const fn under any circumstances at thistime). Although const unsafe fn isn't really that bad, as you can't do anything weird with it I guess. cc @RalfJung

@oli-obk
Copy link
Contributor

oli-obk commented May 31, 2019

Note that with a feature gate you can get this without const unsafe fn: #51909

@RalfJung
Copy link
Member

Yeah IIRC we wanted to ban union field accesses in all const fn including unsafe const fn. Why is that even a separate code path?

@oli-obk
Copy link
Contributor

oli-obk commented May 31, 2019

Oh.. wait, the const_fn_union feature gate is activated. Nevermind, sorry about the ping. This is just a bug in the feature gate not allowing enough.

@DutchGhost
Copy link
Contributor Author

Related to #59729 maybe?

@RalfJung
Copy link
Member

The code in the OP compiles in the latest nightly. Closing.

@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants