Skip to content

Commit 846cdcf

Browse files
authored
Rollup merge of rust-lang#108503 - BoxyUwU:rustdog_dont_use_querynormalizer, r=oli-obk
use `ObligationCtxt` not `QueryNormalizer` in rustdoc's `normalization` `QueryNormalizer` doesn't handle not-well-formed projections or ambiguity so should not be used by rustdoc as rustdoc happens on code that is not well formed. This PR replaces the usage of `QueryNormalizer` with `ObligationCtxt::normalize` which is designed to work on not-wf code while not being in typeck. This also removes two uses of `actually_rustdoc` from the compiler which seems good to me. I am somewhat confused as to the "point" of `QueryNormalizer`, it intends to be "the main way of normalizing" in the future and yet ICEs when encountering not wf types or when normalization is ambiguous which seems very incompatible with its stated goal since that makes it only suitable for using after typeck?
2 parents 2824db3 + d0c308c commit 846cdcf

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,10 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
284284
let result = tcx.normalize_projection_ty(c_data)?;
285285
// We don't expect ambiguity.
286286
if result.is_ambiguous() {
287-
// Rustdoc normalizes possibly not well-formed types, so only
288-
// treat this as a bug if we're not in rustdoc.
289-
if !tcx.sess.opts.actually_rustdoc {
290-
tcx.sess.delay_span_bug(
291-
DUMMY_SP,
292-
format!("unexpected ambiguity: {:?} {:?}", c_data, result),
293-
);
294-
}
287+
tcx.sess.delay_span_bug(
288+
DUMMY_SP,
289+
format!("unexpected ambiguity: {:?} {:?}", c_data, result),
290+
);
295291
return Err(NoSolution);
296292
}
297293
let InferOk { value: result, obligations } =

src/librustdoc/clean/mod.rs

+36-13
Original file line numberDiff line numberDiff line change
@@ -1666,22 +1666,45 @@ fn normalize<'tcx>(
16661666
}
16671667

16681668
use crate::rustc_trait_selection::infer::TyCtxtInferExt;
1669-
use crate::rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
1669+
use crate::rustc_trait_selection::traits::ObligationCtxt;
16701670
use rustc_middle::traits::ObligationCause;
16711671

1672-
// Try to normalize `<X as Y>::T` to a type
1672+
assert!(
1673+
!ty.has_non_region_infer(),
1674+
"`ty`: {ty:?} has pre existing infer vars before `InferCtxt` creation",
1675+
);
1676+
16731677
let infcx = cx.tcx.infer_ctxt().build();
1674-
let normalized = infcx
1675-
.at(&ObligationCause::dummy(), cx.param_env)
1676-
.query_normalize(ty)
1677-
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value));
1678-
match normalized {
1679-
Ok(normalized_value) => {
1680-
debug!("normalized {:?} to {:?}", ty, normalized_value);
1681-
Some(normalized_value)
1682-
}
1683-
Err(err) => {
1684-
debug!("failed to normalize {:?}: {:?}", ty, err);
1678+
// use an `ObligationCtxt` as it has a nice API for dealing with returned obligations from normalization
1679+
// and does not expect us to be inside of typeck. It also does not ICE when the projection could not be
1680+
// normalized like some other normalization routines (`QueryNormalizer`, `normalize_erasing_regions`, etc)
1681+
let ocx = ObligationCtxt::new(&infcx);
1682+
1683+
// Try to normalize `<X as Y>::T` to a type
1684+
let normalized = ocx.normalize(&ObligationCause::dummy(), cx.param_env, ty);
1685+
// We have to ensure that we deal with nested obligations from attempting to normalize as `ty`
1686+
// normalizing to `normalized` is only the case if the nested obligations hold.
1687+
let errs = ocx.select_all_or_error();
1688+
// Evaluating nested obligations might constrain infer vars that were created during normalization
1689+
// so we should resolve any infer vars in `normalized` to their new values.
1690+
let normalized = infcx.resolve_vars_if_possible(normalized);
1691+
1692+
match errs.as_slice() {
1693+
[] if normalized == ty => {
1694+
debug!("normalizing {:?} did not make progress", ty);
1695+
None
1696+
}
1697+
[] => {
1698+
debug!("normalized {:?} to {:?}", ty, normalized);
1699+
1700+
assert!(
1701+
!normalized.has_non_region_infer(),
1702+
"`normalized` has infer vars which would escape the `InferCtxt` they were created in"
1703+
);
1704+
Some(normalized)
1705+
}
1706+
errs => {
1707+
debug!("failed to normalize {:?}: {:?}", ty, errs);
16851708
None
16861709
}
16871710
}

0 commit comments

Comments
 (0)