1
Fork 0

Auto merge of #125077 - spastorino:add-new-fnsafety-enum2, r=jackh726

Rename Unsafe to Safety

Alternative to #124455, which is to just have one Safety enum to use everywhere, this opens the posibility of adding `ast::Safety::Safe` that's useful for unsafe extern blocks.

This leaves us today with:

```rust
enum ast::Safety {
    Unsafe(Span),
    Default,
    // Safe (going to be added for unsafe extern blocks)
}

enum hir::Safety {
    Unsafe,
    Safe,
}
```

We would convert from `ast::Safety::Default` into the right Safety level according the context.
This commit is contained in:
bors 2024-05-18 19:35:24 +00:00
commit eb1a5c9bb3
115 changed files with 460 additions and 494 deletions

View file

@ -15,7 +15,7 @@ pub enum PointerCoercion {
/// Go from a non-capturing closure to an fn pointer or an unsafe fn pointer.
/// It cannot convert a closure that requires unsafe.
ClosureFnPointer(hir::Unsafety),
ClosureFnPointer(hir::Safety),
/// Go from a mut raw pointer to a const raw pointer.
MutToConstPointer,

View file

@ -114,7 +114,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type AllocId = crate::mir::interpret::AllocId;
type Pat = Pattern<'tcx>;
type Unsafety = hir::Unsafety;
type Safety = hir::Safety;
type Abi = abi::Abi;
type Const = ty::Const<'tcx>;
@ -236,7 +236,7 @@ impl<'tcx> rustc_type_ir::inherent::Abi<TyCtxt<'tcx>> for abi::Abi {
}
}
impl<'tcx> rustc_type_ir::inherent::Unsafety<TyCtxt<'tcx>> for hir::Unsafety {
impl<'tcx> rustc_type_ir::inherent::Safety<TyCtxt<'tcx>> for hir::Safety {
fn prefix_str(self) -> &'static str {
self.prefix_str()
}
@ -2025,11 +2025,8 @@ impl<'tcx> TyCtxt<'tcx> {
/// that is, a `fn` type that is equivalent in every way for being
/// unsafe.
pub fn safe_to_unsafe_fn_ty(self, sig: PolyFnSig<'tcx>) -> Ty<'tcx> {
assert_eq!(sig.unsafety(), hir::Unsafety::Normal);
Ty::new_fn_ptr(
self,
sig.map_bound(|sig| ty::FnSig { unsafety: hir::Unsafety::Unsafe, ..sig }),
)
assert_eq!(sig.safety(), hir::Safety::Safe);
Ty::new_fn_ptr(self, sig.map_bound(|sig| ty::FnSig { safety: hir::Safety::Unsafe, ..sig }))
}
/// Given the def_id of a Trait `trait_def_id` and the name of an associated item `assoc_name`
@ -2086,20 +2083,16 @@ impl<'tcx> TyCtxt<'tcx> {
/// and so forth -- so e.g., if we have a sig with `Fn<(u32, i32)>` then
/// you would get a `fn(u32, i32)`.
/// `unsafety` determines the unsafety of the fn signature. If you pass
/// `hir::Unsafety::Unsafe` in the previous example, then you would get
/// `hir::Safety::Unsafe` in the previous example, then you would get
/// an `unsafe fn (u32, i32)`.
/// It cannot convert a closure that requires unsafe.
pub fn signature_unclosure(
self,
sig: PolyFnSig<'tcx>,
unsafety: hir::Unsafety,
) -> PolyFnSig<'tcx> {
pub fn signature_unclosure(self, sig: PolyFnSig<'tcx>, safety: hir::Safety) -> PolyFnSig<'tcx> {
sig.map_bound(|s| {
let params = match s.inputs()[0].kind() {
ty::Tuple(params) => *params,
_ => bug!(),
};
self.mk_fn_sig(params, s.output(), s.c_variadic, unsafety, abi::Abi::Rust)
self.mk_fn_sig(params, s.output(), s.c_variadic, safety, abi::Abi::Rust)
})
}
@ -2366,7 +2359,7 @@ impl<'tcx> TyCtxt<'tcx> {
inputs: I,
output: I::Item,
c_variadic: bool,
unsafety: hir::Unsafety,
safety: hir::Safety,
abi: abi::Abi,
) -> T::Output
where
@ -2376,7 +2369,7 @@ impl<'tcx> TyCtxt<'tcx> {
T::collect_and_apply(inputs.into_iter().chain(iter::once(output)), |xs| ty::FnSig {
inputs_and_output: self.mk_type_list(xs),
c_variadic,
unsafety,
safety,
abi,
})
}

View file

@ -34,7 +34,7 @@ pub enum TypeError<'tcx> {
Mismatch,
ConstnessMismatch(ExpectedFound<ty::BoundConstness>),
PolarityMismatch(ExpectedFound<ty::PredicatePolarity>),
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
SafetyMismatch(ExpectedFound<hir::Safety>),
AbiMismatch(ExpectedFound<abi::Abi>),
Mutability,
ArgumentMutability(usize),
@ -107,7 +107,7 @@ impl<'tcx> TypeError<'tcx> {
format!("expected {} polarity, found {} polarity", values.expected, values.found)
.into()
}
UnsafetyMismatch(values) => {
SafetyMismatch(values) => {
format!("expected {} fn, found {} fn", values.expected, values.found).into()
}
AbiMismatch(values) => {
@ -204,7 +204,7 @@ impl<'tcx> TypeError<'tcx> {
pub fn must_include_note(self) -> bool {
use self::TypeError::*;
match self {
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | ConstnessMismatch(_)
CyclicTy(_) | CyclicConst(_) | SafetyMismatch(_) | ConstnessMismatch(_)
| PolarityMismatch(_) | Mismatch | AbiMismatch(_) | FixedArraySize(_)
| ArgumentSorts(..) | Sorts(_) | IntMismatch(_) | FloatMismatch(_)
| VariadicMismatch(_) | TargetFeatureCast(_) => false,

View file

@ -281,13 +281,13 @@ impl DeepRejectCtxt {
}
ty::FnPtr(obl_sig) => match k {
ty::FnPtr(impl_sig) => {
let ty::FnSig { inputs_and_output, c_variadic, unsafety, abi } =
let ty::FnSig { inputs_and_output, c_variadic, safety, abi } =
obl_sig.skip_binder();
let impl_sig = impl_sig.skip_binder();
abi == impl_sig.abi
&& c_variadic == impl_sig.c_variadic
&& unsafety == impl_sig.unsafety
&& safety == impl_sig.safety
&& inputs_and_output.len() == impl_sig.inputs_and_output.len()
&& iter::zip(inputs_and_output, impl_sig.inputs_and_output)
.all(|(obl, imp)| self.types_may_unify(obl, imp))

View file

@ -267,7 +267,7 @@ pub struct ImplHeader<'tcx> {
pub struct ImplTraitHeader<'tcx> {
pub trait_ref: ty::EarlyBinder<ty::TraitRef<'tcx>>,
pub polarity: ImplPolarity,
pub unsafety: hir::Unsafety,
pub safety: hir::Safety,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)]

View file

@ -3035,7 +3035,7 @@ define_print! {
(self, cx):
ty::FnSig<'tcx> {
p!(write("{}", self.unsafety.prefix_str()));
p!(write("{}", self.safety.prefix_str()));
if self.abi != Abi::Rust {
p!(write("extern {} ", self.abi));

View file

@ -146,7 +146,7 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
if a.c_variadic != b.c_variadic {
return Err(TypeError::VariadicMismatch(expected_found(a.c_variadic, b.c_variadic)));
}
let unsafety = relation.relate(a.unsafety, b.unsafety)?;
let safety = relation.relate(a.safety, b.safety)?;
let abi = relation.relate(a.abi, b.abi)?;
if a.inputs().len() != b.inputs().len() {
@ -181,7 +181,7 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
Ok(ty::FnSig {
inputs_and_output: tcx.mk_type_list_from_iter(inputs_and_output)?,
c_variadic: a.c_variadic,
unsafety,
safety,
abi,
})
}
@ -197,13 +197,13 @@ impl<'tcx> Relate<'tcx> for ty::BoundConstness {
}
}
impl<'tcx> Relate<'tcx> for hir::Unsafety {
impl<'tcx> Relate<'tcx> for hir::Safety {
fn relate<R: TypeRelation<'tcx>>(
_relation: &mut R,
a: hir::Unsafety,
b: hir::Unsafety,
) -> RelateResult<'tcx, hir::Unsafety> {
if a != b { Err(TypeError::UnsafetyMismatch(expected_found(a, b))) } else { Ok(a) }
a: hir::Safety,
b: hir::Safety,
) -> RelateResult<'tcx, hir::Safety> {
if a != b { Err(TypeError::SafetyMismatch(expected_found(a, b))) } else { Ok(a) }
}
}

View file

@ -355,7 +355,7 @@ TrivialTypeTraversalImpls! {
// interners).
TrivialTypeTraversalAndLiftImpls! {
::rustc_hir::def_id::DefId,
::rustc_hir::Unsafety,
::rustc_hir::Safety,
::rustc_target::spec::abi::Abi,
crate::ty::ClosureKind,
crate::ty::ParamConst,

View file

@ -388,7 +388,7 @@ impl<'tcx> CoroutineClosureArgs<'tcx> {
yield_ty,
return_ty,
c_variadic: sig.c_variadic,
unsafety: sig.unsafety,
safety: sig.safety,
abi: sig.abi,
}
})
@ -416,8 +416,8 @@ pub struct CoroutineClosureSignature<'tcx> {
// from scratch just for good measure.
/// Always false
pub c_variadic: bool,
/// Always [`hir::Unsafety::Normal`]
pub unsafety: hir::Unsafety,
/// Always [`hir::Safety::Safe`]
pub safety: hir::Safety,
/// Always [`abi::Abi::RustCall`]
pub abi: abi::Abi,
}
@ -1129,8 +1129,8 @@ impl<'tcx> PolyFnSig<'tcx> {
self.skip_binder().c_variadic
}
pub fn unsafety(&self) -> hir::Unsafety {
self.skip_binder().unsafety
pub fn safety(&self) -> hir::Safety {
self.skip_binder().safety
}
pub fn abi(&self) -> abi::Abi {
@ -1140,12 +1140,7 @@ impl<'tcx> PolyFnSig<'tcx> {
pub fn is_fn_trait_compatible(&self) -> bool {
matches!(
self.skip_binder(),
ty::FnSig {
unsafety: rustc_hir::Unsafety::Normal,
abi: Abi::Rust,
c_variadic: false,
..
}
ty::FnSig { safety: rustc_hir::Safety::Safe, abi: Abi::Rust, c_variadic: false, .. }
)
}
}
@ -1991,7 +1986,7 @@ impl<'tcx> Ty<'tcx> {
ty::Binder::dummy(ty::FnSig {
inputs_and_output: ty::List::empty(),
c_variadic: false,
unsafety: hir::Unsafety::Normal,
safety: hir::Safety::Safe,
abi: abi::Abi::Rust,
})
}

View file

@ -15,7 +15,7 @@ use rustc_macros::{Decodable, Encodable, HashStable};
pub struct TraitDef {
pub def_id: DefId,
pub unsafety: hir::Unsafety,
pub safety: hir::Safety,
/// If `true`, then this trait had the `#[rustc_paren_sugar]`
/// attribute, indicating that it should be used with `Foo()`

View file

@ -65,7 +65,7 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
std::iter::repeat(err).take(arity),
err,
false,
rustc_hir::Unsafety::Normal,
rustc_hir::Safety::Safe,
rustc_target::spec::abi::Abi::Rust,
));