Skip to content

Commit 064f439

Browse files
Add maybe_const field to ty::TraitRef
1 parent a4262dd commit 064f439

File tree

24 files changed

+115
-109
lines changed

24 files changed

+115
-109
lines changed

src/librustc/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,10 @@ impl NiceRegionError<'me, 'tcx> {
215215
false
216216
};
217217

218-
let expected_trait_ref = self.infcx.resolve_vars_if_possible(&ty::TraitRef {
219-
def_id: trait_def_id,
220-
substs: expected_substs,
221-
});
222-
let actual_trait_ref = self.infcx.resolve_vars_if_possible(&ty::TraitRef {
223-
def_id: trait_def_id,
224-
substs: actual_substs,
225-
});
218+
let expected_trait_ref =
219+
self.infcx.resolve_vars_if_possible(&ty::TraitRef::new(trait_def_id, expected_substs));
220+
let actual_trait_ref =
221+
self.infcx.resolve_vars_if_possible(&ty::TraitRef::new(trait_def_id, actual_substs));
226222

227223
// Search the expected and actual trait references to see (a)
228224
// whether the sub/sup placeholders appear in them (sometimes

src/librustc/traits/auto_trait.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
8383
) -> AutoTraitResult<A> {
8484
let tcx = self.tcx;
8585

86-
let trait_ref = ty::TraitRef { def_id: trait_did, substs: tcx.mk_substs_trait(ty, &[]) };
86+
let trait_ref = ty::TraitRef::new(trait_did, tcx.mk_substs_trait(ty, &[]));
8787

8888
let trait_pred = ty::Binder::bind(trait_ref);
8989

@@ -271,10 +271,7 @@ impl AutoTraitFinder<'tcx> {
271271
let mut already_visited = FxHashSet::default();
272272
let mut predicates = VecDeque::new();
273273
predicates.push_back(ty::Binder::bind(ty::TraitPredicate {
274-
trait_ref: ty::TraitRef {
275-
def_id: trait_did,
276-
substs: infcx.tcx.mk_substs_trait(ty, &[]),
277-
},
274+
trait_ref: ty::TraitRef::new(trait_did, infcx.tcx.mk_substs_trait(ty, &[])),
278275
}));
279276

280277
let mut computed_preds: FxHashSet<_> = param_env.caller_bounds.iter().cloned().collect();

src/librustc/traits/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub trait TraitEngine<'tcx>: 'tcx {
2626
def_id: DefId,
2727
cause: ObligationCause<'tcx>,
2828
) {
29-
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
29+
let trait_ref = ty::TraitRef::new(def_id, infcx.tcx.mk_substs_trait(ty, &[]));
3030
self.register_predicate_obligation(
3131
infcx,
3232
Obligation {

src/librustc/traits/error_reporting.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13401340
cause: ObligationCause<'tcx>,
13411341
param_env: ty::ParamEnv<'tcx>,
13421342
) -> PredicateObligation<'tcx> {
1343-
let new_trait_ref =
1344-
ty::TraitRef { def_id, substs: self.tcx.mk_substs_trait(output_ty, &[]) };
1343+
let new_trait_ref = ty::TraitRef::new(def_id, self.tcx.mk_substs_trait(output_ty, &[]));
13451344
Obligation::new(cause, param_env, new_trait_ref.to_predicate())
13461345
}
13471346

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub struct PendingPredicateObligation<'tcx> {
7272

7373
// `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
7474
#[cfg(target_arch = "x86_64")]
75-
static_assert_size!(PendingPredicateObligation<'_>, 136);
75+
static_assert_size!(PendingPredicateObligation<'_>, 144);
7676

7777
impl<'a, 'tcx> FulfillmentContext<'tcx> {
7878
/// Creates a new fulfillment context.

src/librustc/traits/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
137137

138138
// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
139139
#[cfg(target_arch = "x86_64")]
140-
static_assert_size!(PredicateObligation<'_>, 112);
140+
static_assert_size!(PredicateObligation<'_>, 120);
141141

142142
/// The reason why we incurred this obligation; used for error reporting.
143143
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -320,7 +320,7 @@ pub struct AssocTypeBoundData {
320320

321321
// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
322322
#[cfg(target_arch = "x86_64")]
323-
static_assert_size!(ObligationCauseCode<'_>, 32);
323+
static_assert_size!(ObligationCauseCode<'_>, 40);
324324

325325
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
326326
pub struct MatchExpressionArmCause<'tcx> {
@@ -727,7 +727,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
727727
infcx.tcx.def_path_str(def_id)
728728
);
729729

730-
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
730+
let trait_ref = ty::TraitRef::new(def_id, infcx.tcx.mk_substs_trait(ty, &[]));
731731
let obligation = Obligation {
732732
param_env,
733733
cause: ObligationCause::misc(span, hir::DUMMY_HIR_ID),

src/librustc/traits/object_safety.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,10 @@ fn receiver_is_dispatchable<'tcx>(
579579
let mut param_env = tcx.param_env(method.def_id);
580580

581581
// Self: Unsize<U>
582-
let unsize_predicate = ty::TraitRef {
583-
def_id: unsize_did,
584-
substs: tcx.mk_substs_trait(tcx.types.self_param, &[unsized_self_ty.into()]),
585-
}
582+
let unsize_predicate = ty::TraitRef::new(
583+
unsize_did,
584+
tcx.mk_substs_trait(tcx.types.self_param, &[unsized_self_ty.into()]),
585+
)
586586
.to_predicate();
587587

588588
// U: Trait<Arg1, ..., ArgN>
@@ -596,7 +596,7 @@ fn receiver_is_dispatchable<'tcx>(
596596
}
597597
});
598598

599-
ty::TraitRef { def_id: unsize_did, substs }.to_predicate()
599+
ty::TraitRef::new(unsize_did, substs).to_predicate()
600600
};
601601

602602
let caller_bounds: Vec<Predicate<'tcx>> = param_env
@@ -614,10 +614,10 @@ fn receiver_is_dispatchable<'tcx>(
614614

615615
// Receiver: DispatchFromDyn<Receiver[Self => U]>
616616
let obligation = {
617-
let predicate = ty::TraitRef {
618-
def_id: dispatch_from_dyn_did,
619-
substs: tcx.mk_substs_trait(receiver_ty, &[unsized_receiver_ty.into()]),
620-
}
617+
let predicate = ty::TraitRef::new(
618+
dispatch_from_dyn_did,
619+
tcx.mk_substs_trait(receiver_ty, &[unsized_receiver_ty.into()]),
620+
)
621621
.to_predicate();
622622

623623
Obligation::new(ObligationCause::dummy(), param_env, predicate)

src/librustc/traits/util.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,7 @@ pub fn predicate_for_trait_def(
557557
self_ty: Ty<'tcx>,
558558
params: &[GenericArg<'tcx>],
559559
) -> PredicateObligation<'tcx> {
560-
let trait_ref =
561-
ty::TraitRef { def_id: trait_def_id, substs: tcx.mk_substs_trait(self_ty, params) };
560+
let trait_ref = ty::TraitRef::new(trait_def_id, tcx.mk_substs_trait(self_ty, params));
562561
predicate_for_trait_ref(cause, param_env, trait_ref, recursion_depth)
563562
}
564563

@@ -629,10 +628,8 @@ pub fn closure_trait_ref_and_return_type(
629628
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
630629
TupleArgumentsFlag::Yes => tcx.intern_tup(sig.skip_binder().inputs()),
631630
};
632-
let trait_ref = ty::TraitRef {
633-
def_id: fn_trait_def_id,
634-
substs: tcx.mk_substs_trait(self_ty, &[arguments_tuple.into()]),
635-
};
631+
let trait_ref =
632+
ty::TraitRef::new(fn_trait_def_id, tcx.mk_substs_trait(self_ty, &[arguments_tuple.into()]));
636633
ty::Binder::bind((trait_ref, sig.skip_binder().output()))
637634
}
638635

@@ -642,8 +639,7 @@ pub fn generator_trait_ref_and_outputs(
642639
self_ty: Ty<'tcx>,
643640
sig: ty::PolyGenSig<'tcx>,
644641
) -> ty::Binder<(ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
645-
let trait_ref =
646-
ty::TraitRef { def_id: fn_trait_def_id, substs: tcx.mk_substs_trait(self_ty, &[]) };
642+
let trait_ref = ty::TraitRef::new(fn_trait_def_id, tcx.mk_substs_trait(self_ty, &[]));
647643
ty::Binder::bind((trait_ref, sig.skip_binder().yield_ty, sig.skip_binder().return_ty))
648644
}
649645

src/librustc/traits/wf.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
374374
fn require_sized(&mut self, subty: Ty<'tcx>, cause: traits::ObligationCauseCode<'tcx>) {
375375
if !subty.has_escaping_bound_vars() {
376376
let cause = self.cause(cause);
377-
let trait_ref = ty::TraitRef {
378-
def_id: self.infcx.tcx.require_lang_item(lang_items::SizedTraitLangItem, None),
379-
substs: self.infcx.tcx.mk_substs_trait(subty, &[]),
380-
};
377+
let trait_ref = ty::TraitRef::new(
378+
self.infcx.tcx.require_lang_item(lang_items::SizedTraitLangItem, None),
379+
self.infcx.tcx.mk_substs_trait(subty, &[]),
380+
);
381381
self.out.push(traits::Obligation::new(cause, self.param_env, trait_ref.to_predicate()));
382382
}
383383
}

src/librustc/ty/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,11 +2467,9 @@ impl<'tcx> AdtDef {
24672467
Some(x) => x,
24682468
_ => return vec![ty],
24692469
};
2470-
let sized_predicate = Binder::dummy(TraitRef {
2471-
def_id: sized_trait,
2472-
substs: tcx.mk_substs_trait(ty, &[]),
2473-
})
2474-
.to_predicate();
2470+
let sized_predicate =
2471+
Binder::dummy(TraitRef::new(sized_trait, tcx.mk_substs_trait(ty, &[])))
2472+
.to_predicate();
24752473
let predicates = tcx.predicates_of(self.did).predicates;
24762474
if predicates.iter().any(|(p, _)| *p == sized_predicate) {
24772475
vec![]

src/librustc/ty/print/pretty.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,13 @@ define_print_and_forward_display! {
17491749
}
17501750

17511751
ty::TraitRef<'tcx> {
1752-
p!(write("<{} as {}>", self.self_ty(), self.print_only_trait_path()))
1752+
p!(
1753+
write("<{} as {}{}>",
1754+
self.self_ty(),
1755+
if self.maybe_const { "?const " } else { "" },
1756+
self.print_only_trait_path()
1757+
)
1758+
)
17531759
}
17541760

17551761
TraitRefPrintOnlyTraitPath<'tcx> {

src/librustc/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
287287
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
288288
} else {
289289
let substs = relate_substs(relation, None, a.substs, b.substs)?;
290-
Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
290+
Ok(ty::TraitRef::new(a.def_id, substs))
291291
}
292292
}
293293
}

src/librustc/ty/structural_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
392392
impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
393393
type Lifted = ty::TraitRef<'tcx>;
394394
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
395-
tcx.lift(&self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
395+
tcx.lift(&self.substs).map(|substs| ty::TraitRef::new(self.def_id, substs))
396396
}
397397
}
398398

src/librustc/ty/sty.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,7 @@ impl<'tcx> Binder<ExistentialPredicate<'tcx>> {
670670
ty::Predicate::Projection(Binder(p.with_self_ty(tcx, self_ty)))
671671
}
672672
ExistentialPredicate::AutoTrait(did) => {
673-
let trait_ref =
674-
Binder(ty::TraitRef { def_id: did, substs: tcx.mk_substs_trait(self_ty, &[]) });
673+
let trait_ref = Binder(ty::TraitRef::new(did, tcx.mk_substs_trait(self_ty, &[])));
675674
trait_ref.to_predicate()
676675
}
677676
}
@@ -784,17 +783,32 @@ impl<'tcx> Binder<&'tcx List<ExistentialPredicate<'tcx>>> {
784783
pub struct TraitRef<'tcx> {
785784
pub def_id: DefId,
786785
pub substs: SubstsRef<'tcx>,
786+
787+
/// Whether the reference to the trait is prefixed by `?const`.
788+
//
789+
// FIXME(ecstaticmorse): This field causes `TraitRef` to increase in size by an entire machine
790+
// word, despite it never being used during trait solving. Try implementing the checks in
791+
// `qualify_min_const_fn` using `hir::TraitRef` instead.
792+
pub maybe_const: bool,
787793
}
788794

789795
impl<'tcx> TraitRef<'tcx> {
790796
pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> TraitRef<'tcx> {
791-
TraitRef { def_id, substs }
797+
TraitRef::new_maybe_const(def_id, substs, false)
798+
}
799+
800+
pub fn new_maybe_const(
801+
def_id: DefId,
802+
substs: SubstsRef<'tcx>,
803+
maybe_const: bool,
804+
) -> TraitRef<'tcx> {
805+
TraitRef { def_id, substs, maybe_const }
792806
}
793807

794808
/// Returns a `TraitRef` of the form `P0: Foo<P1..Pn>` where `Pi`
795809
/// are the parameters defined on trait.
796810
pub fn identity(tcx: TyCtxt<'tcx>, def_id: DefId) -> TraitRef<'tcx> {
797-
TraitRef { def_id, substs: InternalSubsts::identity_for_item(tcx, def_id) }
811+
TraitRef::new(def_id, InternalSubsts::identity_for_item(tcx, def_id))
798812
}
799813

800814
#[inline]
@@ -817,7 +831,7 @@ impl<'tcx> TraitRef<'tcx> {
817831
) -> ty::TraitRef<'tcx> {
818832
let defs = tcx.generics_of(trait_id);
819833

820-
ty::TraitRef { def_id: trait_id, substs: tcx.intern_substs(&substs[..defs.params.len()]) }
834+
ty::TraitRef::new(trait_id, tcx.intern_substs(&substs[..defs.params.len()]))
821835
}
822836
}
823837

@@ -882,7 +896,7 @@ impl<'tcx> ExistentialTraitRef<'tcx> {
882896
// otherwise the escaping vars would be captured by the binder
883897
// debug_assert!(!self_ty.has_escaping_bound_vars());
884898

885-
ty::TraitRef { def_id: self.def_id, substs: tcx.mk_substs_trait(self_ty, self.substs) }
899+
ty::TraitRef::new(self.def_id, tcx.mk_substs_trait(self_ty, self.substs))
886900
}
887901
}
888902

@@ -1058,7 +1072,7 @@ impl<'tcx> ProjectionTy<'tcx> {
10581072
/// then this function would return a `T: Iterator` trait reference.
10591073
pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> {
10601074
let def_id = tcx.associated_item(self.item_def_id).container.id();
1061-
ty::TraitRef { def_id, substs: self.substs.truncate_to(tcx, tcx.generics_of(def_id)) }
1075+
ty::TraitRef::new(def_id, self.substs.truncate_to(tcx, tcx.generics_of(def_id)))
10621076
}
10631077

10641078
pub fn self_ty(&self) -> Ty<'tcx> {

src/librustc_mir/borrow_check/type_check/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
470470
if place.projection.is_empty() {
471471
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
472472
let tcx = self.tcx();
473-
let trait_ref = ty::TraitRef {
474-
def_id: tcx.lang_items().copy_trait().unwrap(),
475-
substs: tcx.mk_substs_trait(place_ty.ty, &[]),
476-
};
473+
let trait_ref = ty::TraitRef::new(
474+
tcx.lang_items().copy_trait().unwrap(),
475+
tcx.mk_substs_trait(place_ty.ty, &[]),
476+
);
477477

478478
// To have a `Copy` operand, the type `T` of the
479479
// value must be `Copy`. Note that we prove that `T: Copy`,
@@ -1391,10 +1391,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13911391

13921392
self.check_rvalue(body, rv, location);
13931393
if !self.tcx().features().unsized_locals {
1394-
let trait_ref = ty::TraitRef {
1395-
def_id: tcx.lang_items().sized_trait().unwrap(),
1396-
substs: tcx.mk_substs_trait(place_ty, &[]),
1397-
};
1394+
let trait_ref = ty::TraitRef::new(
1395+
tcx.lang_items().sized_trait().unwrap(),
1396+
tcx.mk_substs_trait(place_ty, &[]),
1397+
);
13981398
self.prove_trait_ref(
13991399
trait_ref,
14001400
location.to_locations(),
@@ -1954,10 +1954,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19541954
self.ensure_place_sized(ty, span);
19551955
}
19561956

1957-
let trait_ref = ty::TraitRef {
1958-
def_id: tcx.lang_items().sized_trait().unwrap(),
1959-
substs: tcx.mk_substs_trait(ty, &[]),
1960-
};
1957+
let trait_ref = ty::TraitRef::new(
1958+
tcx.lang_items().sized_trait().unwrap(),
1959+
tcx.mk_substs_trait(ty, &[]),
1960+
);
19611961

19621962
self.prove_trait_ref(
19631963
trait_ref,
@@ -2054,10 +2054,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20542054

20552055
CastKind::Pointer(PointerCast::Unsize) => {
20562056
let &ty = ty;
2057-
let trait_ref = ty::TraitRef {
2058-
def_id: tcx.lang_items().coerce_unsized_trait().unwrap(),
2059-
substs: tcx.mk_substs_trait(op.ty(*body, tcx), &[ty.into()]),
2060-
};
2057+
let trait_ref = ty::TraitRef::new(
2058+
tcx.lang_items().coerce_unsized_trait().unwrap(),
2059+
tcx.mk_substs_trait(op.ty(*body, tcx), &[ty.into()]),
2060+
);
20612061

20622062
self.prove_trait_ref(
20632063
trait_ref,

src/librustc_mir/monomorphize/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ pub fn custom_coerce_unsize_info<'tcx>(
1212
) -> CustomCoerceUnsized {
1313
let def_id = tcx.lang_items().coerce_unsized_trait().unwrap();
1414

15-
let trait_ref = ty::Binder::bind(ty::TraitRef {
16-
def_id: def_id,
17-
substs: tcx.mk_substs_trait(source_ty, &[target_ty.into()]),
18-
});
15+
let trait_ref = ty::Binder::bind(ty::TraitRef::new(
16+
def_id,
17+
tcx.mk_substs_trait(source_ty, &[target_ty.into()]),
18+
));
1919

2020
match tcx.codegen_fulfill_obligation((ty::ParamEnv::reveal_all(), trait_ref)) {
2121
traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {

src/librustc_privacy/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ where
8484
V: DefIdVisitor<'tcx> + ?Sized,
8585
{
8686
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> bool {
87-
let TraitRef { def_id, substs } = trait_ref;
87+
let TraitRef { def_id, substs, maybe_const: _ } = trait_ref;
8888
self.def_id_visitor.visit_def_id(def_id, "trait", &trait_ref.print_only_trait_path())
8989
|| (!self.def_id_visitor.shallow() && substs.visit_with(self))
9090
}

0 commit comments

Comments
 (0)