Skip to content

Commit d87fbb9

Browse files
committed
Deduplicate logic between projection normalization with and without escaping bound vars
1 parent d3d537b commit d87fbb9

File tree

1 file changed

+21
-59
lines changed

1 file changed

+21
-59
lines changed

compiler/rustc_trait_selection/src/traits/query/normalize.rs

+21-59
Original file line numberDiff line numberDiff line change
@@ -257,62 +257,20 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
257257

258258
ty::Opaque => ty.try_super_fold_with(self)?,
259259

260-
ty::Projection if !data.has_escaping_bound_vars() => {
261-
// This branch is just an optimization: when we don't have escaping bound vars,
262-
// we don't need to replace them with placeholders (see branch below).
263-
264-
let tcx = self.infcx.tcx;
265-
let data = data.try_fold_with(self)?;
266-
267-
let mut orig_values = OriginalQueryValues::default();
268-
// HACK(matthewjasper) `'static` is special-cased in selection,
269-
// so we cannot canonicalize it.
270-
let c_data = self
271-
.infcx
272-
.canonicalize_query_keep_static(self.param_env.and(data), &mut orig_values);
273-
debug!("QueryNormalizer: c_data = {:#?}", c_data);
274-
debug!("QueryNormalizer: orig_values = {:#?}", orig_values);
275-
let result = tcx.normalize_projection_ty(c_data)?;
276-
// We don't expect ambiguity.
277-
if result.is_ambiguous() {
278-
// Rustdoc normalizes possibly not well-formed types, so only
279-
// treat this as a bug if we're not in rustdoc.
280-
if !tcx.sess.opts.actually_rustdoc {
281-
tcx.sess.delay_span_bug(
282-
DUMMY_SP,
283-
format!("unexpected ambiguity: {:?} {:?}", c_data, result),
284-
);
285-
}
286-
return Err(NoSolution);
287-
}
288-
let InferOk { value: result, obligations } =
289-
self.infcx.instantiate_query_response_and_region_obligations(
290-
self.cause,
291-
self.param_env,
292-
&orig_values,
293-
result,
294-
)?;
295-
debug!("QueryNormalizer: result = {:#?}", result);
296-
debug!("QueryNormalizer: obligations = {:#?}", obligations);
297-
self.obligations.extend(obligations);
298-
299-
let res = result.normalized_ty;
300-
// `tcx.normalize_projection_ty` may normalize to a type that still has
301-
// unevaluated consts, so keep normalizing here if that's the case.
302-
if res != ty && res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) {
303-
res.try_super_fold_with(self)?
304-
} else {
305-
res
306-
}
307-
}
308-
309260
ty::Projection => {
310261
// See note in `rustc_trait_selection::traits::project`
311262

312263
let tcx = self.infcx.tcx;
313264
let infcx = self.infcx;
314-
let (data, mapped_regions, mapped_types, mapped_consts) =
315-
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
265+
// Just an optimization: When we don't have escaping bound vars,
266+
// we don't need to replace them with placeholders.
267+
let (data, maps) = if data.has_escaping_bound_vars() {
268+
let (data, mapped_regions, mapped_types, mapped_consts) =
269+
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
270+
(data, Some((mapped_regions, mapped_types, mapped_consts)))
271+
} else {
272+
(data, None)
273+
};
316274
let data = data.try_fold_with(self)?;
317275

318276
let mut orig_values = OriginalQueryValues::default();
@@ -346,14 +304,18 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
346304
debug!("QueryNormalizer: result = {:#?}", result);
347305
debug!("QueryNormalizer: obligations = {:#?}", obligations);
348306
self.obligations.extend(obligations);
349-
let res = PlaceholderReplacer::replace_placeholders(
350-
infcx,
351-
mapped_regions,
352-
mapped_types,
353-
mapped_consts,
354-
&self.universes,
355-
result.normalized_ty,
356-
);
307+
let res = if let Some((mapped_regions, mapped_types, mapped_consts)) = maps {
308+
PlaceholderReplacer::replace_placeholders(
309+
infcx,
310+
mapped_regions,
311+
mapped_types,
312+
mapped_consts,
313+
&self.universes,
314+
result.normalized_ty,
315+
)
316+
} else {
317+
result.normalized_ty
318+
};
357319
// `tcx.normalize_projection_ty` may normalize to a type that still has
358320
// unevaluated consts, so keep normalizing here if that's the case.
359321
if res != ty && res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) {

0 commit comments

Comments
 (0)