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

Lines changed: 2 additions & 8 deletions
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

Lines changed: 8 additions & 19 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 10 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 6 deletions
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(): {:?}",

0 commit comments

Comments
 (0)