Skip to content

Commit 6af3638

Browse files
committed
Prevent the creation of TraitRef without dedicated methods
1 parent a5cd3bd commit 6af3638

File tree

9 files changed

+44
-47
lines changed

9 files changed

+44
-47
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
570570
assoc_item,
571571
assoc_item,
572572
default.span,
573-
ty::TraitRef { def_id: it.owner_id.to_def_id(), substs: trait_substs },
573+
tcx.mk_trait_ref(it.owner_id.to_def_id(), trait_substs),
574574
);
575575
}
576576
_ => {}

compiler/rustc_hir_analysis/src/variance/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
160160
// instead of requiring an additional `+ 'a`.
161161
match pred.kind().skip_binder() {
162162
ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate {
163-
trait_ref: ty::TraitRef { def_id: _, substs },
163+
trait_ref: ty::TraitRef { def_id: _, substs, .. },
164164
constness: _,
165165
polarity: _,
166166
})) => {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir_analysis::astconv::AstConv;
1313
use rustc_infer::infer;
1414
use rustc_infer::traits::{self, StatementAsExpression};
1515
use rustc_middle::lint::in_external_macro;
16-
use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty};
16+
use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, Ty};
1717
use rustc_session::errors::ExprParenthesesNeeded;
1818
use rustc_span::symbol::sym;
1919
use rustc_span::Span;
@@ -1277,17 +1277,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12771277
// Check that we're in fact trying to clone into the expected type
12781278
&& self.can_coerce(*pointee_ty, expected_ty)
12791279
// And the expected type doesn't implement `Clone`
1280-
&& !self.predicate_must_hold_considering_regions(&traits::Obligation {
1281-
cause: traits::ObligationCause::dummy(),
1282-
param_env: self.param_env,
1283-
recursion_depth: 0,
1284-
predicate: ty::Binder::dummy(ty::TraitRef {
1285-
def_id: clone_trait_did,
1286-
substs: self.tcx.mk_substs([expected_ty.into()].iter()),
1287-
})
1288-
.without_const()
1289-
.to_predicate(self.tcx),
1290-
})
1280+
&& !self.predicate_must_hold_considering_regions(&traits::Obligation::new(
1281+
self.tcx,
1282+
traits::ObligationCause::dummy(),
1283+
self.param_env,
1284+
ty::Binder::dummy(self.tcx.mk_trait_ref(
1285+
clone_trait_did,
1286+
[expected_ty],
1287+
)),
1288+
))
12911289
{
12921290
diag.span_note(
12931291
callee_expr.span,

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,11 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
226226
false
227227
};
228228

229-
let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef {
230-
def_id: trait_def_id,
231-
substs: expected_substs,
232-
});
233-
let actual_trait_ref = self
229+
let expected_trait_ref = self
234230
.cx
235-
.resolve_vars_if_possible(ty::TraitRef { def_id: trait_def_id, substs: actual_substs });
231+
.resolve_vars_if_possible(self.cx.tcx.mk_trait_ref(trait_def_id, expected_substs));
232+
let actual_trait_ref =
233+
self.cx.resolve_vars_if_possible(self.cx.tcx.mk_trait_ref(trait_def_id, actual_substs));
236234

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

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
322322
Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
323323
} else {
324324
let substs = relate_substs(relation, a.substs, b.substs)?;
325-
Ok(ty::TraitRef { def_id: a.def_id, substs })
325+
Ok(relation.tcx().mk_trait_ref(a.def_id, substs))
326326
}
327327
}
328328
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,14 @@ impl<'tcx> List<ty::PolyExistentialPredicate<'tcx>> {
816816
pub struct TraitRef<'tcx> {
817817
pub def_id: DefId,
818818
pub substs: SubstsRef<'tcx>,
819+
/// This field exists to prevent the creation of `TraitRef` without
820+
/// calling [TyCtxt::mk_trait_ref].
821+
_use_mk_trait_ref_instead: (),
819822
}
820823

821824
impl<'tcx> TraitRef<'tcx> {
822825
pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> TraitRef<'tcx> {
823-
TraitRef { def_id, substs }
826+
TraitRef { def_id, substs, _use_mk_trait_ref_instead: () }
824827
}
825828

826829
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
@@ -836,6 +839,7 @@ impl<'tcx> TraitRef<'tcx> {
836839
ty::Binder::dummy(TraitRef {
837840
def_id,
838841
substs: InternalSubsts::identity_for_item(tcx, def_id),
842+
_use_mk_trait_ref_instead: (),
839843
})
840844
}
841845

@@ -850,7 +854,11 @@ impl<'tcx> TraitRef<'tcx> {
850854
substs: SubstsRef<'tcx>,
851855
) -> ty::TraitRef<'tcx> {
852856
let defs = tcx.generics_of(trait_id);
853-
ty::TraitRef { def_id: trait_id, substs: tcx.intern_substs(&substs[..defs.params.len()]) }
857+
ty::TraitRef {
858+
def_id: trait_id,
859+
substs: tcx.intern_substs(&substs[..defs.params.len()]),
860+
_use_mk_trait_ref_instead: (),
861+
}
854862
}
855863
}
856864

@@ -1194,10 +1202,7 @@ impl<'tcx> AliasTy<'tcx> {
11941202
let trait_def_id = self.trait_def_id(tcx);
11951203
let trait_generics = tcx.generics_of(trait_def_id);
11961204
(
1197-
ty::TraitRef {
1198-
def_id: trait_def_id,
1199-
substs: self.substs.truncate_to(tcx, trait_generics),
1200-
},
1205+
tcx.mk_trait_ref(trait_def_id, self.substs.truncate_to(tcx, trait_generics)),
12011206
&self.substs[trait_generics.count()..],
12021207
)
12031208
}
@@ -1211,7 +1216,7 @@ impl<'tcx> AliasTy<'tcx> {
12111216
/// as well.
12121217
pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> {
12131218
let def_id = self.trait_def_id(tcx);
1214-
ty::TraitRef { def_id, substs: self.substs.truncate_to(tcx, tcx.generics_of(def_id)) }
1219+
tcx.mk_trait_ref(def_id, self.substs.truncate_to(tcx, tcx.generics_of(def_id)))
12151220
}
12161221

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

compiler/rustc_privacy/src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,25 @@ where
110110
V: DefIdVisitor<'tcx> + ?Sized,
111111
{
112112
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> ControlFlow<V::BreakTy> {
113-
let TraitRef { def_id, substs } = trait_ref;
113+
let TraitRef { def_id, substs, .. } = trait_ref;
114114
self.def_id_visitor.visit_def_id(def_id, "trait", &trait_ref.print_only_trait_path())?;
115115
if self.def_id_visitor.shallow() { ControlFlow::CONTINUE } else { substs.visit_with(self) }
116116
}
117117

118118
fn visit_projection_ty(&mut self, projection: ty::AliasTy<'tcx>) -> ControlFlow<V::BreakTy> {
119119
let tcx = self.def_id_visitor.tcx();
120-
let (trait_ref, assoc_substs) = if tcx.def_kind(projection.def_id)
121-
!= DefKind::ImplTraitPlaceholder
122-
{
123-
projection.trait_ref_and_own_substs(tcx)
124-
} else {
125-
// HACK(RPITIT): Remove this when RPITITs are lowered to regular assoc tys
126-
let def_id = tcx.impl_trait_in_trait_parent(projection.def_id);
127-
let trait_generics = tcx.generics_of(def_id);
128-
(
129-
ty::TraitRef { def_id, substs: projection.substs.truncate_to(tcx, trait_generics) },
130-
&projection.substs[trait_generics.count()..],
131-
)
132-
};
120+
let (trait_ref, assoc_substs) =
121+
if tcx.def_kind(projection.def_id) != DefKind::ImplTraitPlaceholder {
122+
projection.trait_ref_and_own_substs(tcx)
123+
} else {
124+
// HACK(RPITIT): Remove this when RPITITs are lowered to regular assoc tys
125+
let def_id = tcx.impl_trait_in_trait_parent(projection.def_id);
126+
let trait_generics = tcx.generics_of(def_id);
127+
(
128+
tcx.mk_trait_ref(def_id, projection.substs.truncate_to(tcx, trait_generics)),
129+
&projection.substs[trait_generics.count()..],
130+
)
131+
};
133132
self.visit_trait(trait_ref)?;
134133
if self.def_id_visitor.shallow() {
135134
ControlFlow::CONTINUE

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,7 @@ fn receiver_is_dispatchable<'tcx>(
703703
}
704704
});
705705

706-
ty::Binder::dummy(ty::TraitRef { def_id: unsize_did, substs })
707-
.without_const()
708-
.to_predicate(tcx)
706+
ty::Binder::dummy(tcx.mk_trait_ref(unsize_did, substs)).to_predicate(tcx)
709707
};
710708

711709
let caller_bounds: Vec<Predicate<'tcx>> =

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,7 @@ fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>(
13091309
let trait_substs =
13101310
obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id));
13111311
// FIXME(named-returns): Binders
1312-
let trait_predicate =
1313-
ty::Binder::dummy(ty::TraitRef { def_id: trait_def_id, substs: trait_substs });
1312+
let trait_predicate = ty::Binder::dummy(tcx.mk_trait_ref(trait_def_id, trait_substs));
13141313

13151314
let _ = selcx.infcx.commit_if_ok(|_| {
13161315
match selcx.select(&obligation.with(tcx, trait_predicate)) {

0 commit comments

Comments
 (0)