Pre intern the Self parameter type

Use this to simplify the object safety code a bit.
This commit is contained in:
Matthew Jasper 2019-08-12 22:15:12 +01:00
parent 165e460c92
commit 24587d20df
10 changed files with 35 additions and 47 deletions

View file

@ -92,9 +92,8 @@ impl<'tcx> TyCtxt<'tcx> {
-> Vec<ObjectSafetyViolation> -> Vec<ObjectSafetyViolation>
{ {
debug_assert!(self.generics_of(trait_def_id).has_self); debug_assert!(self.generics_of(trait_def_id).has_self);
let self_ty = self.mk_self_type();
let violations = traits::supertrait_def_ids(self, trait_def_id) let violations = traits::supertrait_def_ids(self, trait_def_id)
.filter(|&def_id| self.predicates_reference_self(def_id, self_ty, true)) .filter(|&def_id| self.predicates_reference_self(def_id, true))
.map(|_| ObjectSafetyViolation::SupertraitSelf) .map(|_| ObjectSafetyViolation::SupertraitSelf)
.collect(); .collect();
@ -109,11 +108,10 @@ impl<'tcx> TyCtxt<'tcx> {
-> Vec<ObjectSafetyViolation> -> Vec<ObjectSafetyViolation>
{ {
debug_assert!(self.generics_of(trait_def_id).has_self); debug_assert!(self.generics_of(trait_def_id).has_self);
let self_ty = self.mk_self_type();
debug!("object_safety_violations: {:?}", trait_def_id); debug!("object_safety_violations: {:?}", trait_def_id);
traits::supertrait_def_ids(self, trait_def_id) traits::supertrait_def_ids(self, trait_def_id)
.flat_map(|def_id| self.object_safety_violations_for_trait(def_id, self_ty)) .flat_map(|def_id| self.object_safety_violations_for_trait(def_id))
.collect() .collect()
} }
@ -123,29 +121,24 @@ impl<'tcx> TyCtxt<'tcx> {
/// otherwise ensure that they cannot be used when `Self=Trait`. /// otherwise ensure that they cannot be used when `Self=Trait`.
pub fn is_vtable_safe_method(self, trait_def_id: DefId, method: &ty::AssocItem) -> bool { pub fn is_vtable_safe_method(self, trait_def_id: DefId, method: &ty::AssocItem) -> bool {
debug_assert!(self.generics_of(trait_def_id).has_self); debug_assert!(self.generics_of(trait_def_id).has_self);
let self_ty = self.mk_self_type();
debug!("is_vtable_safe_method({:?}, {:?})", trait_def_id, method); debug!("is_vtable_safe_method({:?}, {:?})", trait_def_id, method);
// Any method that has a `Self : Sized` requisite can't be called. // Any method that has a `Self : Sized` requisite can't be called.
if self.generics_require_sized_self(method.def_id, self_ty) { if self.generics_require_sized_self(method.def_id) {
return false; return false;
} }
match self.virtual_call_violation_for_method(trait_def_id, self_ty, method) { match self.virtual_call_violation_for_method(trait_def_id, method) {
None | Some(MethodViolationCode::WhereClauseReferencesSelf(_)) => true, None | Some(MethodViolationCode::WhereClauseReferencesSelf(_)) => true,
Some(_) => false, Some(_) => false,
} }
} }
fn object_safety_violations_for_trait( fn object_safety_violations_for_trait(self, trait_def_id: DefId) -> Vec<ObjectSafetyViolation> {
self,
trait_def_id: DefId,
self_ty: Ty<'tcx>,
) -> Vec<ObjectSafetyViolation> {
// Check methods for violations. // Check methods for violations.
let mut violations: Vec<_> = self.associated_items(trait_def_id) let mut violations: Vec<_> = self.associated_items(trait_def_id)
.filter(|item| item.kind == ty::AssocKind::Method) .filter(|item| item.kind == ty::AssocKind::Method)
.filter_map(|item| .filter_map(|item|
self.object_safety_violation_for_method(trait_def_id, self_ty, &item) self.object_safety_violation_for_method(trait_def_id, &item)
.map(|code| ObjectSafetyViolation::Method(item.ident.name, code)) .map(|code| ObjectSafetyViolation::Method(item.ident.name, code))
).filter(|violation| { ).filter(|violation| {
if let ObjectSafetyViolation::Method(_, if let ObjectSafetyViolation::Method(_,
@ -167,10 +160,10 @@ impl<'tcx> TyCtxt<'tcx> {
}).collect(); }).collect();
// Check the trait itself. // Check the trait itself.
if self.trait_has_sized_self(trait_def_id, self_ty) { if self.trait_has_sized_self(trait_def_id) {
violations.push(ObjectSafetyViolation::SizedSelf); violations.push(ObjectSafetyViolation::SizedSelf);
} }
if self.predicates_reference_self(trait_def_id, self_ty, false) { if self.predicates_reference_self(trait_def_id, false) {
violations.push(ObjectSafetyViolation::SupertraitSelf); violations.push(ObjectSafetyViolation::SupertraitSelf);
} }
@ -188,7 +181,6 @@ impl<'tcx> TyCtxt<'tcx> {
fn predicates_reference_self( fn predicates_reference_self(
self, self,
trait_def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>,
supertraits_only: bool, supertraits_only: bool,
) -> bool { ) -> bool {
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id)); let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id));
@ -197,6 +189,7 @@ impl<'tcx> TyCtxt<'tcx> {
} else { } else {
self.predicates_of(trait_def_id) self.predicates_of(trait_def_id)
}; };
let self_ty = self.types.self_param;
let has_self_ty = |t: Ty<'tcx>| t.walk().any(|t| t == self_ty); let has_self_ty = |t: Ty<'tcx>| t.walk().any(|t| t == self_ty);
predicates predicates
.predicates .predicates
@ -241,11 +234,11 @@ impl<'tcx> TyCtxt<'tcx> {
}) })
} }
fn trait_has_sized_self(self, trait_def_id: DefId, self_ty: Ty<'tcx>) -> bool { fn trait_has_sized_self(self, trait_def_id: DefId) -> bool {
self.generics_require_sized_self(trait_def_id, self_ty) self.generics_require_sized_self(trait_def_id)
} }
fn generics_require_sized_self(self, def_id: DefId, self_ty: Ty<'tcx>) -> bool { fn generics_require_sized_self(self, def_id: DefId) -> bool {
let sized_def_id = match self.lang_items().sized_trait() { let sized_def_id = match self.lang_items().sized_trait() {
Some(def_id) => def_id, Some(def_id) => def_id,
None => { return false; /* No Sized trait, can't require it! */ } None => { return false; /* No Sized trait, can't require it! */ }
@ -258,7 +251,7 @@ impl<'tcx> TyCtxt<'tcx> {
.any(|predicate| match predicate { .any(|predicate| match predicate {
ty::Predicate::Trait(ref trait_pred) => { ty::Predicate::Trait(ref trait_pred) => {
trait_pred.def_id() == sized_def_id trait_pred.def_id() == sized_def_id
&& trait_pred.skip_binder().self_ty() == self_ty && trait_pred.skip_binder().self_ty().is_param(0)
} }
ty::Predicate::Projection(..) | ty::Predicate::Projection(..) |
ty::Predicate::Subtype(..) | ty::Predicate::Subtype(..) |
@ -278,17 +271,16 @@ impl<'tcx> TyCtxt<'tcx> {
fn object_safety_violation_for_method( fn object_safety_violation_for_method(
self, self,
trait_def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>,
method: &ty::AssocItem, method: &ty::AssocItem,
) -> Option<MethodViolationCode> { ) -> Option<MethodViolationCode> {
debug!("object_safety_violation_for_method({:?}, {:?})", trait_def_id, method); debug!("object_safety_violation_for_method({:?}, {:?})", trait_def_id, method);
// Any method that has a `Self : Sized` requisite is otherwise // Any method that has a `Self : Sized` requisite is otherwise
// exempt from the regulations. // exempt from the regulations.
if self.generics_require_sized_self(method.def_id, self_ty) { if self.generics_require_sized_self(method.def_id) {
return None; return None;
} }
self.virtual_call_violation_for_method(trait_def_id, self_ty, method) self.virtual_call_violation_for_method(trait_def_id, method)
} }
/// Returns `Some(_)` if this method cannot be called on a trait /// Returns `Some(_)` if this method cannot be called on a trait
@ -298,7 +290,6 @@ impl<'tcx> TyCtxt<'tcx> {
fn virtual_call_violation_for_method( fn virtual_call_violation_for_method(
self, self,
trait_def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>,
method: &ty::AssocItem, method: &ty::AssocItem,
) -> Option<MethodViolationCode> { ) -> Option<MethodViolationCode> {
// The method's first parameter must be named `self` // The method's first parameter must be named `self`
@ -309,15 +300,11 @@ impl<'tcx> TyCtxt<'tcx> {
let sig = self.fn_sig(method.def_id); let sig = self.fn_sig(method.def_id);
for input_ty in &sig.skip_binder().inputs()[1..] { for input_ty in &sig.skip_binder().inputs()[1..] {
if self.contains_illegal_self_type_reference(trait_def_id, self_ty, input_ty) { if self.contains_illegal_self_type_reference(trait_def_id, input_ty) {
return Some(MethodViolationCode::ReferencesSelf); return Some(MethodViolationCode::ReferencesSelf);
} }
} }
if self.contains_illegal_self_type_reference( if self.contains_illegal_self_type_reference(trait_def_id, sig.output().skip_binder()) {
trait_def_id,
self_ty,
sig.output().skip_binder(),
) {
return Some(MethodViolationCode::ReferencesSelf); return Some(MethodViolationCode::ReferencesSelf);
} }
@ -336,7 +323,7 @@ impl<'tcx> TyCtxt<'tcx> {
// Do a shallow visit so that `contains_illegal_self_type_reference` // Do a shallow visit so that `contains_illegal_self_type_reference`
// may apply it's custom visiting. // may apply it's custom visiting.
.visit_tys_shallow(|t| { .visit_tys_shallow(|t| {
self.contains_illegal_self_type_reference(trait_def_id, self_ty, t) self.contains_illegal_self_type_reference(trait_def_id, t)
}) { }) {
let span = self.def_span(method.def_id); let span = self.def_span(method.def_id);
return Some(MethodViolationCode::WhereClauseReferencesSelf(span)); return Some(MethodViolationCode::WhereClauseReferencesSelf(span));
@ -351,7 +338,7 @@ impl<'tcx> TyCtxt<'tcx> {
// However, this is already considered object-safe. We allow it as a special case here. // However, this is already considered object-safe. We allow it as a special case here.
// FIXME(mikeyhew) get rid of this `if` statement once `receiver_is_dispatchable` allows // FIXME(mikeyhew) get rid of this `if` statement once `receiver_is_dispatchable` allows
// `Receiver: Unsize<Receiver[Self => dyn Trait]>` // `Receiver: Unsize<Receiver[Self => dyn Trait]>`
if receiver_ty != self_ty { if receiver_ty != self.types.self_param {
if !self.receiver_is_dispatchable(method, receiver_ty) { if !self.receiver_is_dispatchable(method, receiver_ty) {
return Some(MethodViolationCode::UndispatchableReceiver); return Some(MethodViolationCode::UndispatchableReceiver);
} else { } else {
@ -572,7 +559,7 @@ impl<'tcx> TyCtxt<'tcx> {
// Self: Unsize<U> // Self: Unsize<U>
let unsize_predicate = ty::TraitRef { let unsize_predicate = ty::TraitRef {
def_id: unsize_did, def_id: unsize_did,
substs: self.mk_substs_trait(self.mk_self_type(), &[unsized_self_ty.into()]), substs: self.mk_substs_trait(self.types.self_param, &[unsized_self_ty.into()]),
}.to_predicate(); }.to_predicate();
// U: Trait<Arg1, ..., ArgN> // U: Trait<Arg1, ..., ArgN>
@ -628,7 +615,6 @@ impl<'tcx> TyCtxt<'tcx> {
fn contains_illegal_self_type_reference( fn contains_illegal_self_type_reference(
self, self,
trait_def_id: DefId, trait_def_id: DefId,
self_ty: Ty<'tcx>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) -> bool { ) -> bool {
// This is somewhat subtle. In general, we want to forbid // This is somewhat subtle. In general, we want to forbid
@ -672,6 +658,7 @@ impl<'tcx> TyCtxt<'tcx> {
let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None; let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None;
let mut error = false; let mut error = false;
let self_ty = self.types.self_param;
ty.maybe_walk(|ty| { ty.maybe_walk(|ty| {
match ty.sty { match ty.sty {
ty::Param(_) => { ty::Param(_) => {

View file

@ -173,6 +173,7 @@ pub struct CommonTypes<'tcx> {
pub f32: Ty<'tcx>, pub f32: Ty<'tcx>,
pub f64: Ty<'tcx>, pub f64: Ty<'tcx>,
pub never: Ty<'tcx>, pub never: Ty<'tcx>,
pub self_param: Ty<'tcx>,
pub err: Ty<'tcx>, pub err: Ty<'tcx>,
/// Dummy type used for the `Self` of a `TraitRef` created for converting /// Dummy type used for the `Self` of a `TraitRef` created for converting
@ -915,6 +916,10 @@ impl<'tcx> CommonTypes<'tcx> {
u128: mk(Uint(ast::UintTy::U128)), u128: mk(Uint(ast::UintTy::U128)),
f32: mk(Float(ast::FloatTy::F32)), f32: mk(Float(ast::FloatTy::F32)),
f64: mk(Float(ast::FloatTy::F64)), f64: mk(Float(ast::FloatTy::F64)),
self_param: mk(ty::Param(ty::ParamTy {
index: 0,
name: kw::SelfUpper.as_interned_str(),
})),
trait_object_dummy_self: mk(Infer(ty::FreshTy(0))), trait_object_dummy_self: mk(Infer(ty::FreshTy(0))),
} }
@ -2566,10 +2571,6 @@ impl<'tcx> TyCtxt<'tcx> {
}) })
} }
#[inline]
pub fn mk_self_type(self) -> Ty<'tcx> {
self.mk_ty_param(0, kw::SelfUpper.as_interned_str())
}
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> { pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
match param.kind { match param.kind {

View file

@ -710,7 +710,7 @@ fn build_call_shim<'tcx>(
Adjustment::DerefMove => { Adjustment::DerefMove => {
// fn(Self, ...) -> fn(*mut Self, ...) // fn(Self, ...) -> fn(*mut Self, ...)
let arg_ty = local_decls[rcvr_arg].ty; let arg_ty = local_decls[rcvr_arg].ty;
debug_assert!(tcx.generics_of(def_id).has_self && arg_ty == tcx.mk_self_type()); debug_assert!(tcx.generics_of(def_id).has_self && arg_ty == tcx.types.self_param);
local_decls[rcvr_arg].ty = tcx.mk_mut_ptr(arg_ty); local_decls[rcvr_arg].ty = tcx.mk_mut_ptr(arg_ty);
Operand::Move(rcvr_l.deref()) Operand::Move(rcvr_l.deref())

View file

@ -636,7 +636,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let default_needs_object_self = |param: &ty::GenericParamDef| { let default_needs_object_self = |param: &ty::GenericParamDef| {
if let GenericParamDefKind::Type { has_default, .. } = param.kind { if let GenericParamDefKind::Type { has_default, .. } = param.kind {
if is_object && has_default && has_self { if is_object && has_default && has_self {
let self_param = tcx.mk_self_type(); let self_param = tcx.types.self_param;
if tcx.at(span).type_of(param.def_id).walk().any(|ty| ty == self_param) { if tcx.at(span).type_of(param.def_id).walk().any(|ty| ty == self_param) {
// There is no suitable inference default for a type parameter // There is no suitable inference default for a type parameter
// that references self, in an object type. // that references self, in an object type.
@ -2031,7 +2031,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// `Self` in trait or type alias. // `Self` in trait or type alias.
assert_eq!(opt_self_ty, None); assert_eq!(opt_self_ty, None);
self.prohibit_generics(&path.segments); self.prohibit_generics(&path.segments);
tcx.mk_self_type() tcx.types.self_param
} }
Res::SelfTy(_, Some(def_id)) => { Res::SelfTy(_, Some(def_id)) => {
// `Self` in impl (we know the concrete type). // `Self` in impl (we know the concrete type).

View file

@ -518,7 +518,7 @@ fn compare_self_type<'tcx>(
let self_string = |method: &ty::AssocItem| { let self_string = |method: &ty::AssocItem| {
let untransformed_self_ty = match method.container { let untransformed_self_ty = match method.container {
ty::ImplContainer(_) => impl_trait_ref.self_ty(), ty::ImplContainer(_) => impl_trait_ref.self_ty(),
ty::TraitContainer(_) => tcx.mk_self_type() ty::TraitContainer(_) => tcx.types.self_param
}; };
let self_arg_ty = *tcx.fn_sig(method.def_id).input(0).skip_binder(); let self_arg_ty = *tcx.fn_sig(method.def_id).input(0).skip_binder();
let param_env = ty::ParamEnv::reveal_all(); let param_env = ty::ParamEnv::reveal_all();

View file

@ -191,7 +191,7 @@ fn check_associated_item(
let item = fcx.tcx.associated_item(fcx.tcx.hir().local_def_id(item_id)); let item = fcx.tcx.associated_item(fcx.tcx.hir().local_def_id(item_id));
let (mut implied_bounds, self_ty) = match item.container { let (mut implied_bounds, self_ty) = match item.container {
ty::TraitContainer(_) => (vec![], fcx.tcx.mk_self_type()), ty::TraitContainer(_) => (vec![], fcx.tcx.types.self_param),
ty::ImplContainer(def_id) => (fcx.impl_implied_bounds(def_id, span), ty::ImplContainer(def_id) => (fcx.impl_implied_bounds(def_id, span),
fcx.tcx.type_of(def_id)) fcx.tcx.type_of(def_id))
}; };

View file

@ -713,7 +713,7 @@ fn super_predicates_of(
let icx = ItemCtxt::new(tcx, trait_def_id); let icx = ItemCtxt::new(tcx, trait_def_id);
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`. // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
let self_param_ty = tcx.mk_self_type(); let self_param_ty = tcx.types.self_param;
let superbounds1 = AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No, let superbounds1 = AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No,
item.span); item.span);

View file

@ -211,7 +211,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
substs, substs,
required_predicates, required_predicates,
explicit_map, explicit_map,
Some(tcx.mk_self_type()), Some(tcx.types.self_param),
); );
} }
} }

View file

@ -2303,7 +2303,7 @@ impl Clean<Item> for ty::AssocItem {
ty::ImplContainer(def_id) => { ty::ImplContainer(def_id) => {
cx.tcx.type_of(def_id) cx.tcx.type_of(def_id)
} }
ty::TraitContainer(_) => cx.tcx.mk_self_type() ty::TraitContainer(_) => cx.tcx.types.self_param,
}; };
let self_arg_ty = *sig.input(0).skip_binder(); let self_arg_ty = *sig.input(0).skip_binder();
if self_arg_ty == self_ty { if self_arg_ty == self_ty {

View file

@ -150,7 +150,7 @@ fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId,
} }
let predicates = cx.tcx.super_predicates_of(child); let predicates = cx.tcx.super_predicates_of(child);
debug_assert!(cx.tcx.generics_of(child).has_self); debug_assert!(cx.tcx.generics_of(child).has_self);
let self_ty = cx.tcx.mk_self_type(); let self_ty = cx.tcx.types.self_param;
predicates.predicates.iter().filter_map(|(pred, _)| { predicates.predicates.iter().filter_map(|(pred, _)| {
if let ty::Predicate::Trait(ref pred) = *pred { if let ty::Predicate::Trait(ref pred) = *pred {
if pred.skip_binder().trait_ref.self_ty() == self_ty { if pred.skip_binder().trait_ref.self_ty() == self_ty {