Skip to content

Commit 24587d2

Browse files
committed
Pre intern the Self parameter type
Use this to simplify the object safety code a bit.
1 parent 165e460 commit 24587d2

File tree

10 files changed

+35
-47
lines changed

10 files changed

+35
-47
lines changed

src/librustc/traits/object_safety.rs

+21-34
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ impl<'tcx> TyCtxt<'tcx> {
9292
-> Vec<ObjectSafetyViolation>
9393
{
9494
debug_assert!(self.generics_of(trait_def_id).has_self);
95-
let self_ty = self.mk_self_type();
9695
let violations = traits::supertrait_def_ids(self, trait_def_id)
97-
.filter(|&def_id| self.predicates_reference_self(def_id, self_ty, true))
96+
.filter(|&def_id| self.predicates_reference_self(def_id, true))
9897
.map(|_| ObjectSafetyViolation::SupertraitSelf)
9998
.collect();
10099

@@ -109,11 +108,10 @@ impl<'tcx> TyCtxt<'tcx> {
109108
-> Vec<ObjectSafetyViolation>
110109
{
111110
debug_assert!(self.generics_of(trait_def_id).has_self);
112-
let self_ty = self.mk_self_type();
113111
debug!("object_safety_violations: {:?}", trait_def_id);
114112

115113
traits::supertrait_def_ids(self, trait_def_id)
116-
.flat_map(|def_id| self.object_safety_violations_for_trait(def_id, self_ty))
114+
.flat_map(|def_id| self.object_safety_violations_for_trait(def_id))
117115
.collect()
118116
}
119117

@@ -123,29 +121,24 @@ impl<'tcx> TyCtxt<'tcx> {
123121
/// otherwise ensure that they cannot be used when `Self=Trait`.
124122
pub fn is_vtable_safe_method(self, trait_def_id: DefId, method: &ty::AssocItem) -> bool {
125123
debug_assert!(self.generics_of(trait_def_id).has_self);
126-
let self_ty = self.mk_self_type();
127124
debug!("is_vtable_safe_method({:?}, {:?})", trait_def_id, method);
128125
// Any method that has a `Self : Sized` requisite can't be called.
129-
if self.generics_require_sized_self(method.def_id, self_ty) {
126+
if self.generics_require_sized_self(method.def_id) {
130127
return false;
131128
}
132129

133-
match self.virtual_call_violation_for_method(trait_def_id, self_ty, method) {
130+
match self.virtual_call_violation_for_method(trait_def_id, method) {
134131
None | Some(MethodViolationCode::WhereClauseReferencesSelf(_)) => true,
135132
Some(_) => false,
136133
}
137134
}
138135

139-
fn object_safety_violations_for_trait(
140-
self,
141-
trait_def_id: DefId,
142-
self_ty: Ty<'tcx>,
143-
) -> Vec<ObjectSafetyViolation> {
136+
fn object_safety_violations_for_trait(self, trait_def_id: DefId) -> Vec<ObjectSafetyViolation> {
144137
// Check methods for violations.
145138
let mut violations: Vec<_> = self.associated_items(trait_def_id)
146139
.filter(|item| item.kind == ty::AssocKind::Method)
147140
.filter_map(|item|
148-
self.object_safety_violation_for_method(trait_def_id, self_ty, &item)
141+
self.object_safety_violation_for_method(trait_def_id, &item)
149142
.map(|code| ObjectSafetyViolation::Method(item.ident.name, code))
150143
).filter(|violation| {
151144
if let ObjectSafetyViolation::Method(_,
@@ -167,10 +160,10 @@ impl<'tcx> TyCtxt<'tcx> {
167160
}).collect();
168161

169162
// Check the trait itself.
170-
if self.trait_has_sized_self(trait_def_id, self_ty) {
163+
if self.trait_has_sized_self(trait_def_id) {
171164
violations.push(ObjectSafetyViolation::SizedSelf);
172165
}
173-
if self.predicates_reference_self(trait_def_id, self_ty, false) {
166+
if self.predicates_reference_self(trait_def_id, false) {
174167
violations.push(ObjectSafetyViolation::SupertraitSelf);
175168
}
176169

@@ -188,7 +181,6 @@ impl<'tcx> TyCtxt<'tcx> {
188181
fn predicates_reference_self(
189182
self,
190183
trait_def_id: DefId,
191-
self_ty: Ty<'tcx>,
192184
supertraits_only: bool,
193185
) -> bool {
194186
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id));
@@ -197,6 +189,7 @@ impl<'tcx> TyCtxt<'tcx> {
197189
} else {
198190
self.predicates_of(trait_def_id)
199191
};
192+
let self_ty = self.types.self_param;
200193
let has_self_ty = |t: Ty<'tcx>| t.walk().any(|t| t == self_ty);
201194
predicates
202195
.predicates
@@ -241,11 +234,11 @@ impl<'tcx> TyCtxt<'tcx> {
241234
})
242235
}
243236

244-
fn trait_has_sized_self(self, trait_def_id: DefId, self_ty: Ty<'tcx>) -> bool {
245-
self.generics_require_sized_self(trait_def_id, self_ty)
237+
fn trait_has_sized_self(self, trait_def_id: DefId) -> bool {
238+
self.generics_require_sized_self(trait_def_id)
246239
}
247240

248-
fn generics_require_sized_self(self, def_id: DefId, self_ty: Ty<'tcx>) -> bool {
241+
fn generics_require_sized_self(self, def_id: DefId) -> bool {
249242
let sized_def_id = match self.lang_items().sized_trait() {
250243
Some(def_id) => def_id,
251244
None => { return false; /* No Sized trait, can't require it! */ }
@@ -258,7 +251,7 @@ impl<'tcx> TyCtxt<'tcx> {
258251
.any(|predicate| match predicate {
259252
ty::Predicate::Trait(ref trait_pred) => {
260253
trait_pred.def_id() == sized_def_id
261-
&& trait_pred.skip_binder().self_ty() == self_ty
254+
&& trait_pred.skip_binder().self_ty().is_param(0)
262255
}
263256
ty::Predicate::Projection(..) |
264257
ty::Predicate::Subtype(..) |
@@ -278,17 +271,16 @@ impl<'tcx> TyCtxt<'tcx> {
278271
fn object_safety_violation_for_method(
279272
self,
280273
trait_def_id: DefId,
281-
self_ty: Ty<'tcx>,
282274
method: &ty::AssocItem,
283275
) -> Option<MethodViolationCode> {
284276
debug!("object_safety_violation_for_method({:?}, {:?})", trait_def_id, method);
285277
// Any method that has a `Self : Sized` requisite is otherwise
286278
// exempt from the regulations.
287-
if self.generics_require_sized_self(method.def_id, self_ty) {
279+
if self.generics_require_sized_self(method.def_id) {
288280
return None;
289281
}
290282

291-
self.virtual_call_violation_for_method(trait_def_id, self_ty, method)
283+
self.virtual_call_violation_for_method(trait_def_id, method)
292284
}
293285

294286
/// Returns `Some(_)` if this method cannot be called on a trait
@@ -298,7 +290,6 @@ impl<'tcx> TyCtxt<'tcx> {
298290
fn virtual_call_violation_for_method(
299291
self,
300292
trait_def_id: DefId,
301-
self_ty: Ty<'tcx>,
302293
method: &ty::AssocItem,
303294
) -> Option<MethodViolationCode> {
304295
// The method's first parameter must be named `self`
@@ -309,15 +300,11 @@ impl<'tcx> TyCtxt<'tcx> {
309300
let sig = self.fn_sig(method.def_id);
310301

311302
for input_ty in &sig.skip_binder().inputs()[1..] {
312-
if self.contains_illegal_self_type_reference(trait_def_id, self_ty, input_ty) {
303+
if self.contains_illegal_self_type_reference(trait_def_id, input_ty) {
313304
return Some(MethodViolationCode::ReferencesSelf);
314305
}
315306
}
316-
if self.contains_illegal_self_type_reference(
317-
trait_def_id,
318-
self_ty,
319-
sig.output().skip_binder(),
320-
) {
307+
if self.contains_illegal_self_type_reference(trait_def_id, sig.output().skip_binder()) {
321308
return Some(MethodViolationCode::ReferencesSelf);
322309
}
323310

@@ -336,7 +323,7 @@ impl<'tcx> TyCtxt<'tcx> {
336323
// Do a shallow visit so that `contains_illegal_self_type_reference`
337324
// may apply it's custom visiting.
338325
.visit_tys_shallow(|t| {
339-
self.contains_illegal_self_type_reference(trait_def_id, self_ty, t)
326+
self.contains_illegal_self_type_reference(trait_def_id, t)
340327
}) {
341328
let span = self.def_span(method.def_id);
342329
return Some(MethodViolationCode::WhereClauseReferencesSelf(span));
@@ -351,7 +338,7 @@ impl<'tcx> TyCtxt<'tcx> {
351338
// However, this is already considered object-safe. We allow it as a special case here.
352339
// FIXME(mikeyhew) get rid of this `if` statement once `receiver_is_dispatchable` allows
353340
// `Receiver: Unsize<Receiver[Self => dyn Trait]>`
354-
if receiver_ty != self_ty {
341+
if receiver_ty != self.types.self_param {
355342
if !self.receiver_is_dispatchable(method, receiver_ty) {
356343
return Some(MethodViolationCode::UndispatchableReceiver);
357344
} else {
@@ -572,7 +559,7 @@ impl<'tcx> TyCtxt<'tcx> {
572559
// Self: Unsize<U>
573560
let unsize_predicate = ty::TraitRef {
574561
def_id: unsize_did,
575-
substs: self.mk_substs_trait(self.mk_self_type(), &[unsized_self_ty.into()]),
562+
substs: self.mk_substs_trait(self.types.self_param, &[unsized_self_ty.into()]),
576563
}.to_predicate();
577564

578565
// U: Trait<Arg1, ..., ArgN>
@@ -628,7 +615,6 @@ impl<'tcx> TyCtxt<'tcx> {
628615
fn contains_illegal_self_type_reference(
629616
self,
630617
trait_def_id: DefId,
631-
self_ty: Ty<'tcx>,
632618
ty: Ty<'tcx>,
633619
) -> bool {
634620
// This is somewhat subtle. In general, we want to forbid
@@ -672,6 +658,7 @@ impl<'tcx> TyCtxt<'tcx> {
672658

673659
let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None;
674660
let mut error = false;
661+
let self_ty = self.types.self_param;
675662
ty.maybe_walk(|ty| {
676663
match ty.sty {
677664
ty::Param(_) => {

src/librustc/ty/context.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pub struct CommonTypes<'tcx> {
173173
pub f32: Ty<'tcx>,
174174
pub f64: Ty<'tcx>,
175175
pub never: Ty<'tcx>,
176+
pub self_param: Ty<'tcx>,
176177
pub err: Ty<'tcx>,
177178

178179
/// Dummy type used for the `Self` of a `TraitRef` created for converting
@@ -915,6 +916,10 @@ impl<'tcx> CommonTypes<'tcx> {
915916
u128: mk(Uint(ast::UintTy::U128)),
916917
f32: mk(Float(ast::FloatTy::F32)),
917918
f64: mk(Float(ast::FloatTy::F64)),
919+
self_param: mk(ty::Param(ty::ParamTy {
920+
index: 0,
921+
name: kw::SelfUpper.as_interned_str(),
922+
})),
918923

919924
trait_object_dummy_self: mk(Infer(ty::FreshTy(0))),
920925
}
@@ -2566,10 +2571,6 @@ impl<'tcx> TyCtxt<'tcx> {
25662571
})
25672572
}
25682573

2569-
#[inline]
2570-
pub fn mk_self_type(self) -> Ty<'tcx> {
2571-
self.mk_ty_param(0, kw::SelfUpper.as_interned_str())
2572-
}
25732574

25742575
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
25752576
match param.kind {

src/librustc_mir/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ fn build_call_shim<'tcx>(
710710
Adjustment::DerefMove => {
711711
// fn(Self, ...) -> fn(*mut Self, ...)
712712
let arg_ty = local_decls[rcvr_arg].ty;
713-
debug_assert!(tcx.generics_of(def_id).has_self && arg_ty == tcx.mk_self_type());
713+
debug_assert!(tcx.generics_of(def_id).has_self && arg_ty == tcx.types.self_param);
714714
local_decls[rcvr_arg].ty = tcx.mk_mut_ptr(arg_ty);
715715

716716
Operand::Move(rcvr_l.deref())

src/librustc_typeck/astconv.rs

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

src/librustc_typeck/check/compare_method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ fn compare_self_type<'tcx>(
518518
let self_string = |method: &ty::AssocItem| {
519519
let untransformed_self_ty = match method.container {
520520
ty::ImplContainer(_) => impl_trait_ref.self_ty(),
521-
ty::TraitContainer(_) => tcx.mk_self_type()
521+
ty::TraitContainer(_) => tcx.types.self_param
522522
};
523523
let self_arg_ty = *tcx.fn_sig(method.def_id).input(0).skip_binder();
524524
let param_env = ty::ParamEnv::reveal_all();

src/librustc_typeck/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn check_associated_item(
191191
let item = fcx.tcx.associated_item(fcx.tcx.hir().local_def_id(item_id));
192192

193193
let (mut implied_bounds, self_ty) = match item.container {
194-
ty::TraitContainer(_) => (vec![], fcx.tcx.mk_self_type()),
194+
ty::TraitContainer(_) => (vec![], fcx.tcx.types.self_param),
195195
ty::ImplContainer(def_id) => (fcx.impl_implied_bounds(def_id, span),
196196
fcx.tcx.type_of(def_id))
197197
};

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ fn super_predicates_of(
713713
let icx = ItemCtxt::new(tcx, trait_def_id);
714714

715715
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
716-
let self_param_ty = tcx.mk_self_type();
716+
let self_param_ty = tcx.types.self_param;
717717
let superbounds1 = AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No,
718718
item.span);
719719

src/librustc_typeck/outlives/implicit_infer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
211211
substs,
212212
required_predicates,
213213
explicit_map,
214-
Some(tcx.mk_self_type()),
214+
Some(tcx.types.self_param),
215215
);
216216
}
217217
}

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ impl Clean<Item> for ty::AssocItem {
23032303
ty::ImplContainer(def_id) => {
23042304
cx.tcx.type_of(def_id)
23052305
}
2306-
ty::TraitContainer(_) => cx.tcx.mk_self_type()
2306+
ty::TraitContainer(_) => cx.tcx.types.self_param,
23072307
};
23082308
let self_arg_ty = *sig.input(0).skip_binder();
23092309
if self_arg_ty == self_ty {

src/librustdoc/clean/simplify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId,
150150
}
151151
let predicates = cx.tcx.super_predicates_of(child);
152152
debug_assert!(cx.tcx.generics_of(child).has_self);
153-
let self_ty = cx.tcx.mk_self_type();
153+
let self_ty = cx.tcx.types.self_param;
154154
predicates.predicates.iter().filter_map(|(pred, _)| {
155155
if let ty::Predicate::Trait(ref pred) = *pred {
156156
if pred.skip_binder().trait_ref.self_ty() == self_ty {

0 commit comments

Comments
 (0)