Skip to content

Commit 2cf6bcf

Browse files
committed
elaborate sized hierarchy to remove hacks
1 parent fba5977 commit 2cf6bcf

16 files changed

Lines changed: 37 additions & 245 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ where
9292
// and `assumption`'s trait refs directly inside this function in
9393
// order to prevent unsoundness (see below).
9494

95-
let goal_lang_item = ecx.cx().as_trait_lang_item(goal.predicate.trait_def_id(ecx.cx()));
96-
Self::fast_reject_assumption(ecx, goal, goal_lang_item, assumption)?;
95+
Self::fast_reject_assumption(ecx, goal, assumption)?;
9796

9897
ecx.probe_trait_candidate(source).enter(|ecx| {
9998
let cx = ecx.cx();
@@ -227,8 +226,7 @@ where
227226
assumption: I::Clause,
228227
then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResultOrRerunNonErased<I>,
229228
) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
230-
let goal_lang_item = ecx.cx().as_trait_lang_item(goal.predicate.trait_def_id(ecx.cx()));
231-
Self::fast_reject_assumption(ecx, goal, goal_lang_item, assumption)?;
229+
Self::fast_reject_assumption(ecx, goal, assumption)?;
232230

233231
ecx.probe_trait_candidate(source)
234232
.enter(|ecx| Self::match_assumption(ecx, goal, assumption, then))
@@ -239,7 +237,6 @@ where
239237
fn fast_reject_assumption(
240238
ecx: &mut EvalCtxt<'_, D>,
241239
goal: Goal<I, Self>,
242-
goal_lang_item: Option<SolverTraitLangItem>,
243240
assumption: I::Clause,
244241
) -> Result<(), NoSolution>;
245242

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ where
4343
goal: Goal<I, Self>,
4444
) -> impl Iterator<Item = I::Clause> {
4545
goal.param_env.caller_bounds().non_trait_clauses().iter().filter(move |&assumption| {
46-
match Self::fast_reject_assumption(ecx, goal, None, assumption) {
46+
match Self::fast_reject_assumption(ecx, goal, assumption) {
4747
Ok(()) => true,
4848
Err(NoSolution) => false,
4949
}
@@ -52,7 +52,6 @@ where
5252
fn fast_reject_assumption(
5353
ecx: &mut EvalCtxt<'_, D>,
5454
goal: Goal<I, Self>,
55-
_goal_lang_item: Option<SolverTraitLangItem>,
5655
assumption: I::Clause,
5756
) -> Result<(), NoSolution> {
5857
if let Some(host_clause) = assumption.as_host_effect_clause()

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ where
185185
goal: Goal<I, Self>,
186186
) -> impl Iterator<Item = I::Clause> {
187187
goal.param_env.caller_bounds().non_trait_clauses().iter().filter(move |&assumption| {
188-
match Self::fast_reject_assumption(ecx, goal, None, assumption) {
188+
match Self::fast_reject_assumption(ecx, goal, assumption) {
189189
Ok(()) => true,
190190
Err(NoSolution) => false,
191191
}
@@ -195,7 +195,6 @@ where
195195
fn fast_reject_assumption(
196196
ecx: &mut EvalCtxt<'_, D>,
197197
goal: Goal<I, Self>,
198-
_goal_lang_item: Option<SolverTraitLangItem>,
199198
assumption: I::Clause,
200199
) -> Result<(), NoSolution> {
201200
let alias_def_id = match goal.predicate.alias.kind {

‎compiler/rustc_next_trait_solver/src/solve/trait_goals.rs‎

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -177,35 +177,11 @@ where
177177
fn fast_reject_assumption(
178178
ecx: &mut EvalCtxt<'_, D>,
179179
goal: Goal<I, Self>,
180-
goal_lang_item: Option<SolverTraitLangItem>,
181180
assumption: I::Clause,
182181
) -> Result<(), NoSolution> {
183-
fn trait_def_id_matches<I: Interner>(
184-
cx: I,
185-
clause_def_id: I::TraitId,
186-
goal_def_id: I::TraitId,
187-
polarity: ClausePolarity,
188-
goal_lang_item: Option<SolverTraitLangItem>,
189-
) -> bool {
190-
clause_def_id == goal_def_id
191-
// PERF(sized-hierarchy): Sizedness supertraits aren't elaborated to improve perf, so
192-
// check for a `MetaSized` supertrait being matched against a `Sized` assumption.
193-
//
194-
// `PointeeSized` bounds are syntactic sugar for a lack of bounds so don't need this.
195-
|| (polarity == ClausePolarity::Positive
196-
&& goal_lang_item == Some(SolverTraitLangItem::MetaSized)
197-
&& cx.is_trait_lang_item(clause_def_id, SolverTraitLangItem::Sized))
198-
}
199-
200182
if let Some(trait_clause) = assumption.as_trait_clause()
201183
&& trait_clause.polarity() == goal.predicate.polarity
202-
&& trait_def_id_matches(
203-
ecx.cx(),
204-
trait_clause.def_id(),
205-
goal.predicate.def_id(),
206-
goal.predicate.polarity,
207-
goal_lang_item,
208-
)
184+
&& trait_clause.def_id() == goal.predicate.def_id()
209185
&& DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
210186
goal.predicate.trait_ref.args,
211187
trait_clause.skip_binder().trait_ref.args,
@@ -225,19 +201,6 @@ where
225201
) -> QueryResultOrRerunNonErased<I> {
226202
let trait_clause = assumption.as_trait_clause().unwrap();
227203

228-
// PERF(sized-hierarchy): Sizedness supertraits aren't elaborated to improve perf, so
229-
// check for a `Sized` subtrait when looking for `MetaSized`. `PointeeSized` bounds
230-
// are syntactic sugar for a lack of bounds so don't need this.
231-
// We don't need to check polarity, `fast_reject_assumption` already rejected non-`Positive`
232-
// polarity `Sized` assumptions as matching non-`Positive` `MetaSized` goals.
233-
if ecx.cx().is_trait_lang_item(goal.predicate.def_id(), SolverTraitLangItem::MetaSized)
234-
&& ecx.cx().is_trait_lang_item(trait_clause.def_id(), SolverTraitLangItem::Sized)
235-
{
236-
let meta_sized_clause =
237-
trait_predicate_with_def_id(ecx.cx(), trait_clause, goal.predicate.def_id());
238-
return Self::match_assumption(ecx, goal, meta_sized_clause, then);
239-
}
240-
241204
let assumption_trait_pred = ecx.instantiate_binder_with_infer(trait_clause);
242205
ecx.eq(goal.param_env, goal.predicate.trait_ref, assumption_trait_pred.trait_ref)?;
243206

@@ -1056,25 +1019,6 @@ where
10561019
}
10571020
}
10581021

1059-
/// Small helper function to change the `def_id` of a trait predicate - this is not normally
1060-
/// something that you want to do, as different traits will require different args and so making
1061-
/// it easy to change the trait is something of a footgun, but it is useful in the narrow
1062-
/// circumstance of changing from `MetaSized` to `Sized`, which happens as part of the lazy
1063-
/// elaboration of sizedness candidates.
1064-
#[inline(always)]
1065-
fn trait_predicate_with_def_id<I: Interner>(
1066-
cx: I,
1067-
clause: ty::Binder<I, ty::TraitClause<I>>,
1068-
did: I::TraitId,
1069-
) -> I::Clause {
1070-
clause
1071-
.map_bound(|c| TraitClause {
1072-
trait_ref: TraitRef::new_from_args(cx, did, c.trait_ref.args),
1073-
polarity: c.polarity,
1074-
})
1075-
.upcast(cx)
1076-
}
1077-
10781022
impl<D, I> EvalCtxt<'_, D>
10791023
where
10801024
D: SolverDelegate<Interner = I>,

‎compiler/rustc_trait_selection/src/traits/coherence.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ fn impl_intersection_has_negative_obligation(
447447
.into_iter()
448448
.map(|(c, s)| (c.skip_norm_wip(), s)),
449449
)
450-
.elaborate_sized()
451450
.any(|(clause, _)| try_prove_negated_where_clause(infcx, clause, param_env))
452451
}
453452

‎compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
232232
}
233233

234234
selcx.infcx.probe(|_| {
235-
let bound = util::lazily_elaborate_sizedness_candidate(
236-
selcx.infcx,
237-
obligation,
238-
bound,
239-
);
240-
241235
// We checked the polarity already
242236
match selcx.match_normalize_trait_ref(
243237
obligation,
@@ -293,9 +287,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
293287
let obligation_args = stack.obligation.predicate.skip_binder().trait_ref.args;
294288
// Keep only those bounds which may apply, and propagate overflow if it occurs.
295289
for bound in bounds {
296-
let bound =
297-
util::lazily_elaborate_sizedness_candidate(self.infcx, stack.obligation, bound);
298-
299290
// Micro-optimization: filter out predicates relating to different traits.
300291
if bound.def_id() != stack.obligation.predicate.def_id() {
301292
continue;

‎compiler/rustc_trait_selection/src/traits/select/confirmation.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
166166
let candidate_predicate = candidate_predicate
167167
.as_trait_clause()
168168
.expect("projection candidate is not a trait predicate");
169-
let candidate_predicate =
170-
util::lazily_elaborate_sizedness_candidate(self.infcx, obligation, candidate_predicate);
171169

172170
let candidate = candidate_predicate.map_bound(|t| t.trait_ref);
173171

@@ -204,13 +202,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
204202
) -> PredicateObligations<'tcx> {
205203
debug!(?obligation, ?param, "confirm_param_candidate");
206204

207-
let param = util::lazily_elaborate_sizedness_candidate(
208-
self.infcx,
209-
obligation,
210-
param.upcast(self.infcx.tcx),
211-
)
212-
.map_bound(|p| p.trait_ref);
213-
214205
// During evaluation, we already checked that this
215206
// where-clause trait-ref could be unified with the obligation
216207
// trait-ref. Repeat that unification now without any

‎compiler/rustc_trait_selection/src/traits/util.rs‎

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use rustc_attr_ir::lang_items::LangItem;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_hir::def_id::DefId;
66
use rustc_infer::infer::InferCtxt;
7-
use rustc_infer::traits::PolyTraitObligation;
87
pub use rustc_infer::traits::util::*;
9-
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
108
use rustc_middle::ty::{
11-
self, CallerBoundsIterator, ClausePolarity, PolyTraitClause, SizedTraitKind, TraitClause,
12-
TraitRef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, Unnormalized,
9+
self, CallerBoundsIterator, SizedTraitKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt,
10+
Unnormalized,
1311
};
1412
pub use rustc_next_trait_solver::placeholder::{BoundVarReplacer, PlaceholderReplacer};
1513
use rustc_span::Span;
@@ -250,10 +248,7 @@ pub fn implicit_fast_path<'tcx>(
250248
.iter()
251249
{
252250
let clause_pred = clause_pred.skip_binder();
253-
if clause_pred.def_id() == trait_pred.def_id()
254-
|| (sizedness == SizedTraitKind::MetaSized
255-
&& tcx.is_lang_item(clause_pred.def_id(), LangItem::Sized))
256-
{
251+
if clause_pred.def_id() == trait_pred.def_id() {
257252
return true;
258253
}
259254
}
@@ -262,41 +257,3 @@ pub fn implicit_fast_path<'tcx>(
262257

263258
false
264259
}
265-
266-
/// To improve performance, sizedness traits are not elaborated and so special-casing is required
267-
/// in the trait solver to find a `Sized` candidate for a `MetaSized` obligation. Returns the
268-
/// predicate to used in the candidate for such a `obligation`, given a `candidate`.
269-
pub(crate) fn lazily_elaborate_sizedness_candidate<'tcx>(
270-
infcx: &InferCtxt<'tcx>,
271-
obligation: &PolyTraitObligation<'tcx>,
272-
candidate: PolyTraitClause<'tcx>,
273-
) -> PolyTraitClause<'tcx> {
274-
if !infcx.tcx.is_lang_item(obligation.predicate.def_id(), LangItem::MetaSized)
275-
|| !infcx.tcx.is_lang_item(candidate.def_id(), LangItem::Sized)
276-
{
277-
return candidate;
278-
}
279-
280-
if obligation.predicate.polarity() != ClausePolarity::Positive
281-
|| candidate.polarity() != ClausePolarity::Positive
282-
{
283-
return candidate;
284-
}
285-
286-
let drcx = DeepRejectCtxt::relate_rigid_rigid(infcx.tcx);
287-
if !drcx.args_may_unify(
288-
obligation.predicate.skip_binder().trait_ref.args,
289-
candidate.skip_binder().trait_ref.args,
290-
) {
291-
return candidate;
292-
}
293-
294-
candidate.map_bound(|c| TraitClause {
295-
trait_ref: TraitRef::new_from_args(
296-
infcx.tcx,
297-
obligation.predicate.def_id(),
298-
c.trait_ref.args,
299-
),
300-
polarity: c.polarity,
301-
})
302-
}

‎compiler/rustc_type_ir/src/elaborate.rs‎

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use smallvec::smallvec;
44

55
use crate::data_structures::HashSet;
66
use crate::inherent::*;
7-
use crate::lang_items::SolverTraitLangItem;
87
use crate::outlives::{Component, push_outlives_components};
98
use crate::{self as ty, Interner, Region, Unnormalized, Upcast as _};
109

@@ -19,20 +18,13 @@ pub struct Elaborator<I: Interner, O> {
1918
stack: Vec<O>,
2019
visited: HashSet<ty::Binder<I, ty::PredicateKind<I>>>,
2120
mode: Filter,
22-
elaborate_sized: ElaborateSized,
2321
}
2422

2523
enum Filter {
2624
All,
2725
OnlySelf,
2826
}
2927

30-
#[derive(Eq, PartialEq)]
31-
enum ElaborateSized {
32-
Yes,
33-
No,
34-
}
35-
3628
/// Describes how to elaborate an obligation into a sub-obligation.
3729
pub trait Elaboratable<I: Interner> {
3830
fn predicate(&self) -> I::Predicate;
@@ -87,13 +79,8 @@ pub fn elaborate<I: Interner, O: Elaboratable<I>>(
8779
cx: I,
8880
obligations: impl IntoIterator<Item = O>,
8981
) -> Elaborator<I, O> {
90-
let mut elaborator = Elaborator {
91-
cx,
92-
stack: Vec::new(),
93-
visited: HashSet::default(),
94-
mode: Filter::All,
95-
elaborate_sized: ElaborateSized::No,
96-
};
82+
let mut elaborator =
83+
Elaborator { cx, stack: Vec::new(), visited: HashSet::default(), mode: Filter::All };
9784
elaborator.extend_deduped(obligations);
9885
elaborator
9986
}
@@ -119,13 +106,6 @@ impl<I: Interner, O: Elaboratable<I>> Elaborator<I, O> {
119106
self
120107
}
121108

122-
/// Start elaborating `Sized` - reqd during coherence checking, normally skipped to improve
123-
/// compiler performance.
124-
pub fn elaborate_sized(mut self) -> Self {
125-
self.elaborate_sized = ElaborateSized::Yes;
126-
self
127-
}
128-
129109
fn elaborate(&mut self, elaboratable: &O) {
130110
let cx = self.cx;
131111

@@ -134,19 +114,6 @@ impl<I: Interner, O: Elaboratable<I>> Elaborator<I, O> {
134114
return;
135115
};
136116

137-
// PERF(sized-hierarchy): To avoid iterating over sizedness supertraits in
138-
// parameter environments, as an optimisation, sizedness supertraits aren't
139-
// elaborated, so check if a `Sized` obligation is being elaborated to a
140-
// `MetaSized` obligation and emit it. Candidate assembly and confirmation
141-
// are modified to check for the `Sized` subtrait when a `MetaSized` obligation
142-
// is present.
143-
if self.elaborate_sized == ElaborateSized::No
144-
&& let Some(did) = clause.as_trait_clause().map(|c| c.def_id())
145-
&& self.cx.is_trait_lang_item(did, SolverTraitLangItem::Sized)
146-
{
147-
return;
148-
}
149-
150117
let bound_clause = clause.kind();
151118
match bound_clause.skip_binder() {
152119
ty::ClauseKind::Trait(data) => {

‎tests/ui/attributes/doc_examples/rustc_dump_item_bounds.stderr‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ error: rustc_dump_item_bounds
66
|
77
= note: Binder { value: TraitClause(<<Self as Trait<T>>::Assoc as std::cmp::PartialEq<std::string::String>>, polarity:Positive), bound_vars: [] }
88
= note: Binder { value: TraitClause(<<Self as Trait<T>>::Assoc as std::marker::Sized>, polarity:Positive), bound_vars: [] }
9+
= note: Binder { value: TraitClause(<<Self as Trait<T>>::Assoc as std::marker::MetaSized>, polarity:Positive), bound_vars: [] }
910

1011
error: aborting due to 1 previous error
1112

0 commit comments

Comments
 (0)