Skip to content

2021 edition: Unsafe block not detected when accessing a union field inside a closure #87378

Closed
@lazytype

Description

@lazytype

Code

I tried this code:

union Union {
    value: u64,
}

fn main() {
    let u = Union { value: 42 };
    
    || {
       unsafe { u.value }
    };
}

I expected to see this happen: no error

Instead, this happened: error[E0133]: access to union field is unsafe and requires unsafe function or block

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=091d060959b30415131533f2e6d93f71

Version it worked on

rustc 1.55.0-nightly 2018 edition
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=091d060959b30415131533f2e6d93f71

Version with regression

rustc 1.55.0-nightly 2021 edition
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=091d060959b30415131533f2e6d93f71

Metadata

Metadata

Assignees

Labels

A-edition-2021Area: The 2021 editionC-bugCategory: This is a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions