Skip to content

Undefined behavior in safe rust using rustc_layout_scalar_valid_range_start #115284

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

Closed
bart358 opened this issue Aug 27, 2023 · 0 comments · Fixed by #115290
Closed

Undefined behavior in safe rust using rustc_layout_scalar_valid_range_start #115284

bart358 opened this issue Aug 27, 2023 · 0 comments · Fixed by #115290
Labels
C-bug Category: This is a bug. F-rustc_attrs Internal rustc attributes gated on the `#[rustc_attrs]` feature gate. requires-internal-features This issue requires the use of internal features.

Comments

@bart358
Copy link

bart358 commented Aug 27, 2023

Given the following struct:

#[rustc_layout_scalar_valid_range_start(2)]
struct NonZeroAndOneU8(u8);

trying to initialise it directly with NonZeroAndOneU8(1) (rightfully) results in the error:

error[[E0133]](https://doc.rust-lang.org/nightly/error_codes/E0133.html): initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
 --> src/main.rs:9:5
  |
9 |     NonZeroAndOneU8(1);
  |     ^^^^^^^^^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
  |
  = note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior

For more information about this error, try `rustc --explain E0133`.
error: could not compile `playground` (bin "playground") due to previous error

But Some(1).map(NonZeroAndOneU8).unwrap() compiles just fine returning NonZeroAndOneU8(1) which is an invalid state (link to the playground).

@bart358 bart358 added the C-bug Category: This is a bug. label Aug 27, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 27, 2023
@compiler-errors compiler-errors added F-rustc_attrs Internal rustc attributes gated on the `#[rustc_attrs]` feature gate. requires-internal-features This issue requires the use of internal features. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 27, 2023
@bors bors closed this as completed in 784916c Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-rustc_attrs Internal rustc attributes gated on the `#[rustc_attrs]` feature gate. requires-internal-features This issue requires the use of internal features.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants