Skip to content

Commit 14f42a7

Browse files
committed
Auto merge of #55032 - oli-obk:the_early_unwrap_gets_the_panic, r=Mark-Simulacrum
Check the invariant for `principal` inside the method r? @Mark-Simulacrum
2 parents 68df433 + 585490d commit 14f42a7

File tree

26 files changed

+147
-189
lines changed

26 files changed

+147
-189
lines changed

src/librustc/traits/coherence.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,7 @@ fn fundamental_ty(tcx: TyCtxt<'_, '_, '_>, ty: Ty<'_>) -> bool {
418418
match ty.sty {
419419
ty::Ref(..) => true,
420420
ty::Adt(def, _) => def.is_fundamental(),
421-
ty::Dynamic(ref data, ..) => {
422-
data.principal().map_or(false, |p| tcx.has_attr(p.def_id(), "fundamental"))
423-
}
421+
ty::Dynamic(ref data, ..) => tcx.has_attr(data.principal().def_id(), "fundamental"),
424422
_ => false
425423
}
426424
}
@@ -467,11 +465,7 @@ fn ty_is_local_constructor(ty: Ty<'_>, in_crate: InCrate) -> bool {
467465
ty::Adt(def, _) => def_id_is_local(def.did, in_crate),
468466
ty::Foreign(did) => def_id_is_local(did, in_crate),
469467

470-
ty::Dynamic(ref tt, ..) => {
471-
tt.principal().map_or(false, |p|
472-
def_id_is_local(p.def_id(), in_crate)
473-
)
474-
}
468+
ty::Dynamic(ref tt, ..) => def_id_is_local(tt.principal().def_id(), in_crate),
475469

476470
ty::Error => true,
477471

src/librustc/traits/select.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -2088,10 +2088,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20882088
return;
20892089
}
20902090

2091-
match data.principal() {
2092-
Some(p) => p.with_self_ty(this.tcx(), self_ty),
2093-
None => return,
2094-
}
2091+
data.principal().with_self_ty(this.tcx(), self_ty)
20952092
}
20962093
ty::Infer(ty::TyVar(_)) => {
20972094
debug!("assemble_candidates_from_object_ty: ambiguous");
@@ -2183,15 +2180,10 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21832180
//
21842181
// We always upcast when we can because of reason
21852182
// #2 (region bounds).
2186-
match (data_a.principal(), data_b.principal()) {
2187-
(Some(a), Some(b)) => {
2188-
a.def_id() == b.def_id()
2189-
&& data_b.auto_traits()
2190-
// All of a's auto traits need to be in b's auto traits.
2191-
.all(|b| data_a.auto_traits().any(|a| a == b))
2192-
}
2193-
_ => false,
2194-
}
2183+
data_a.principal().def_id() == data_b.principal().def_id()
2184+
&& data_b.auto_traits()
2185+
// All of a's auto traits need to be in b's auto traits.
2186+
.all(|b| data_a.auto_traits().any(|a| a == b))
21952187
}
21962188

21972189
// T -> Trait.
@@ -2981,7 +2973,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
29812973
.shallow_resolve(*obligation.self_ty().skip_binder());
29822974
let poly_trait_ref = match self_ty.sty {
29832975
ty::Dynamic(ref data, ..) => {
2984-
data.principal().unwrap().with_self_ty(self.tcx(), self_ty)
2976+
data.principal().with_self_ty(self.tcx(), self_ty)
29852977
}
29862978
_ => span_bug!(obligation.cause.span, "object candidate with non-object"),
29872979
};
@@ -3244,10 +3236,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
32443236
(&ty::Dynamic(ref data_a, r_a), &ty::Dynamic(ref data_b, r_b)) => {
32453237
// See assemble_candidates_for_unsizing for more info.
32463238
let existential_predicates = data_a.map_bound(|data_a| {
3247-
let principal = data_a.principal();
3248-
let iter = principal
3249-
.into_iter()
3250-
.map(ty::ExistentialPredicate::Trait)
3239+
let iter = iter::once(ty::ExistentialPredicate::Trait(data_a.principal()))
32513240
.chain(
32523241
data_a
32533242
.projection_bounds()
@@ -3285,7 +3274,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
32853274
// T -> Trait.
32863275
(_, &ty::Dynamic(ref data, r)) => {
32873276
let mut object_dids = data.auto_traits()
3288-
.chain(data.principal().map(|p| p.def_id()));
3277+
.chain(iter::once(data.principal().def_id()));
32893278
if let Some(did) = object_dids.find(|did| !tcx.is_object_safe(*did)) {
32903279
return Err(TraitNotObjectSafe(did));
32913280
}

src/librustc/ty/error.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
208208
ty::FnDef(..) => "fn item".into(),
209209
ty::FnPtr(_) => "fn pointer".into(),
210210
ty::Dynamic(ref inner, ..) => {
211-
inner.principal().map_or_else(|| "trait".into(),
212-
|p| format!("trait {}", tcx.item_path_str(p.def_id())).into())
211+
format!("trait {}", tcx.item_path_str(inner.principal().def_id())).into()
213212
}
214213
ty::Closure(..) => "closure".into(),
215214
ty::Generator(..) => "generator".into(),

src/librustc/ty/fast_reject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
7878
ty::Array(..) | ty::Slice(_) => Some(ArraySimplifiedType),
7979
ty::RawPtr(_) => Some(PtrSimplifiedType),
8080
ty::Dynamic(ref trait_info, ..) => {
81-
trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
81+
Some(TraitSimplifiedType(trait_info.principal().def_id()))
8282
}
8383
ty::Ref(_, ty, _) => {
8484
// since we introduce auto-refs during method lookup, we

src/librustc/ty/item_path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
436436
match ty.sty {
437437
ty::Adt(adt_def, _) => Some(adt_def.did),
438438

439-
ty::Dynamic(data, ..) => data.principal().map(|p| p.def_id()),
439+
ty::Dynamic(data, ..) => Some(data.principal().def_id()),
440440

441441
ty::Array(subty, _) |
442442
ty::Slice(subty) => characteristic_def_id_of_type(subty),

src/librustc/ty/sty.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,10 @@ impl<'a, 'gcx, 'tcx> Binder<ExistentialPredicate<'tcx>> {
559559
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx List<ExistentialPredicate<'tcx>> {}
560560

561561
impl<'tcx> List<ExistentialPredicate<'tcx>> {
562-
pub fn principal(&self) -> Option<ExistentialTraitRef<'tcx>> {
563-
match self.get(0) {
564-
Some(&ExistentialPredicate::Trait(tr)) => Some(tr),
565-
_ => None,
562+
pub fn principal(&self) -> ExistentialTraitRef<'tcx> {
563+
match self[0] {
564+
ExistentialPredicate::Trait(tr) => tr,
565+
other => bug!("first predicate is {:?}", other),
566566
}
567567
}
568568

@@ -589,8 +589,8 @@ impl<'tcx> List<ExistentialPredicate<'tcx>> {
589589
}
590590

591591
impl<'tcx> Binder<&'tcx List<ExistentialPredicate<'tcx>>> {
592-
pub fn principal(&self) -> Option<PolyExistentialTraitRef<'tcx>> {
593-
self.skip_binder().principal().map(Binder::bind)
592+
pub fn principal(&self) -> PolyExistentialTraitRef<'tcx> {
593+
Binder::bind(self.skip_binder().principal())
594594
}
595595

596596
#[inline]
@@ -1825,9 +1825,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
18251825
}
18261826
Dynamic(ref obj, region) => {
18271827
let mut v = vec![region];
1828-
if let Some(p) = obj.principal() {
1829-
v.extend(p.skip_binder().substs.regions());
1830-
}
1828+
v.extend(obj.principal().skip_binder().substs.regions());
18311829
v
18321830
}
18331831
Adt(_, substs) | Opaque(_, substs) => {

src/librustc/ty/wf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> {
387387

388388
let cause = self.cause(traits::MiscObligation);
389389
let component_traits =
390-
data.auto_traits().chain(data.principal().map(|p| p.def_id()));
390+
data.auto_traits().chain(once(data.principal().def_id()));
391391
self.out.extend(
392392
component_traits.map(|did| traits::Obligation::new(
393393
cause.clone(),

src/librustc/util/ppaux.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -621,16 +621,16 @@ define_print! {
621621
// Use a type that can't appear in defaults of type parameters.
622622
let dummy_self = tcx.mk_infer(ty::FreshTy(0));
623623

624-
if let Some(p) = self.principal() {
625-
let principal = tcx.lift(&p).expect("could not lift TraitRef for printing")
626-
.with_self_ty(tcx, dummy_self);
627-
let projections = self.projection_bounds().map(|p| {
628-
tcx.lift(&p)
629-
.expect("could not lift projection for printing")
630-
.with_self_ty(tcx, dummy_self)
631-
}).collect::<Vec<_>>();
632-
cx.parameterized(f, principal.substs, principal.def_id, &projections)?;
633-
}
624+
let principal = tcx
625+
.lift(&self.principal())
626+
.expect("could not lift TraitRef for printing")
627+
.with_self_ty(tcx, dummy_self);
628+
let projections = self.projection_bounds().map(|p| {
629+
tcx.lift(&p)
630+
.expect("could not lift projection for printing")
631+
.with_self_ty(tcx, dummy_self)
632+
}).collect::<Vec<_>>();
633+
cx.parameterized(f, principal.substs, principal.def_id, &projections)?;
634634

635635
// Builtin bounds.
636636
for did in self.auto_traits() {

src/librustc_codegen_llvm/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct CodegenCx<'a, 'tcx: 'a> {
5959
/// Cache instances of monomorphic and polymorphic items
6060
pub instances: RefCell<FxHashMap<Instance<'tcx>, &'a Value>>,
6161
/// Cache generated vtables
62-
pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>),
62+
pub vtables: RefCell<FxHashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>),
6363
&'a Value>>,
6464
/// Cache of constant strings,
6565
pub const_cstr_cache: RefCell<FxHashMap<LocalInternedString, &'a Value>>,

src/librustc_codegen_llvm/debuginfo/metadata.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,7 @@ fn trait_pointer_metadata(
435435
// But it does not describe the trait's methods.
436436

437437
let containing_scope = match trait_type.sty {
438-
ty::Dynamic(ref data, ..) => if let Some(principal) = data.principal() {
439-
let def_id = principal.def_id();
440-
Some(get_namespace_for_item(cx, def_id))
441-
} else {
442-
NO_SCOPE_METADATA
443-
},
438+
ty::Dynamic(ref data, ..) => Some(get_namespace_for_item(cx, data.principal().def_id())),
444439
_ => {
445440
bug!("debuginfo: Unexpected trait-object type in \
446441
trait_pointer_metadata(): {:?}",

src/librustc_codegen_llvm/debuginfo/type_names.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,12 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
116116
}
117117
},
118118
ty::Dynamic(ref trait_data, ..) => {
119-
if let Some(principal) = trait_data.principal() {
120-
let principal = cx.tcx.normalize_erasing_late_bound_regions(
121-
ty::ParamEnv::reveal_all(),
122-
&principal,
123-
);
124-
push_item_name(cx, principal.def_id, false, output);
125-
push_type_params(cx, principal.substs, output);
126-
}
119+
let principal = cx.tcx.normalize_erasing_late_bound_regions(
120+
ty::ParamEnv::reveal_all(),
121+
&trait_data.principal(),
122+
);
123+
push_item_name(cx, principal.def_id, false, output);
124+
push_type_params(cx, principal.substs, output);
127125
},
128126
ty::FnDef(..) | ty::FnPtr(_) => {
129127
let sig = t.fn_sig(cx.tcx);

src/librustc_codegen_llvm/meth.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a, 'tcx> VirtualIndex {
7272
pub fn get_vtable(
7373
cx: &CodegenCx<'ll, 'tcx>,
7474
ty: Ty<'tcx>,
75-
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
75+
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
7676
) -> &'ll Value {
7777
let tcx = cx.tcx;
7878

@@ -86,23 +86,19 @@ pub fn get_vtable(
8686
// Not in the cache. Build it.
8787
let nullptr = C_null(Type::i8p(cx));
8888

89+
let methods = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
90+
let methods = methods.iter().cloned().map(|opt_mth| {
91+
opt_mth.map_or(nullptr, |(def_id, substs)| {
92+
callee::resolve_and_get_fn(cx, def_id, substs)
93+
})
94+
});
95+
8996
let (size, align) = cx.size_and_align_of(ty);
90-
let mut components: Vec<_> = [
97+
let components: Vec<_> = [
9198
callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
9299
C_usize(cx, size.bytes()),
93100
C_usize(cx, align.abi())
94-
].iter().cloned().collect();
95-
96-
if let Some(trait_ref) = trait_ref {
97-
let trait_ref = trait_ref.with_self_ty(tcx, ty);
98-
let methods = tcx.vtable_methods(trait_ref);
99-
let methods = methods.iter().cloned().map(|opt_mth| {
100-
opt_mth.map_or(nullptr, |(def_id, substs)| {
101-
callee::resolve_and_get_fn(cx, def_id, substs)
102-
})
103-
});
104-
components.extend(methods);
105-
}
101+
].iter().cloned().chain(methods).collect();
106102

107103
let vtable_const = C_struct(cx, &components, false);
108104
let align = cx.data_layout().pointer_align;

src/librustc_mir/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
327327
}
328328
(_, &ty::Dynamic(ref data, _)) => {
329329
// Initial cast from sized to dyn trait
330-
let trait_ref = data.principal().unwrap().with_self_ty(
330+
let trait_ref = data.principal().with_self_ty(
331331
*self.tcx,
332332
src_pointee_ty,
333333
);

src/librustc_mir/monomorphize/collector.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -907,22 +907,20 @@ fn create_mono_items_for_vtable_methods<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
907907
!impl_ty.needs_subst() && !impl_ty.has_escaping_regions());
908908

909909
if let ty::Dynamic(ref trait_ty, ..) = trait_ty.sty {
910-
if let Some(principal) = trait_ty.principal() {
911-
let poly_trait_ref = principal.with_self_ty(tcx, impl_ty);
912-
assert!(!poly_trait_ref.has_escaping_regions());
913-
914-
// Walk all methods of the trait, including those of its supertraits
915-
let methods = tcx.vtable_methods(poly_trait_ref);
916-
let methods = methods.iter().cloned().filter_map(|method| method)
917-
.map(|(def_id, substs)| ty::Instance::resolve(
918-
tcx,
919-
ty::ParamEnv::reveal_all(),
920-
def_id,
921-
substs).unwrap())
922-
.filter(|&instance| should_monomorphize_locally(tcx, &instance))
923-
.map(|instance| create_fn_mono_item(instance));
924-
output.extend(methods);
925-
}
910+
let poly_trait_ref = trait_ty.principal().with_self_ty(tcx, impl_ty);
911+
assert!(!poly_trait_ref.has_escaping_regions());
912+
913+
// Walk all methods of the trait, including those of its supertraits
914+
let methods = tcx.vtable_methods(poly_trait_ref);
915+
let methods = methods.iter().cloned().filter_map(|method| method)
916+
.map(|(def_id, substs)| ty::Instance::resolve(
917+
tcx,
918+
ty::ParamEnv::reveal_all(),
919+
def_id,
920+
substs).unwrap())
921+
.filter(|&instance| should_monomorphize_locally(tcx, &instance))
922+
.map(|instance| create_fn_mono_item(instance));
923+
output.extend(methods);
926924
// Also add the destructor
927925
visit_drop_use(tcx, impl_ty, false, output);
928926
}

src/librustc_mir/monomorphize/item.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,13 @@ impl<'a, 'tcx> DefPathBasedNames<'a, 'tcx> {
320320
output.push(']');
321321
},
322322
ty::Dynamic(ref trait_data, ..) => {
323-
if let Some(principal) = trait_data.principal() {
324-
self.push_def_path(principal.def_id(), output);
325-
self.push_type_params(principal.skip_binder().substs,
326-
trait_data.projection_bounds(),
327-
output);
328-
}
323+
let principal = trait_data.principal();
324+
self.push_def_path(principal.def_id(), output);
325+
self.push_type_params(
326+
principal.skip_binder().substs,
327+
trait_data.projection_bounds(),
328+
output,
329+
);
329330
},
330331
ty::Foreign(did) => self.push_def_path(did, output),
331332
ty::FnDef(..) |

src/librustc_privacy/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ impl<'a, 'tcx> EmbargoVisitor<'a, 'tcx> {
9393
let ty_def_id = match self.tcx.type_of(item_def_id).sty {
9494
ty::Adt(adt, _) => adt.did,
9595
ty::Foreign(did) => did,
96-
ty::Dynamic(ref obj, ..) if obj.principal().is_some() =>
97-
obj.principal().unwrap().def_id(),
96+
ty::Dynamic(ref obj, ..) => obj.principal().def_id(),
9897
ty::Projection(ref proj) => proj.trait_ref(self.tcx).def_id,
9998
_ => return Some(AccessLevel::Public)
10099
};
@@ -484,7 +483,7 @@ impl<'b, 'a, 'tcx> TypeVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'b
484483
let ty_def_id = match ty.sty {
485484
ty::Adt(adt, _) => Some(adt.did),
486485
ty::Foreign(did) => Some(did),
487-
ty::Dynamic(ref obj, ..) => obj.principal().map(|p| p.def_id()),
486+
ty::Dynamic(ref obj, ..) => Some(obj.principal().def_id()),
488487
ty::Projection(ref proj) => Some(proj.item_def_id),
489488
ty::FnDef(def_id, ..) |
490489
ty::Closure(def_id, ..) |
@@ -1456,7 +1455,7 @@ impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'
14561455
let ty_def_id = match ty.sty {
14571456
ty::Adt(adt, _) => Some(adt.did),
14581457
ty::Foreign(did) => Some(did),
1459-
ty::Dynamic(ref obj, ..) => obj.principal().map(|p| p.def_id()),
1458+
ty::Dynamic(ref obj, ..) => Some(obj.principal().def_id()),
14601459
ty::Projection(ref proj) => {
14611460
if self.required_visibility == ty::Visibility::Invisible {
14621461
// Conservatively approximate the whole type alias as public without

src/librustc_typeck/check/cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ enum PointerKind<'tcx> {
7373
/// No metadata attached, ie pointer to sized type or foreign type
7474
Thin,
7575
/// A trait object
76-
Vtable(Option<DefId>),
76+
Vtable(DefId),
7777
/// Slice
7878
Length,
7979
/// The unsize info of this projection
@@ -105,7 +105,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
105105
Ok(match t.sty {
106106
ty::Slice(_) | ty::Str => Some(PointerKind::Length),
107107
ty::Dynamic(ref tty, ..) =>
108-
Some(PointerKind::Vtable(tty.principal().map(|p| p.def_id()))),
108+
Some(PointerKind::Vtable(tty.principal().def_id())),
109109
ty::Adt(def, substs) if def.is_struct() => {
110110
match def.non_enum_variant().fields.last() {
111111
None => Some(PointerKind::Thin),

src/librustc_typeck/check/closure.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
198198
self.deduce_sig_from_projection(None, &pb)
199199
})
200200
.next();
201-
let kind = object_type
202-
.principal()
203-
.and_then(|p| self.tcx.lang_items().fn_trait_kind(p.def_id()));
201+
let kind = self.tcx.lang_items().fn_trait_kind(object_type.principal().def_id());
204202
(sig, kind)
205203
}
206204
ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),

0 commit comments

Comments
 (0)