Skip to content

Commit afa7fb9

Browse files
committed
Auto merge of #156619 - ShoyuVanilla:less-normalizes-to-jank, r=lcnr
`-Znext-solver` Less normalizes-to janks cc rust-lang/trait-system-refactor-initiative#223 (comment) r? lcnr
2 parents 9d862dd + 0536376 commit afa7fb9

80 files changed

Lines changed: 614 additions & 622 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4141
self.type_matches_expected_vid(data.self_ty(), expected_vid)
4242
}
4343
ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
44-
self.type_matches_expected_vid(data.projection_term.self_ty(), expected_vid)
44+
if data.projection_term.kind.is_trait_projection() {
45+
self.type_matches_expected_vid(data.self_ty(), expected_vid)
46+
} else {
47+
false
48+
}
4549
}
4650
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
4751
| ty::PredicateKind::Subtype(..)

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18401840
let pred = bound_predicate.rebind(pred);
18411841
// `<Foo as Iterator>::Item = String`.
18421842
let projection_term = pred.skip_binder().projection_term;
1843+
if !projection_term.kind.is_trait_projection() {
1844+
return None;
1845+
}
1846+
18431847
let quiet_projection_term = projection_term
18441848
.with_replaced_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
18451849

compiler/rustc_infer/src/infer/opaque_types/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,10 @@ impl<'tcx> InferCtxt<'tcx> {
333333
goals.push(Goal::new(
334334
self.tcx,
335335
param_env,
336-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(
337-
ty::ProjectionPredicate {
338-
projection_term: projection_ty.into(),
339-
term: ty_var.into(),
340-
},
341-
)),
336+
ty::ProjectionPredicate {
337+
projection_term: projection_ty.into(),
338+
term: ty_var.into(),
339+
},
342340
));
343341
ty_var
344342
}

compiler/rustc_infer/src/infer/projection.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ impl<'tcx> InferCtxt<'tcx> {
2929
self.next_const_var(span).into()
3030
};
3131

32-
let projection =
33-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(ty::ProjectionPredicate {
34-
projection_term: alias_term,
35-
term: infer_var,
36-
}));
32+
let projection = ty::ProjectionPredicate { projection_term: alias_term, term: infer_var };
3733
let obligation =
3834
Obligation::with_depth(self.tcx, cause, recursion_depth, param_env, projection);
3935
obligations.push(obligation);

compiler/rustc_next_trait_solver/src/normalize.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_type_ir::data_structures::ensure_sufficient_stack;
22
use rustc_type_ir::inherent::*;
33
use rustc_type_ir::solve::{Goal, NoSolution};
44
use rustc_type_ir::{
5-
self as ty, Binder, FallibleTypeFolder, InferConst, InferCtxtLike, InferTy, Interner,
6-
TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
5+
self as ty, AliasTerm, Binder, FallibleTypeFolder, InferConst, InferCtxtLike, InferTy,
6+
Interner, TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
77
TypeVisitor, UniverseIndex,
88
};
99
use tracing::instrument;
@@ -101,7 +101,7 @@ impl<'a, Infcx, I, F> NormalizationFolder<'a, Infcx, I, F>
101101
where
102102
Infcx: InferCtxtLike<Interner = I>,
103103
I: Interner,
104-
F: FnMut(I::Term) -> Result<(I::Term, Option<Goal<I, I::Predicate>>), NoSolution>,
104+
F: FnMut(AliasTerm<I>) -> Result<(I::Term, Option<Goal<I, I::Predicate>>), NoSolution>,
105105
{
106106
pub fn new(
107107
infcx: &'a Infcx,
@@ -118,9 +118,9 @@ where
118118

119119
fn normalize_alias_term(
120120
&mut self,
121-
alias_term: I::Term,
121+
alias_term: AliasTerm<I>,
122122
has_escaping: HasEscapingBoundVars,
123-
) -> Result<I::Term, NoSolution> {
123+
) -> Result<Option<I::Term>, NoSolution> {
124124
let current_universe = self.infcx.universe();
125125
self.infcx.create_next_universe();
126126

@@ -139,20 +139,20 @@ where
139139
normalized.visit_with(&mut visitor);
140140
let max_universe = visitor.max_universe();
141141
if current_universe.cannot_name(max_universe) {
142-
return Ok(alias_term);
142+
return Ok(None);
143143
}
144144
}
145145

146146
self.stalled_goals.extend(ambig_goal);
147-
Ok(normalized)
147+
Ok(Some(normalized))
148148
}
149149
}
150150

151151
impl<'a, Infcx, I, F> FallibleTypeFolder<I> for NormalizationFolder<'a, Infcx, I, F>
152152
where
153153
Infcx: InferCtxtLike<Interner = I>,
154154
I: Interner,
155-
F: FnMut(I::Term) -> Result<(I::Term, Option<Goal<I, I::Predicate>>), NoSolution>,
155+
F: FnMut(AliasTerm<I>) -> Result<(I::Term, Option<Goal<I, I::Predicate>>), NoSolution>,
156156
{
157157
type Error = NoSolution;
158158

@@ -180,28 +180,32 @@ where
180180
// With eager normalization, we should normalize the args of alias before
181181
// normalizing the alias itself.
182182
let ty = ty.try_super_fold_with(self)?;
183-
let ty::Alias(..) = ty.kind() else { return Ok(ty) };
183+
let ty::Alias(alias_ty) = ty.kind() else { return Ok(ty) };
184184

185185
if ty.has_escaping_bound_vars() {
186-
let (ty, mapped_regions, mapped_types, mapped_consts) =
187-
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, ty);
188-
let result = ensure_sufficient_stack(|| {
189-
self.normalize_alias_term(ty.into(), HasEscapingBoundVars::Yes)
186+
let (alias_ty, mapped_regions, mapped_types, mapped_consts) =
187+
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, alias_ty);
188+
let Some(result) = ensure_sufficient_stack(|| {
189+
self.normalize_alias_term(alias_ty.into(), HasEscapingBoundVars::Yes)
190190
})?
191-
.expect_ty();
191+
else {
192+
return Ok(ty);
193+
};
194+
192195
Ok(PlaceholderReplacer::replace_placeholders(
193196
infcx,
194197
mapped_regions,
195198
mapped_types,
196199
mapped_consts,
197200
&self.universes,
198-
result,
201+
result.expect_ty(),
199202
))
200203
} else {
201204
Ok(ensure_sufficient_stack(|| {
202-
self.normalize_alias_term(ty.into(), HasEscapingBoundVars::No)
205+
self.normalize_alias_term(alias_ty.into(), HasEscapingBoundVars::No)
203206
})?
204-
.expect_ty())
207+
.map(|term| term.expect_ty())
208+
.unwrap_or(ty))
205209
}
206210
}
207211

@@ -215,28 +219,31 @@ where
215219
// With eager normalization, we should normalize the args of alias before
216220
// normalizing the alias itself.
217221
let ct = ct.try_super_fold_with(self)?;
218-
let ty::ConstKind::Unevaluated(..) = ct.kind() else { return Ok(ct) };
222+
let ty::ConstKind::Unevaluated(uv) = ct.kind() else { return Ok(ct) };
219223

220224
if ct.has_escaping_bound_vars() {
221-
let (ct, mapped_regions, mapped_types, mapped_consts) =
222-
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, ct);
223-
let result = ensure_sufficient_stack(|| {
224-
self.normalize_alias_term(ct.into(), HasEscapingBoundVars::Yes)
225+
let (uv, mapped_regions, mapped_types, mapped_consts) =
226+
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, uv);
227+
let Some(result) = ensure_sufficient_stack(|| {
228+
self.normalize_alias_term(uv.into(), HasEscapingBoundVars::Yes)
225229
})?
226-
.expect_const();
230+
else {
231+
return Ok(ct);
232+
};
227233
Ok(PlaceholderReplacer::replace_placeholders(
228234
infcx,
229235
mapped_regions,
230236
mapped_types,
231237
mapped_consts,
232238
&self.universes,
233-
result,
239+
result.expect_const(),
234240
))
235241
} else {
236242
Ok(ensure_sufficient_stack(|| {
237-
self.normalize_alias_term(ct.into(), HasEscapingBoundVars::No)
243+
self.normalize_alias_term(uv.into(), HasEscapingBoundVars::No)
238244
})?
239-
.expect_const())
245+
.map(|term| term.expect_const())
246+
.unwrap_or(ct))
240247
}
241248
}
242249
}

compiler/rustc_next_trait_solver/src/solve/alias_relate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ where
5252
let term = self.next_term_infer_of_kind(lhs);
5353
self.add_goal(
5454
GoalSource::TypeRelating,
55-
goal.with(cx, ty::NormalizesTo { alias, term }),
55+
goal.with(cx, ty::ProjectionPredicate { projection_term: alias, term }),
5656
);
5757
term
5858
} else {
@@ -64,7 +64,7 @@ where
6464
let term = self.next_term_infer_of_kind(rhs);
6565
self.add_goal(
6666
GoalSource::TypeRelating,
67-
goal.with(cx, ty::NormalizesTo { alias, term }),
67+
goal.with(cx, ty::ProjectionPredicate { projection_term: alias, term }),
6868
);
6969
term
7070
} else {

0 commit comments

Comments
 (0)