Skip to content

Commit dbba04e

Browse files
committed
use the new routine to eagerly normalize
1 parent e82c3c8 commit dbba04e

12 files changed

Lines changed: 44 additions & 164 deletions

File tree

compiler/rustc_borrowck/src/type_check/canonical.rs

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use rustc_middle::mir::{Body, ConstraintCategory};
88
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, Unnormalized, Upcast};
99
use rustc_span::Span;
1010
use rustc_span::def_id::DefId;
11-
use rustc_trait_selection::solve::NoSolution;
1211
use rustc_trait_selection::traits::ObligationCause;
13-
use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
1412
use rustc_trait_selection::traits::query::type_op::{self, TypeOpOutput};
1513
use tracing::{debug, instrument};
1614

@@ -242,71 +240,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
242240
body.source.def_id().expect_local(),
243241
);
244242

245-
if self.infcx.next_trait_solver() {
246-
let param_env = self.infcx.param_env;
247-
// FIXME: Make this into a real type op?
248-
self.fully_perform_op(
249-
location.to_locations(),
250-
ConstraintCategory::Boring,
251-
CustomTypeOp::new(
252-
|ocx| {
253-
let structurally_normalize = |ty| {
254-
ocx.structurally_normalize_ty(&cause, param_env, Unnormalized::new_wip(ty))
255-
.unwrap_or_else(|_| bug!("struct tail should have been computable, since we computed it in HIR"))
256-
};
257-
258-
let tail = tcx.struct_tail_raw(
259-
ty,
260-
&cause,
261-
structurally_normalize,
262-
|| {},
263-
);
264-
265-
Ok(tail)
266-
},
267-
"normalizing struct tail",
268-
),
269-
)
270-
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))
271-
} else {
272-
let mut normalize = |ty| self.normalize(ty, location);
273-
let tail = tcx.struct_tail_raw(ty, &cause, &mut normalize, || {});
274-
normalize(tail)
275-
}
276-
}
277-
278-
#[instrument(skip(self), level = "debug")]
279-
pub(super) fn structurally_resolve(
280-
&mut self,
281-
ty: Ty<'tcx>,
282-
location: impl NormalizeLocation,
283-
) -> Ty<'tcx> {
284-
if self.infcx.next_trait_solver() {
285-
let body = self.body;
286-
let param_env = self.infcx.param_env;
287-
// FIXME: Make this into a real type op?
288-
self.fully_perform_op(
289-
location.to_locations(),
290-
ConstraintCategory::Boring,
291-
CustomTypeOp::new(
292-
|ocx| {
293-
ocx.structurally_normalize_ty(
294-
&ObligationCause::misc(
295-
location.to_locations().span(body),
296-
body.source.def_id().expect_local(),
297-
),
298-
param_env,
299-
Unnormalized::new_wip(ty),
300-
)
301-
.map_err(|_| NoSolution)
302-
},
303-
"normalizing struct tail",
304-
),
305-
)
306-
.unwrap_or_else(|guar| Ty::new_error(self.tcx(), guar))
307-
} else {
308-
self.normalize(ty, location)
309-
}
243+
let mut normalize = |ty| self.normalize(ty, location);
244+
let tail = tcx.struct_tail_raw(ty, &cause, &mut normalize, || {});
245+
normalize(tail)
310246
}
311247

312248
#[instrument(skip(self), level = "debug")]

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
461461
let projected_ty = curr_projected_ty.projection_ty_core(
462462
tcx,
463463
proj,
464-
|ty| self.structurally_resolve(ty, locations),
464+
|ty| self.normalize(ty, locations),
465465
|ty, variant_index, field, ()| PlaceTy::field_ty(tcx, ty, variant_index, field),
466466
|_| unreachable!(),
467467
);

compiler/rustc_hir_analysis/src/autoderef.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,7 @@ impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
8787
let (kind, new_ty) =
8888
if let Some(ty) = self.state.cur_ty.builtin_deref(self.include_raw_pointers) {
8989
debug_assert_eq!(ty, self.infcx.resolve_vars_if_possible(ty));
90-
// NOTE: we may still need to normalize the built-in deref in case
91-
// we have some type like `&<Ty as Trait>::Assoc`, since users of
92-
// autoderef expect this type to have been structurally normalized.
93-
if self.infcx.next_trait_solver()
94-
&& let ty::Alias(..) = ty.kind()
95-
{
96-
let (normalized_ty, obligations) =
97-
self.structurally_normalize_ty(Unnormalized::new_wip(ty))?;
98-
self.state.obligations.extend(obligations);
99-
(AutoderefKind::Builtin, normalized_ty)
100-
} else {
101-
(AutoderefKind::Builtin, ty)
102-
}
90+
(AutoderefKind::Builtin, ty)
10391
} else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
10492
// The overloaded deref check already normalizes the pointee type.
10593
(AutoderefKind::Overloaded, ty)
@@ -177,35 +165,27 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
177165
return None;
178166
}
179167

180-
let (normalized_ty, obligations) = self.structurally_normalize_ty(Unnormalized::new(
181-
Ty::new_projection(tcx, trait_target_def_id, [ty]),
182-
))?;
168+
let (normalized_ty, obligations) = self
169+
.normalize_ty(Unnormalized::new(Ty::new_projection(tcx, trait_target_def_id, [ty])))?;
183170
debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
184171
self.state.obligations.extend(obligations);
185172

186173
Some(self.infcx.resolve_vars_if_possible(normalized_ty))
187174
}
188175

189176
#[instrument(level = "debug", skip(self), ret)]
190-
pub fn structurally_normalize_ty(
177+
pub fn normalize_ty(
191178
&self,
192179
ty: Unnormalized<'tcx, Ty<'tcx>>,
193180
) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
194181
let ocx = ObligationCtxt::new(self.infcx);
195-
let Ok(normalized_ty) = ocx.structurally_normalize_ty(
182+
let normalized_ty = ocx.normalize(
196183
&traits::ObligationCause::misc(self.span, self.body_id),
197184
self.param_env,
198185
ty,
199-
) else {
200-
// We shouldn't have errors here in the old solver, except for
201-
// evaluate/fulfill mismatches, but that's not a reason for an ICE.
202-
return None;
203-
};
186+
);
204187
let errors = ocx.try_evaluate_obligations();
205188
if !errors.is_empty() {
206-
if self.infcx.next_trait_solver() {
207-
unreachable!();
208-
}
209189
// We shouldn't have errors here in the old solver, except for
210190
// evaluate/fulfill mismatches, but that's not a reason for an ICE.
211191
debug!(?errors, "encountered errors while fulfilling");

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,6 @@ fn orphan_check<'tcx>(
315315
return Ok(user_ty);
316316
}
317317

318-
let ty = if infcx.next_trait_solver() {
319-
ocx.structurally_normalize_ty(
320-
&cause,
321-
ty::ParamEnv::empty(),
322-
Unnormalized::new_wip(infcx.resolve_vars_if_possible(ty)),
323-
)
324-
.unwrap_or(ty)
325-
} else {
326-
ty
327-
};
328-
329318
Ok::<_, !>(ty)
330319
};
331320

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,23 +1099,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10991099
// Make sure to structurally resolve the types, since we use
11001100
// the `TyKind`s heavily in coercion.
11011101
let ocx = ObligationCtxt::new(self);
1102-
let structurally_resolve = |ty| {
1103-
let ty = self.shallow_resolve(ty);
1104-
if self.next_trait_solver()
1105-
&& let ty::Alias(..) = ty.kind()
1106-
{
1107-
ocx.structurally_normalize_ty(&cause, self.param_env, Unnormalized::new_wip(ty))
1108-
} else {
1109-
Ok(ty)
1110-
}
1111-
};
1112-
let Ok(expr_ty) = structurally_resolve(expr_ty) else {
1113-
return false;
1114-
};
1115-
let Ok(target_ty) = structurally_resolve(target_ty) else {
1116-
return false;
1117-
};
1118-
11191102
let Ok(ok) = coerce.coerce(expr_ty, target_ty) else {
11201103
return false;
11211104
};

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,21 +2112,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
21122112
// `WhereClauseCandidate` requires that the self type is a param,
21132113
// because it has special behavior with candidate preference as an
21142114
// inherent pick.
2115-
match ocx.structurally_normalize_ty(
2115+
let ty = ocx.normalize(
21162116
cause,
21172117
self.param_env,
21182118
Unnormalized::new_wip(trait_ref.self_ty()),
2119-
) {
2120-
Ok(ty) => {
2121-
if !matches!(ty.kind(), ty::Param(_)) {
2122-
debug!("--> not a param ty: {xform_self_ty:?}");
2123-
return ProbeResult::NoMatch;
2124-
}
2125-
}
2126-
Err(errors) => {
2127-
debug!("--> cannot relate self-types {:?}", errors);
2128-
return ProbeResult::NoMatch;
2129-
}
2119+
);
2120+
if !matches!(ty.kind(), ty::Param(_)) {
2121+
debug!("--> not a param ty: {xform_self_ty:?}");
2122+
return ProbeResult::NoMatch;
21302123
}
21312124
}
21322125

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,13 +1586,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15861586
|alias_term: ty::AliasTerm<'tcx>, expected_term: ty::Term<'tcx>| {
15871587
let ocx = ObligationCtxt::new(self);
15881588

1589-
let Ok(normalized_term) = ocx.structurally_normalize_term(
1589+
let normalized_term = ocx.normalize(
15901590
&ObligationCause::dummy(),
15911591
obligation.param_env,
15921592
Unnormalized::new_wip(alias_term.to_term(self.tcx)),
1593-
) else {
1594-
return None;
1595-
};
1593+
);
15961594

15971595
if let Err(terr) = ocx.eq(
15981596
&ObligationCause::dummy(),
@@ -2859,13 +2857,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
28592857
);
28602858
let trait_ref = normalized_predicate.trait_ref;
28612859

2862-
let Ok(assume) = ocx.structurally_normalize_const(
2860+
let assume = ocx.normalize(
28632861
&obligation.cause,
28642862
obligation.param_env,
28652863
Unnormalized::new_wip(trait_ref.args.const_at(2)),
2866-
) else {
2867-
return (obligation.clone(), trait_predicate);
2868-
};
2864+
);
28692865

28702866
let Some(assume) = rustc_transmute::Assume::from_const(self.tcx, assume) else {
28712867
return (obligation.clone(), trait_predicate);
@@ -2912,17 +2908,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
29122908
);
29132909

29142910
let ocx = ObligationCtxt::new(self);
2915-
let Ok(assume) = ocx.structurally_normalize_const(
2911+
let assume = ocx.normalize(
29162912
&obligation.cause,
29172913
obligation.param_env,
29182914
Unnormalized::new_wip(trait_pred.trait_ref.args.const_at(2)),
2919-
) else {
2920-
self.dcx().span_delayed_bug(
2921-
span,
2922-
"Unable to construct rustc_transmute::Assume where it was previously possible",
2923-
);
2924-
return GetSafeTransmuteErrorAndReason::Silent;
2925-
};
2915+
);
29262916

29272917
let Some(assume) = rustc_transmute::Assume::from_const(self.infcx.tcx, assume) else {
29282918
self.dcx().span_delayed_bug(

compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ fn do_normalize_predicates<'tcx>(
258258
elaborated_env: ty::ParamEnv<'tcx>,
259259
predicates: Vec<ty::Clause<'tcx>>,
260260
) -> Result<Vec<ty::Clause<'tcx>>, ErrorGuaranteed> {
261-
let span = cause.span;
261+
// Even if we move back to eager normalization elsewhere,
262+
// param env normalization remains lazy in the next solver.
263+
if tcx.next_trait_solver_globally() {
264+
return Ok(predicates);
265+
}
262266

263267
// FIXME. We should really... do something with these region
264268
// obligations. But this call just continues the older
@@ -273,6 +277,7 @@ fn do_normalize_predicates<'tcx>(
273277
// by wfcheck anyway, so I'm not sure we have to check
274278
// them here too, and we will remove this function when
275279
// we move over to lazy normalization *anyway*.
280+
let span = cause.span;
276281
let infcx = tcx.infer_ctxt().ignoring_regions().build(TypingMode::non_body_analysis());
277282
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
278283
let predicates = ocx.normalize(&cause, elaborated_env, Unnormalized::new_wip(predicates));

compiler/rustc_trait_selection/src/traits/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ impl<'tcx> At<'_, 'tcx> {
3232
&self,
3333
value: Unnormalized<'tcx, T>,
3434
) -> InferOk<'tcx, T> {
35-
let value = value.skip_normalization();
3635
if self.infcx.next_trait_solver() {
3736
let Normalized { value, obligations } = crate::solve::normalize(*self, value);
3837
InferOk { value, obligations }
3938
} else {
39+
let value = value.skip_normalization();
4040
let mut selcx = SelectionContext::new(self.infcx);
4141
let Normalized { value, obligations } =
4242
normalize_with_depth(&mut selcx, self.param_env, self.cause.clone(), 0, value);

compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ where
158158
root_def_id,
159159
"query type op",
160160
span,
161-
|ocx| QueryTypeOp::perform_locally_with_next_solver(ocx, self, span),
161+
|ocx| {
162+
if let Some(result) = QueryTypeOp::try_fast_path(infcx.tcx, &self) {
163+
return Ok(result);
164+
}
165+
QueryTypeOp::perform_locally_with_next_solver(ocx, self, span)
166+
},
162167
)?
163168
.0);
164169
}

0 commit comments

Comments
 (0)