diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index 4e5f4408ca31a..965988c7b21d8 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -1085,8 +1085,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { } // Build a new closure where the return type is an owned value, instead of a ref. - let fn_sig_kind = - FnSigKind::default().set_safe(true).set_c_variadic(liberated_sig.c_variadic()); + let fn_sig_kind = FnSigKind::default() + .set_safety(hir::Safety::Safe) + .set_c_variadic(liberated_sig.c_variadic()); let closure_sig_as_fn_ptr_ty = Ty::new_fn_ptr( tcx, ty::Binder::dummy(tcx.mk_fn_sig( diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index a027c552d92b9..31fd54236c41f 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -3590,7 +3590,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let fn_sig_kind = FnSigKind::default() .set_abi(abi) - .set_safe(safety.is_safe()) + .set_safety(safety) .set_c_variadic(decl.fn_decl_kind.c_variadic()); let fn_ty = tcx.mk_fn_sig(input_tys, output_ty, fn_sig_kind); let fn_ptr_ty = ty::Binder::bind_with_vars(fn_ty, bound_vars); diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index d90cbaf935a71..eded203dac9a5 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -723,7 +723,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let bound_sig = expected_sig.sig.map_bound(|sig| { let fn_sig_kind = FnSigKind::default() .set_abi(ExternAbi::RustCall) - .set_safe(true) + .set_safety(hir::Safety::Safe) .set_c_variadic(sig.c_variadic()); self.tcx.mk_fn_sig(sig.inputs().iter().cloned(), sig.output(), fn_sig_kind) }); @@ -860,7 +860,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let fn_sig_kind = FnSigKind::default() .set_abi(ExternAbi::RustCall) - .set_safe(true) + .set_safety(hir::Safety::Safe) .set_c_variadic(expected_sigs.liberated_sig.c_variadic()); expected_sigs.liberated_sig = self.tcx.mk_fn_sig(inputs, supplied_output_ty, fn_sig_kind); @@ -935,7 +935,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let fn_sig_kind = FnSigKind::default() .set_abi(ExternAbi::RustCall) - .set_safe(true) + .set_safety(hir::Safety::Safe) .set_c_variadic(decl.c_variadic()); let result = ty::Binder::bind_with_vars( self.tcx.mk_fn_sig(supplied_arguments, supplied_return, fn_sig_kind), @@ -1098,7 +1098,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let fn_sig_kind = FnSigKind::default() .set_abi(ExternAbi::RustCall) - .set_safe(true) + .set_safety(hir::Safety::Safe) .set_c_variadic(decl.c_variadic()); let result = ty::Binder::dummy(self.tcx.mk_fn_sig(supplied_arguments, err_ty, fn_sig_kind)); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 71b0077b62131..61929d9654dcf 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -47,9 +47,7 @@ use rustc_span::def_id::{CRATE_DEF_ID, DefPathHash, StableCrateId}; use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym}; use rustc_type_ir::TyKind::*; pub use rustc_type_ir::lift::Lift; -use rustc_type_ir::{ - CollectAndApply, FnSigKind, TypeFlags, WithCachedTypeInfo, elaborate, search_graph, -}; +use rustc_type_ir::{CollectAndApply, TypeFlags, WithCachedTypeInfo, elaborate, search_graph}; use tracing::{debug, instrument}; use crate::arena::Arena; @@ -69,11 +67,11 @@ use crate::traits; use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques}; use crate::ty::predicate::ExistentialPredicateStableCmpExt as _; use crate::ty::{ - self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, GenericArg, GenericArgs, - GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, Pattern, - PatternKind, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, PredicatePolarity, - Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid, ValTree, ValTreeKind, - Visibility, + self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, FnSigKind, GenericArg, + GenericArgs, GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, + Pattern, PatternKind, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, + PredicatePolarity, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid, + ValTree, ValTreeKind, Visibility, }; impl<'tcx> rustc_type_ir::inherent::DefId> for DefId { @@ -86,50 +84,6 @@ impl<'tcx> rustc_type_ir::inherent::DefId> for DefId { } } -impl<'tcx> rustc_type_ir::inherent::FSigKind> for FnSigKind { - fn fn_sig_kind(self) -> Self { - self - } - - fn new(abi: ExternAbi, safety: hir::Safety, c_variadic: bool) -> Self { - FnSigKind::default().set_abi(abi).set_safe(safety.is_safe()).set_c_variadic(c_variadic) - } - - fn abi(self) -> ExternAbi { - self.abi() - } - - fn safety(self) -> hir::Safety { - if self.is_safe() { hir::Safety::Safe } else { hir::Safety::Unsafe } - } - - fn c_variadic(self) -> bool { - self.c_variadic() - } -} - -impl<'tcx> rustc_type_ir::inherent::Abi> for ExternAbi { - fn abi(self) -> Self { - self - } - - fn rust() -> Self { - ExternAbi::Rust - } - - fn is_rust(self) -> bool { - matches!(self, ExternAbi::Rust) - } - - fn pack_abi(self) -> u8 { - self.as_packed() - } - - fn unpack_abi(abi_index: u8) -> Self { - Self::from_packed(abi_index) - } -} - impl<'tcx> rustc_type_ir::inherent::Safety> for hir::Safety { fn safe() -> Self { hir::Safety::Safe @@ -1374,7 +1328,7 @@ impl<'tcx> TyCtxt<'tcx> { let caller_features = &self.body_codegen_attrs(caller).target_features; if self.is_target_feature_call_safe(&fun_features, &caller_features) { return Some(fun_sig.map_bound(|sig| ty::FnSig { - fn_sig_kind: fun_sig.fn_sig_kind().set_safe(true), + fn_sig_kind: fun_sig.fn_sig_kind().set_safety(hir::Safety::Safe), ..sig })); } @@ -2143,7 +2097,10 @@ impl<'tcx> TyCtxt<'tcx> { assert!(sig.safety().is_safe()); Ty::new_fn_ptr( self, - sig.map_bound(|sig| ty::FnSig { fn_sig_kind: sig.fn_sig_kind.set_safe(false), ..sig }), + sig.map_bound(|sig| ty::FnSig { + fn_sig_kind: sig.fn_sig_kind.set_safety(hir::Safety::Unsafe), + ..sig + }), ) } @@ -2152,7 +2109,10 @@ impl<'tcx> TyCtxt<'tcx> { /// unsafe. pub fn safe_to_unsafe_sig(self, sig: PolyFnSig<'tcx>) -> PolyFnSig<'tcx> { assert!(sig.safety().is_safe()); - sig.map_bound(|sig| ty::FnSig { fn_sig_kind: sig.fn_sig_kind.set_safe(false), ..sig }) + sig.map_bound(|sig| ty::FnSig { + fn_sig_kind: sig.fn_sig_kind.set_safety(hir::Safety::Unsafe), + ..sig + }) } /// Given the def_id of a Trait `trait_def_id` and the name of an associated item `assoc_name` @@ -2197,7 +2157,7 @@ impl<'tcx> TyCtxt<'tcx> { self.mk_fn_sig( params, s.output(), - s.fn_sig_kind.set_safe(safety.is_safe()).set_abi(ExternAbi::Rust), + s.fn_sig_kind.set_safety(safety).set_abi(ExternAbi::Rust), ) }) } @@ -2471,7 +2431,12 @@ impl<'tcx> TyCtxt<'tcx> { // IntoIterator` instead of `I: Iterator`, and it doesn't have a slice // variant, because of the need to combine `inputs` and `output`. This // explains the lack of `_from_iter` suffix. - pub fn mk_fn_sig(self, inputs: I, output: I::Item, fn_sig_kind: FnSigKind) -> T::Output + pub fn mk_fn_sig( + self, + inputs: I, + output: I::Item, + fn_sig_kind: FnSigKind<'tcx>, + ) -> T::Output where I: IntoIterator, T: CollectAndApply, ty::FnSig<'tcx>>, @@ -2493,7 +2458,7 @@ impl<'tcx> TyCtxt<'tcx> { I: IntoIterator, T: CollectAndApply, ty::FnSig<'tcx>>, { - self.mk_fn_sig(inputs, output, FnSigKind::default().set_safe(safety.is_safe())) + self.mk_fn_sig(inputs, output, FnSigKind::default().set_safety(safety)) } /// `mk_fn_sig`, but with a safe Rust ABI, and no C-variadic argument. @@ -2502,7 +2467,7 @@ impl<'tcx> TyCtxt<'tcx> { I: IntoIterator, T: CollectAndApply, ty::FnSig<'tcx>>, { - self.mk_fn_sig(inputs, output, FnSigKind::default().set_safe(true)) + self.mk_fn_sig(inputs, output, FnSigKind::default().set_safety(hir::Safety::Safe)) } pub fn mk_poly_existential_predicates_from_iter(self, iter: I) -> T::Output diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 733985c606e22..6de86712c7097 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -2,7 +2,6 @@ use std::{debug_assert_matches, fmt}; -use rustc_abi::ExternAbi; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind}; @@ -10,9 +9,7 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::lang_items::LangItem; use rustc_span::{DUMMY_SP, Span, Symbol}; use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem}; -use rustc_type_ir::{ - CollectAndApply, FnSigKind, Interner, TypeFoldable, Unnormalized, search_graph, -}; +use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, Unnormalized, search_graph}; use crate::dep_graph::{DepKind, DepNodeIndex}; use crate::infer::canonical::CanonicalVarKinds; @@ -92,9 +89,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type AllocId = crate::mir::interpret::AllocId; type Pat = Pattern<'tcx>; type PatList = &'tcx List>; - type FSigKind = FnSigKind; type Safety = hir::Safety; - type Abi = ExternAbi; type Const = ty::Const<'tcx>; type Consts = &'tcx List; diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index d3132d3f65780..79f239580460f 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -751,7 +751,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { if self.tcx().codegen_fn_attrs(def_id).safe_target_features { write!(self, "#[target_features] ")?; sig = sig.map_bound(|mut sig| { - sig.fn_sig_kind = sig.fn_sig_kind.set_safe(true); + sig.fn_sig_kind = sig.fn_sig_kind.set_safety(hir::Safety::Safe); sig }); } diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index f6d5d226683b3..29b784e837954 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -198,7 +198,6 @@ TrivialLiftImpls! { rustc_hir::Safety, rustc_middle::mir::ConstValue, rustc_type_ir::BoundConstness, - rustc_type_ir::FnSigKind, rustc_type_ir::PredicatePolarity, // tidy-alphabetical-end } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 295acc5350831..9462026903eed 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -37,7 +37,7 @@ pub type TypeAndMut<'tcx> = ir::TypeAndMut>; pub type AliasTy<'tcx> = ir::AliasTy>; pub type AliasTyKind<'tcx> = ir::AliasTyKind>; pub type FnSig<'tcx> = ir::FnSig>; -pub type FnSigKind = ir::FnSigKind; +pub type FnSigKind<'tcx> = ir::FnSigKind>; pub type Binder<'tcx, T> = ir::Binder, T>; pub type EarlyBinder<'tcx, T> = ir::EarlyBinder, T>; pub type Unnormalized<'tcx, T> = ir::Unnormalized, T>; diff --git a/compiler/rustc_public/src/unstable/convert/internal.rs b/compiler/rustc_public/src/unstable/convert/internal.rs index 4f3e9f94c599b..11db3bcf8f4df 100644 --- a/compiler/rustc_public/src/unstable/convert/internal.rs +++ b/compiler/rustc_public/src/unstable/convert/internal.rs @@ -310,7 +310,7 @@ impl RustcInternal for FnSig { ) -> Self::T<'tcx> { let fn_sig_kind = rustc_ty::FnSigKind::default() .set_abi(self.abi.internal(tables, tcx)) - .set_safe(self.safety == Safety::Safe) + .set_safety(self.safety.internal(tables, tcx)) .set_c_variadic(self.c_variadic); tcx.lift(rustc_ty::FnSig { inputs_and_output: tcx.mk_type_list(&self.inputs_and_output.internal(tables, tcx)), diff --git a/compiler/rustc_public/src/unstable/convert/stable/ty.rs b/compiler/rustc_public/src/unstable/convert/stable/ty.rs index 9a9576d47efd2..593d0cc0aeb9d 100644 --- a/compiler/rustc_public/src/unstable/convert/stable/ty.rs +++ b/compiler/rustc_public/src/unstable/convert/stable/ty.rs @@ -254,7 +254,7 @@ where // This internal type isn't publicly exposed, because it is an implementation detail. // But it's a public field of FnSig (which has a public mirror type), so allow conversions. -impl<'tcx> Stable<'tcx> for ty::FnSigKind { +impl<'tcx> Stable<'tcx> for ty::FnSigKind<'tcx> { type T = (bool /*c_variadic*/, crate::mir::Safety, crate::ty::Abi); fn stable<'cx>( &self, diff --git a/compiler/rustc_type_ir/src/error.rs b/compiler/rustc_type_ir/src/error.rs index eba4c7c6644ac..15fbd985d9630 100644 --- a/compiler/rustc_type_ir/src/error.rs +++ b/compiler/rustc_type_ir/src/error.rs @@ -1,4 +1,5 @@ use derive_where::derive_where; +use rustc_abi::ExternAbi; use rustc_type_ir_macros::{GenericTypeVisitable, TypeFoldable_Generic, TypeVisitable_Generic}; use crate::solve::NoSolution; @@ -25,7 +26,7 @@ pub enum TypeError { Mismatch, PolarityMismatch(#[type_visitable(ignore)] ExpectedFound), SafetyMismatch(#[type_visitable(ignore)] ExpectedFound), - AbiMismatch(#[type_visitable(ignore)] ExpectedFound), + AbiMismatch(#[type_visitable(ignore)] ExpectedFound), Mutability, ArgumentMutability(usize), TupleSize(ExpectedFound), diff --git a/compiler/rustc_type_ir/src/generic_visit.rs b/compiler/rustc_type_ir/src/generic_visit.rs index 543397ff155b4..6669168443a4a 100644 --- a/compiler/rustc_type_ir/src/generic_visit.rs +++ b/compiler/rustc_type_ir/src/generic_visit.rs @@ -15,6 +15,8 @@ use rustc_index::{Idx, IndexVec}; use smallvec::SmallVec; use thin_vec::ThinVec; +use crate::Interner; + /// This trait is implemented for every type that can be visited, /// providing the skeleton of the traversal. /// @@ -210,4 +212,9 @@ trivial_impls!( rustc_hash::FxBuildHasher, crate::TypeFlags, crate::solve::GoalSource, + rustc_abi::ExternAbi, ); + +impl GenericTypeVisitable for crate::FnSigKind { + fn generic_visit_with(&self, _visitor: &mut V) {} +} diff --git a/compiler/rustc_type_ir/src/inherent.rs b/compiler/rustc_type_ir/src/inherent.rs index a336313e90b8f..c909a6ece2bfe 100644 --- a/compiler/rustc_type_ir/src/inherent.rs +++ b/compiler/rustc_type_ir/src/inherent.rs @@ -205,42 +205,6 @@ pub trait Tys>: fn output(self) -> I::Ty; } -#[rust_analyzer::prefer_underscore_import] -pub trait FSigKind>: Copy + Debug + Hash + Eq { - /// The identity function. - fn fn_sig_kind(self) -> Self; - - /// Create a new FnSigKind with the given ABI, safety, and C-style variadic flag. - fn new(abi: I::Abi, safety: I::Safety, c_variadic: bool) -> Self; - - /// Returns the ABI. - fn abi(self) -> I::Abi; - - /// Returns the safety mode. - fn safety(self) -> I::Safety; - - /// Do the function arguments end with a C-style variadic argument? - fn c_variadic(self) -> bool; -} - -#[rust_analyzer::prefer_underscore_import] -pub trait Abi>: Copy + Debug + Hash + Eq { - /// The identity function. - fn abi(self) -> Self; - - /// The ABI `extern "Rust"`. - fn rust() -> I::Abi; - - /// Whether this ABI is `extern "Rust"`. - fn is_rust(self) -> bool; - - /// Pack the ABI into a small dense integer, so it can be stored as packed `FnSigKind` flags. - fn pack_abi(self) -> u8; - - /// Unpack the ABI from packed `FnSigKind` flags. - fn unpack_abi(abi_index: u8) -> Self; -} - #[rust_analyzer::prefer_underscore_import] pub trait Safety>: Copy + Debug + Hash + Eq { /// The `safe` safety mode. diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index f71f7c7c1ab38..3fddcd82c662d 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -145,9 +145,7 @@ pub trait Interner: + Eq + TypeVisitable + SliceLike; - type FSigKind: FSigKind; type Safety: Safety; - type Abi: Abi; // Kinds of consts type Const: Const; diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 61ac5acca7405..e136257a5b248 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -1,4 +1,5 @@ use std::fmt; +use std::marker::PhantomData; use std::ops::Deref; use derive_where::derive_where; @@ -762,18 +763,31 @@ impl Eq for TypeAndMut {} /// Contains the packed non-type fields of a function signature. // FIXME(splat): add the splatted argument index as a u16 -#[derive(Copy, Clone, PartialEq, Eq, Hash)] +#[derive_where(Copy, Clone, PartialEq, Eq, Hash; I: Interner)] +#[derive(TypeVisitable_Generic, TypeFoldable_Generic)] #[cfg_attr( feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext) )] -pub struct FnSigKind { +pub struct FnSigKind { /// Holds the c_variadic and safety bitflags, and 6 bits for the `ExternAbi` variant and unwind /// flag. + #[type_visitable(ignore)] + #[type_foldable(identity)] flags: u8, + #[type_visitable(ignore)] + #[type_foldable(identity)] + _marker: PhantomData I>, } -impl fmt::Debug for FnSigKind { +impl crate::lift::Lift for FnSigKind { + type Lifted = FnSigKind; + fn lift_to_interner(self, _cx: J) -> Option { + Some(FnSigKind { flags: self.flags, _marker: PhantomData }) + } +} + +impl fmt::Debug for FnSigKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut f = f.debug_tuple("FnSigKind"); @@ -793,7 +807,7 @@ impl fmt::Debug for FnSigKind { } } -impl FnSigKind { +impl FnSigKind { /// Mask for the `ExternAbi` variant, including the unwind flag. const EXTERN_ABI_MASK: u8 = 0b111111; @@ -806,13 +820,21 @@ impl FnSigKind { /// Create a new FnSigKind with the "Rust" ABI, "Unsafe" safety, and no C-style variadic argument. /// To modify these flags, use the `set_*` methods, for readability. // FIXME: use Default instead when that trait is const stable. - pub const fn default() -> Self { - Self { flags: 0 }.set_abi(ExternAbi::Rust).set_safe(false).set_c_variadic(false) + pub fn default() -> Self { + Self { flags: 0, _marker: PhantomData } + .set_abi(ExternAbi::Rust) + .set_safety(I::Safety::unsafe_mode()) + .set_c_variadic(false) + } + + /// Create a new FnSigKind with the given ABI, safety, and C-style variadic flag. + pub fn new(abi: ExternAbi, safety: I::Safety, c_variadic: bool) -> Self { + Self::default().set_abi(abi).set_safety(safety).set_c_variadic(c_variadic) } /// Set the ABI, including the unwind flag. #[must_use = "this method does not modify the receiver"] - pub const fn set_abi(mut self, abi: ExternAbi) -> Self { + pub fn set_abi(mut self, abi: ExternAbi) -> Self { let abi_index = abi.as_packed(); assert!(abi_index <= Self::EXTERN_ABI_MASK); @@ -824,8 +846,8 @@ impl FnSigKind { /// Set the safety flag, `true` is `Safe`. #[must_use = "this method does not modify the receiver"] - pub const fn set_safe(mut self, is_safe: bool) -> Self { - if is_safe { + pub fn set_safety(mut self, safety: I::Safety) -> Self { + if safety.is_safe() { self.flags |= Self::SAFE_FLAG; } else { self.flags &= !Self::SAFE_FLAG; @@ -836,7 +858,7 @@ impl FnSigKind { /// Set the C-style variadic argument flag. #[must_use = "this method does not modify the receiver"] - pub const fn set_c_variadic(mut self, c_variadic: bool) -> Self { + pub fn set_c_variadic(mut self, c_variadic: bool) -> Self { if c_variadic { self.flags |= Self::C_VARIADIC_FLAG; } else { @@ -847,18 +869,23 @@ impl FnSigKind { } /// Get the ABI, including the unwind flag. - pub const fn abi(self) -> ExternAbi { + pub fn abi(self) -> ExternAbi { let abi_index = self.flags & Self::EXTERN_ABI_MASK; ExternAbi::from_packed(abi_index) } /// Get the safety flag. - pub const fn is_safe(self) -> bool { + pub fn is_safe(self) -> bool { self.flags & Self::SAFE_FLAG != 0 } + /// Returns the safety mode. + pub fn safety(self) -> I::Safety { + if self.is_safe() { I::Safety::safe() } else { I::Safety::unsafe_mode() } + } + /// Do the function arguments end with a C-style variadic argument? - pub const fn c_variadic(self) -> bool { + pub fn c_variadic(self) -> bool { self.flags & Self::C_VARIADIC_FLAG != 0 } } @@ -873,7 +900,7 @@ pub struct FnSig { pub inputs_and_output: I::Tys, #[type_visitable(ignore)] #[type_foldable(identity)] - pub fn_sig_kind: I::FSigKind, + pub fn_sig_kind: FnSigKind, } impl Eq for FnSig {} @@ -888,25 +915,18 @@ impl FnSig { } pub fn is_fn_trait_compatible(self) -> bool { - !self.c_variadic() && self.safety().is_safe() && self.abi().is_rust() + !self.c_variadic() && self.safety().is_safe() && self.abi() == ExternAbi::Rust } - pub fn set_safe(self, is_safe: bool) -> Self { - Self { - fn_sig_kind: I::FSigKind::new( - self.abi(), - if is_safe { I::Safety::safe() } else { I::Safety::unsafe_mode() }, - self.c_variadic(), - ), - ..self - } + pub fn set_safety(self, safety: I::Safety) -> Self { + Self { fn_sig_kind: FnSigKind::new(self.abi(), safety, self.c_variadic()), ..self } } pub fn safety(self) -> I::Safety { self.fn_sig_kind.safety() } - pub fn abi(self) -> I::Abi { + pub fn abi(self) -> ExternAbi { self.fn_sig_kind.abi() } @@ -917,7 +937,7 @@ impl FnSig { pub fn dummy() -> Self { Self { inputs_and_output: Default::default(), - fn_sig_kind: I::FSigKind::new(I::Abi::rust(), I::Safety::safe(), false), + fn_sig_kind: FnSigKind::new(ExternAbi::Rust, I::Safety::safe(), false), } } } @@ -943,7 +963,7 @@ impl ty::Binder> { self.map_bound(|fn_sig| fn_sig.output()) } - pub fn fn_sig_kind(self) -> I::FSigKind { + pub fn fn_sig_kind(self) -> FnSigKind { self.skip_binder().fn_sig_kind } @@ -955,7 +975,7 @@ impl ty::Binder> { self.skip_binder().safety() } - pub fn abi(self) -> I::Abi { + pub fn abi(self) -> ExternAbi { self.skip_binder().abi() } @@ -976,7 +996,7 @@ impl fmt::Debug for FnSig { let FnSig { inputs_and_output: _, fn_sig_kind } = sig; write!(f, "{}", fn_sig_kind.safety().prefix_str())?; - if !fn_sig_kind.abi().is_rust() { + if fn_sig_kind.abi() != ExternAbi::Rust { write!(f, "extern \"{:?}\" ", fn_sig_kind.abi())?; } @@ -1131,7 +1151,7 @@ impl ty::Binder> { pub struct FnHeader { #[type_visitable(ignore)] #[type_foldable(identity)] - pub fn_sig_kind: I::FSigKind, + pub fn_sig_kind: FnSigKind, } impl FnHeader { @@ -1143,12 +1163,12 @@ impl FnHeader { self.fn_sig_kind.safety() } - pub fn abi(self) -> I::Abi { + pub fn abi(self) -> ExternAbi { self.fn_sig_kind.abi() } pub fn dummy() -> Self { - Self { fn_sig_kind: I::FSigKind::new(I::Abi::rust(), I::Safety::safe(), false) } + Self { fn_sig_kind: FnSigKind::new(ExternAbi::Rust, I::Safety::safe(), false) } } } diff --git a/compiler/rustc_type_ir/src/ty_kind/closure.rs b/compiler/rustc_type_ir/src/ty_kind/closure.rs index 1ad5ed45e8b10..3b8ed0a15994d 100644 --- a/compiler/rustc_type_ir/src/ty_kind/closure.rs +++ b/compiler/rustc_type_ir/src/ty_kind/closure.rs @@ -9,7 +9,7 @@ use crate::data_structures::DelayedMap; use crate::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable, shift_region}; use crate::inherent::*; use crate::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor}; -use crate::{self as ty, Interner}; +use crate::{self as ty, FnSigKind, Interner}; /// A closure can be modeled as a struct that looks like: /// ```ignore (illustrative) @@ -367,7 +367,7 @@ pub struct CoroutineClosureSignature { /// Always safe, RustCall, non-c-variadic #[type_visitable(ignore)] #[type_foldable(identity)] - pub fn_sig_kind: I::FSigKind, + pub fn_sig_kind: FnSigKind, } impl Eq for CoroutineClosureSignature {} diff --git a/src/tools/miri/src/helpers.rs b/src/tools/miri/src/helpers.rs index 2a6520ffc2ba5..512ea6ab1a2ff 100644 --- a/src/tools/miri/src/helpers.rs +++ b/src/tools/miri/src/helpers.rs @@ -407,7 +407,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let sig = this.tcx.mk_fn_sig( args.iter().map(|a| a.layout.ty), dest.layout.ty, - FnSigKind::default().set_abi(caller_abi).set_safe(true), + FnSigKind::default().set_abi(caller_abi).set_safety(rustc_hir::Safety::Safe), ); let caller_fn_abi = this.fn_abi_of_fn_ptr(ty::Binder::dummy(sig), ty::List::empty())?; diff --git a/src/tools/miri/src/shims/sig.rs b/src/tools/miri/src/shims/sig.rs index ddfde35f47c4d..68b13a6ed58a0 100644 --- a/src/tools/miri/src/shims/sig.rs +++ b/src/tools/miri/src/shims/sig.rs @@ -275,7 +275,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let fn_sig_binder = Binder::dummy(FnSig { inputs_and_output: this.machine.tcx.mk_type_list(&inputs_and_output), // Safety does not matter for the ABI. - fn_sig_kind: FnSigKind::default().set_abi(shim_sig.abi).set_safe(true), + fn_sig_kind: FnSigKind::default() + .set_abi(shim_sig.abi) + .set_safety(rustc_hir::Safety::Safe), }); let callee_fn_abi = this.fn_abi_of_fn_ptr(fn_sig_binder, Default::default())?;