Skip to content

Commit d464bf7

Browse files
authored
Unrolled build for #149501
Rollup merge of #149501 - lcnr:no-hard-error-on-norm-failure, r=lqd,oli-obk CTFE: avoid emitting a hard error on generic normalization failures Fixes #149081. The fix is quite unsatisfying and should not be necessary, cc #149283. That change is significantly more involved. This temporary fix introduces some unnecessary complexity and may hide other type system bugs. cc ````@rust-lang/types```` I think we should try to fix issue #149283 in the near future and then remove this hack again. I originally intended a more targeted fix. I wanted to skip evaluating constants in MIR opts if their body was polymorphic and the current generic arguments still reference generic parameters. Notes from looking into this: - we only fetch the MIR in the `eval_to_allocation_raw` query - figuring out which MIR to use is hard - dealing with trivial consts is annoying - need to resolve instances for associated consts - implementing this by hand is hard - inlining handles this issue by bailing on literally all normalization failures, even the ones that imply an unsoundness - `try_normalize_after_erasing_regions` generally does two things - deal with ambiguity after inlining - deal with error tainting issues (please don't, we should stop doing that) - CTFE could be changed to always silently ignore normalization failures if we're in a generic body - hides actual bugs <- went with this option r? types
2 parents 556beb9 + 02d84c8 commit d464bf7

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use rustc_middle::ty::layout::{
1111
self, FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf,
1212
LayoutOfHelpers, TyAndLayout,
1313
};
14-
use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypingEnv, Variance};
14+
use rustc_middle::ty::{
15+
self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingEnv, Variance,
16+
};
1517
use rustc_middle::{mir, span_bug};
1618
use rustc_span::Span;
1719
use rustc_target::callconv::FnAbi;
@@ -84,10 +86,31 @@ impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
8486
#[inline]
8587
fn handle_layout_err(
8688
&self,
87-
err: LayoutError<'tcx>,
89+
mut err: LayoutError<'tcx>,
8890
_: Span,
8991
_: Ty<'tcx>,
9092
) -> InterpErrorKind<'tcx> {
93+
// FIXME(#149283): This is really hacky and is only used to hide type
94+
// system bugs. We use it as a temporary fix for #149081.
95+
//
96+
// While it's expected that we sometimes get ambiguity errors when
97+
// entering another generic environment while the current environment
98+
// itself is still generic, we should never fail to entirely prove
99+
// something.
100+
match err {
101+
LayoutError::NormalizationFailure(ty, _) => {
102+
if ty.has_non_region_param() {
103+
err = LayoutError::TooGeneric(ty);
104+
}
105+
}
106+
107+
LayoutError::Unknown(_)
108+
| LayoutError::SizeOverflow(_)
109+
| LayoutError::InvalidSimd { .. }
110+
| LayoutError::TooGeneric(_)
111+
| LayoutError::ReferencesError(_)
112+
| LayoutError::Cycle(_) => {}
113+
}
91114
err_inval!(Layout(err))
92115
}
93116
}
@@ -112,7 +135,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
112135
/// and allows wrapping the actual [LayoutOf::layout_of] with a tracing span.
113136
/// See [LayoutOf::layout_of] for the original documentation.
114137
#[inline(always)]
115-
pub fn layout_of(&self, ty: Ty<'tcx>) -> <Self as LayoutOfHelpers<'tcx>>::LayoutOfResult {
138+
pub fn layout_of(&self, ty: Ty<'tcx>) -> Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>> {
116139
let _trace = enter_trace_span!(M, layouting::layout_of, ty = ?ty.kind());
117140
LayoutOf::layout_of(self, ty)
118141
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ compile-flags: -Copt-level=3 --crate-type=rlib
2+
//@ build-pass
3+
4+
// A regression test for #149081. The environment of `size` and `align`
5+
// currently means that the item bound of`T::Assoc` doesn't hold. This can
6+
// result in normalization failures and ICE during MIR optimizations.
7+
//
8+
// This will no longer be an issue once #149283 is implemented.
9+
10+
pub fn align<T: WithAssoc<Assoc = U>, U>() -> usize {
11+
std::mem::align_of::<Wrapper<T>>()
12+
}
13+
14+
pub fn size<T: WithAssoc<Assoc = U>, U>() -> usize {
15+
std::mem::size_of::<Wrapper<T>>()
16+
}
17+
18+
pub struct Wrapper<T: WithAssoc> {
19+
assoc2: <T::Assoc as WithAssoc>::Assoc,
20+
value: T,
21+
}
22+
23+
pub trait WithAssoc {
24+
type Assoc: WithAssoc;
25+
}

0 commit comments

Comments
 (0)