Skip to content

Commit 2080614

Browse files
Walk into types before normalizing them
1 parent 14393b3 commit 2080614

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

Lines changed: 19 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)]
@@ -222,16 +222,27 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for TryNormalizeAfterErasingRegionsF
222222
}
223223

224224
fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
225-
match self.try_normalize_generic_arg_after_erasing_regions(ty.into()) {
226-
Ok(t) => Ok(t.expect_ty()),
227-
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)
228234
}
229235
}
230236

231-
fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
232-
match self.try_normalize_generic_arg_after_erasing_regions(c.into()) {
233-
Ok(t) => Ok(t.expect_const()),
234-
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)
235246
}
236247
}
237248
}

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

0 commit comments

Comments
 (0)