Skip to content

Commit

Permalink
Rollup merge of rust-lang#133934 - jswrenn:unsafe-fields-auto-traits,…
Browse files Browse the repository at this point in the history
… r=compiler-errors

Do not implement unsafe auto traits for types with unsafe fields

If a type has unsafe fields, its safety invariants are not simply the conjunction of its field types' safety invariants. Consequently, it's invalid to reason about the safety properties of these types in a purely structural manner — i.e., the manner in which `auto` traits are implemented. Consequently, auto implementations of unsafe auto traits should not be generated for types with unsafe fields.

Tracking: rust-lang#132922

r? `@compiler-errors`
  • Loading branch information
matthiaskrgr authored Dec 6, 2024
2 parents 8c78d7a + a122dde commit 6457761
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.trait_def(trait_def_id).implement_via_object
}

fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool {
self.trait_def(trait_def_id).safety == hir::Safety::Unsafe
}

fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {
self.is_impl_trait_in_trait(def_id)
}
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,14 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
fn async_destructor_ty(self, interner: TyCtxt<'tcx>) -> Ty<'tcx> {
self.async_destructor_ty(interner)
}

fn has_unsafe_fields(self) -> bool {
if let ty::Adt(adt_def, ..) = self.kind() {
adt_def.all_fields().any(|x| x.safety == hir::Safety::Unsafe)
} else {
false
}
}
}

/// Type utilities
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ where
return result;
}

// Only consider auto impls of unsafe traits when there are no unsafe
// fields.
if ecx.cx().trait_is_unsafe(goal.predicate.def_id())
&& goal.predicate.self_ty().has_unsafe_fields()
{
return Err(NoSolution);
}

// We only look into opaque types during analysis for opaque types
// outside of their defining scope. Doing so for opaques in the
// defining scope may require calling `typeck` on the same item we're
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_infer::traits::{
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::{self, ToPolyTraitRef, Ty, TypeVisitableExt, TypingMode};
use rustc_middle::{bug, span_bug};
use rustc_type_ir::Interner;
use tracing::{debug, instrument, trace};

use super::SelectionCandidate::*;
Expand Down Expand Up @@ -794,6 +795,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| ty::Never
| ty::Tuple(_)
| ty::CoroutineWitness(..) => {
use rustc_type_ir::inherent::*;

// Only consider auto impls of unsafe traits when there are
// no unsafe fields.
if self.tcx().trait_is_unsafe(def_id) && self_ty.has_unsafe_fields() {
return;
}

// Only consider auto impls if there are no manual impls for the root of `self_ty`.
//
// For example, we only consider auto candidates for `&i32: Auto` if no explicit impl
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_type_ir/src/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ pub trait Ty<I: Interner<Ty = Self>>:
matches!(self.kind(), ty::FnPtr(..))
}

/// Checks whether this type is an ADT that has unsafe fields.
fn has_unsafe_fields(self) -> bool;

fn fn_sig(self, interner: I) -> ty::Binder<I, ty::FnSig<I>> {
match self.kind() {
ty::FnPtr(sig_tys, hdr) => sig_tys.with(hdr),
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ pub trait Interner:

fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool;

/// Returns `true` if this is an `unsafe trait`.
fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool;

fn is_impl_trait_in_trait(self, def_id: Self::DefId) -> bool;

fn delay_bug(self, msg: impl ToString) -> Self::ErrorGuaranteed;
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/unsafe-fields/auto-traits.current.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0277]: the trait bound `UnsafeEnum: UnsafeAuto` is not satisfied
--> $DIR/auto-traits.rs:24:22
|
LL | impl_unsafe_auto(UnsafeEnum::Safe(42));
| ---------------- ^^^^^^^^^^^^^^^^^^^^ the trait `UnsafeAuto` is not implemented for `UnsafeEnum`
| |
| required by a bound introduced by this call
|
note: required by a bound in `impl_unsafe_auto`
--> $DIR/auto-traits.rs:20:29
|
LL | fn impl_unsafe_auto(_: impl UnsafeAuto) {}
| ^^^^^^^^^^ required by this bound in `impl_unsafe_auto`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
17 changes: 17 additions & 0 deletions tests/ui/unsafe-fields/auto-traits.next.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0277]: the trait bound `UnsafeEnum: UnsafeAuto` is not satisfied
--> $DIR/auto-traits.rs:24:22
|
LL | impl_unsafe_auto(UnsafeEnum::Safe(42));
| ---------------- ^^^^^^^^^^^^^^^^^^^^ the trait `UnsafeAuto` is not implemented for `UnsafeEnum`
| |
| required by a bound introduced by this call
|
note: required by a bound in `impl_unsafe_auto`
--> $DIR/auto-traits.rs:20:29
|
LL | fn impl_unsafe_auto(_: impl UnsafeAuto) {}
| ^^^^^^^^^^ required by this bound in `impl_unsafe_auto`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
26 changes: 26 additions & 0 deletions tests/ui/unsafe-fields/auto-traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//@ compile-flags: --crate-type=lib
//@ revisions: current next
//@[next] compile-flags: -Znext-solver

#![feature(auto_traits)]
#![feature(unsafe_fields)]
#![allow(incomplete_features)]

enum UnsafeEnum {
Safe(u8),
Unsafe { unsafe field: u8 },
}

auto trait SafeAuto {}

fn impl_safe_auto(_: impl SafeAuto) {}

unsafe auto trait UnsafeAuto {}

fn impl_unsafe_auto(_: impl UnsafeAuto) {}

fn tests() {
impl_safe_auto(UnsafeEnum::Safe(42));
impl_unsafe_auto(UnsafeEnum::Safe(42));
//~^ ERROR the trait bound `UnsafeEnum: UnsafeAuto` is not satisfied
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 6457761

Please sign in to comment.