Skip to content

Commit 71b6555

Browse files
jhprattgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#136660 - compiler-errors:BikeshedGuaranteedNoDrop, r=lcnr
Use a trait to enforce field validity for union fields + `unsafe` fields + `unsafe<>` binder types This PR introduces a new, internal-only trait called `BikeshedGuaranteedNoDrop`[^1] to faithfully model the field check that used to be implemented manually by `allowed_union_or_unsafe_field`. https://github.com/rust-lang/rust/blob/942db6782f4a28c55b0b75b38fd4394d0483390f/compiler/rustc_hir_analysis/src/check/check.rs#L84-L115 Copying over the doc comment from the trait: ```rust /// Marker trait for the types that are allowed in union fields, unsafe fields, /// and unsafe binder types. /// /// Implemented for: /// * `&T`, `&mut T` for all `T`, /// * `ManuallyDrop<T>` for all `T`, /// * tuples and arrays whose elements implement `BikeshedGuaranteedNoDrop`, /// * or otherwise, all types that are `Copy`. /// /// Notably, this doesn't include all trivially-destructible types for semver /// reasons. /// /// Bikeshed name for now. ``` As far as I am aware, there's no new behavior being guaranteed by this trait, since it operates the same as the manually implemented check. We could easily rip out this trait and go back to using the manually implemented check for union fields, however using a trait means that this code can be shared by WF for `unsafe<>` binders too. See the last commit. The only diagnostic changes are that this now fires false-negatives for fields that are ill-formed. I don't consider that to be much of a problem though. r? oli-obk [^1]: Please let's not bikeshed this name lol. There's no good name for `ValidForUnsafeFieldsUnsafeBindersAndUnionFields`.
2 parents 7f43e01 + 59fb4ee commit 71b6555

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

core/src/marker.rs

+17
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,23 @@ impl Copy for ! {}
453453
#[stable(feature = "rust1", since = "1.0.0")]
454454
impl<T: ?Sized> Copy for &T {}
455455

456+
/// Marker trait for the types that are allowed in union fields, unsafe fields,
457+
/// and unsafe binder types.
458+
///
459+
/// Implemented for:
460+
/// * `&T`, `&mut T` for all `T`,
461+
/// * `ManuallyDrop<T>` for all `T`,
462+
/// * tuples and arrays whose elements implement `BikeshedGuaranteedNoDrop`,
463+
/// * or otherwise, all types that are `Copy`.
464+
///
465+
/// Notably, this doesn't include all trivially-destructible types for semver
466+
/// reasons.
467+
///
468+
/// Bikeshed name for now.
469+
#[unstable(feature = "bikeshed_guaranteed_no_drop", issue = "none")]
470+
#[cfg_attr(not(bootstrap), lang = "bikeshed_guaranteed_no_drop")]
471+
pub trait BikeshedGuaranteedNoDrop {}
472+
456473
/// Types for which it is safe to share references between threads.
457474
///
458475
/// This trait is automatically implemented when the compiler determines

0 commit comments

Comments
 (0)