Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ea514dc

Browse files
committed
Auto merge of rust-lang#135969 - compiler-errors:normalize-erasing-regions, r=<try>
Rewrite `normalize_erasing_regions` to not use `QueryNormalizer` r? `@ghost`
2 parents ebcf860 + 2080614 commit ea514dc

File tree

7 files changed

+107
-78
lines changed

7 files changed

+107
-78
lines changed

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
1111
use tracing::{debug, instrument};
1212

1313
use crate::traits::query::NoSolution;
14-
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder};
14+
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
1515
use crate::ty::{self, EarlyBinder, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt};
1616

1717
#[derive(Debug, Copy, Clone, HashStable, TyEncodable, TyDecodable)]
@@ -46,6 +46,10 @@ impl<'tcx> TyCtxt<'tcx> {
4646
value,
4747
typing_env,
4848
);
49+
debug_assert!(
50+
!value.has_escaping_bound_vars(),
51+
"{value:?} cannot be normalized with escaping bound vars"
52+
);
4953

5054
// Erase first before we do the real query -- this keeps the
5155
// cache from being too polluted.
@@ -218,16 +222,27 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for TryNormalizeAfterErasingRegionsF
218222
}
219223

220224
fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
221-
match self.try_normalize_generic_arg_after_erasing_regions(ty.into()) {
222-
Ok(t) => Ok(t.expect_ty()),
223-
Err(_) => Err(NormalizationError::Type(ty)),
225+
// HACKY HACK: dont walk into binders
226+
if let ty::Alias(..) | ty::Dynamic(..) | ty::FnPtr(..) = ty.kind() {
227+
self.try_normalize_generic_arg_after_erasing_regions(ty.into())
228+
.map(|arg| arg.expect_ty())
229+
.map_err(|_| NormalizationError::Type(ty))
230+
} else if ty.has_aliases() {
231+
ty.try_super_fold_with(self)
232+
} else {
233+
Ok(ty)
224234
}
225235
}
226236

227-
fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
228-
match self.try_normalize_generic_arg_after_erasing_regions(c.into()) {
229-
Ok(t) => Ok(t.expect_const()),
230-
Err(_) => Err(NormalizationError::Const(c)),
237+
fn try_fold_const(&mut self, ct: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
238+
if let ty::ConstKind::Unevaluated(..) = ct.kind() {
239+
self.try_normalize_generic_arg_after_erasing_regions(ct.into())
240+
.map(|arg| arg.expect_const())
241+
.map_err(|_| NormalizationError::Const(ct))
242+
} else if ct.has_aliases() {
243+
ct.try_super_fold_with(self)
244+
} else {
245+
Ok(ct)
231246
}
232247
}
233248
}
Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
use rustc_infer::infer::TyCtxtInferExt;
2+
use rustc_infer::traits::ScrubbedTraitError;
3+
use rustc_middle::bug;
24
use rustc_middle::query::Providers;
35
use rustc_middle::traits::query::NoSolution;
4-
use rustc_middle::ty::{self, PseudoCanonicalInput, TyCtxt, TypeFoldable, TypeVisitableExt};
5-
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
6-
use rustc_trait_selection::traits::{Normalized, ObligationCause};
6+
use rustc_middle::ty::{
7+
self, PseudoCanonicalInput, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
8+
TypeVisitableExt, TypingMode,
9+
};
10+
use rustc_span::DUMMY_SP;
11+
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
12+
use rustc_trait_selection::error_reporting::traits::OverflowCause;
13+
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt};
714
use tracing::debug;
815

916
pub(crate) fn provide(p: &mut Providers) {
@@ -17,54 +24,83 @@ pub(crate) fn provide(p: &mut Providers) {
1724
};
1825
}
1926

27+
// FIXME(-Znext-solver): This can be simplified further to just a `deeply_normalize` call.
2028
fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + PartialEq + Copy>(
2129
tcx: TyCtxt<'tcx>,
2230
goal: PseudoCanonicalInput<'tcx, T>,
2331
) -> Result<T, NoSolution> {
2432
let PseudoCanonicalInput { typing_env, value } = goal;
2533
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
26-
let cause = ObligationCause::dummy();
27-
match infcx.at(&cause, param_env).query_normalize(value) {
28-
Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
29-
// We don't care about the `obligations`; they are
30-
// always only region relations, and we are about to
31-
// erase those anyway:
32-
// This has been seen to fail in RL, so making it a non-debug assertion to better catch
33-
// those cases.
34-
assert_eq!(
35-
normalized_obligations.iter().find(|p| not_outlives_predicate(p.predicate)),
36-
None,
37-
);
34+
let ocx = ObligationCtxt::new(&infcx);
35+
let mut normalized =
36+
ocx.deeply_normalize(&ObligationCause::dummy(), param_env, value).map_err(|errors| {
37+
match infcx.typing_mode() {
38+
TypingMode::PostAnalysis => {
39+
for error in errors {
40+
match error {
41+
ScrubbedTraitError::Cycle(pred) => {
42+
infcx.err_ctxt().report_overflow_error(
43+
OverflowCause::TraitSolver(pred.first().unwrap().predicate),
44+
DUMMY_SP,
45+
false,
46+
|_| {},
47+
);
48+
}
49+
_ => {}
50+
}
51+
}
52+
}
53+
_ => {}
54+
}
3855

39-
let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
40-
// It's unclear when `resolve_vars` would have an effect in a
41-
// fresh `InferCtxt`. If this assert does trigger, it will give
42-
// us a test case.
43-
debug_assert_eq!(normalized_value, resolved_value);
44-
let erased = infcx.tcx.erase_regions(resolved_value);
45-
debug_assert!(!erased.has_infer(), "{erased:?}");
46-
Ok(erased)
47-
}
48-
Err(NoSolution) => Err(NoSolution),
56+
// Otherwise, bail with `NoSolution`
57+
NoSolution
58+
})?;
59+
60+
if tcx.features().generic_const_exprs() {
61+
normalized =
62+
normalized.fold_with(&mut FoldConsts { ocx: &ocx, param_env, universes: vec![] });
63+
}
64+
65+
let resolved = infcx.resolve_vars_if_possible(normalized);
66+
let erased = tcx.erase_regions(resolved);
67+
68+
if erased.has_non_region_infer() {
69+
bug!("encountered infer when normalizing {value:?} to {erased:?}");
4970
}
71+
72+
Ok(erased)
5073
}
5174

52-
fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
53-
match p.kind().skip_binder() {
54-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..))
55-
| ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(..)) => false,
56-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
57-
| ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
58-
| ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..))
59-
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
60-
| ty::PredicateKind::NormalizesTo(..)
61-
| ty::PredicateKind::AliasRelate(..)
62-
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
63-
| ty::PredicateKind::DynCompatible(..)
64-
| ty::PredicateKind::Subtype(..)
65-
| ty::PredicateKind::Coerce(..)
66-
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
67-
| ty::PredicateKind::ConstEquate(..)
68-
| ty::PredicateKind::Ambiguous => true,
75+
struct FoldConsts<'a, 'tcx> {
76+
ocx: &'a ObligationCtxt<'a, 'tcx>,
77+
param_env: ty::ParamEnv<'tcx>,
78+
universes: Vec<Option<ty::UniverseIndex>>,
79+
}
80+
81+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for FoldConsts<'_, 'tcx> {
82+
fn cx(&self) -> TyCtxt<'tcx> {
83+
self.ocx.infcx.tcx
84+
}
85+
86+
fn fold_binder<T>(&mut self, binder: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
87+
where
88+
T: TypeFoldable<TyCtxt<'tcx>>,
89+
{
90+
self.universes.push(None);
91+
let binder = binder.super_fold_with(self);
92+
self.universes.pop();
93+
binder
94+
}
95+
96+
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
97+
let ct = traits::with_replaced_escaping_bound_vars(
98+
self.ocx.infcx,
99+
&mut self.universes,
100+
ct,
101+
|constant| traits::evaluate_const(self.ocx.infcx, constant, self.param_env),
102+
);
103+
debug!(?ct, ?self.param_env);
104+
ct.super_fold_with(self)
69105
}
70106
}

tests/crashes/125801.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/ui/consts/const-size_of-cycle.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | bytes: [u8; std::mem::size_of::<Foo>()]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: ...which requires computing layout of `Foo`...
1313
= note: ...which requires computing layout of `[u8; std::mem::size_of::<Foo>()]`...
14-
= note: ...which requires normalizing `[u8; std::mem::size_of::<Foo>()]`...
14+
= note: ...which requires normalizing `std::mem::size_of::<Foo>()`...
1515
= note: ...which again requires evaluating type-level constant, completing the cycle
1616
note: cycle used when checking that `Foo` is well-formed
1717
--> $DIR/const-size_of-cycle.rs:3:1

tests/ui/consts/issue-44415.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: ...which requires computing layout of `Foo`...
1313
= note: ...which requires computing layout of `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
14-
= note: ...which requires normalizing `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
14+
= note: ...which requires normalizing `unsafe { intrinsics::size_of::<Foo>() }`...
1515
= note: ...which again requires evaluating type-level constant, completing the cycle
1616
note: cycle used when checking that `Foo` is well-formed
1717
--> $DIR/issue-44415.rs:5:1

tests/ui/type-alias-impl-trait/infinite-cycle-involving-weak.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
//~ ERROR overflow normalizing the opaque type
12
#![feature(type_alias_impl_trait)]
23

34
type T = impl Copy;
4-
//~^ ERROR cannot resolve opaque type
55

66
static STATIC: T = None::<&'static T>;
77

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
error[E0720]: cannot resolve opaque type
2-
--> $DIR/infinite-cycle-involving-weak.rs:3:10
1+
error[E0275]: overflow normalizing the opaque type `T::{opaque#0}`
32
|
4-
LL | type T = impl Copy;
5-
| ^^^^^^^^^ cannot resolve opaque type
3+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`infinite_cycle_involving_weak`)
64

75
error: aborting due to 1 previous error
86

9-
For more information about this error, try `rustc --explain E0720`.
7+
For more information about this error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)