Skip to content

Commit 5dee519

Browse files
authored
Rollup merge of #113773 - compiler-errors:err-layout-bail, r=cjgillot
Don't attempt to compute layout of type referencing error Leads to more ICEs and strange diagnostics than are worth it. Fixes #113760
2 parents ca1f813 + d45eb41 commit 5dee519

File tree

10 files changed

+56
-14
lines changed

10 files changed

+56
-14
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ where
138138
err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => {
139139
ErrorHandled::TooGeneric
140140
}
141-
err_inval!(AlreadyReported(error_reported)) => ErrorHandled::Reported(error_reported),
141+
err_inval!(AlreadyReported(guar)) => ErrorHandled::Reported(guar),
142+
err_inval!(Layout(LayoutError::ReferencesError(guar))) => {
143+
ErrorHandled::Reported(guar.into())
144+
}
142145
err_inval!(Layout(layout_error @ LayoutError::SizeOverflow(_))) => {
143146
// We must *always* hard error on these, even if the caller wants just a lint.
144147
// The `message` makes little sense here, this is a more serious error than the

compiler/rustc_hir_typeck/src/intrinsicck.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
122122
} else {
123123
err.note(format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)))
124124
.note(format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
125-
let mut should_delay_as_bug = false;
126-
if let Err(LayoutError::Unknown(bad_from)) = sk_from && bad_from.references_error() {
127-
should_delay_as_bug = true;
128-
}
129-
if let Err(LayoutError::Unknown(bad_to)) = sk_to && bad_to.references_error() {
130-
should_delay_as_bug = true;
131-
}
132-
if should_delay_as_bug {
125+
if let Err(LayoutError::ReferencesError(_)) = sk_from {
126+
err.delay_as_bug();
127+
} else if let Err(LayoutError::ReferencesError(_)) = sk_to {
133128
err.delay_as_bug();
134129
}
135130
}

compiler/rustc_middle/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ middle_drop_check_overflow =
5252
overflow while adding drop-check rules for {$ty}
5353
.note = overflowed on {$overflow_ty}
5454
55+
middle_layout_references_error =
56+
the type has an unknown layout
57+
5558
middle_limit_invalid =
5659
`limit` must be a non-negative integer
5760
.label = {$error_str}

compiler/rustc_middle/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ pub enum LayoutError<'tcx> {
132132

133133
#[diag(middle_cycle)]
134134
Cycle,
135+
136+
#[diag(middle_layout_references_error)]
137+
ReferencesError,
135138
}
136139

137140
#[derive(Diagnostic)]

compiler/rustc_middle/src/ty/layout.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::def_id::DefId;
1010
use rustc_index::IndexVec;
1111
use rustc_session::config::OptLevel;
1212
use rustc_span::symbol::{sym, Symbol};
13-
use rustc_span::{Span, DUMMY_SP};
13+
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
1414
use rustc_target::abi::call::FnAbi;
1515
use rustc_target::abi::*;
1616
use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target};
@@ -212,6 +212,7 @@ pub enum LayoutError<'tcx> {
212212
Unknown(Ty<'tcx>),
213213
SizeOverflow(Ty<'tcx>),
214214
NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>),
215+
ReferencesError(ErrorGuaranteed),
215216
Cycle,
216217
}
217218

@@ -224,6 +225,7 @@ impl<'tcx> LayoutError<'tcx> {
224225
SizeOverflow(_) => middle_values_too_big,
225226
NormalizationFailure(_, _) => middle_cannot_be_normalized,
226227
Cycle => middle_cycle,
228+
ReferencesError(_) => middle_layout_references_error,
227229
}
228230
}
229231

@@ -237,6 +239,7 @@ impl<'tcx> LayoutError<'tcx> {
237239
E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() }
238240
}
239241
Cycle => E::Cycle,
242+
ReferencesError(_) => E::ReferencesError,
240243
}
241244
}
242245
}
@@ -257,6 +260,7 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> {
257260
e.get_type_for_failure()
258261
),
259262
LayoutError::Cycle => write!(f, "a cycle occurred during layout computation"),
263+
LayoutError::ReferencesError(_) => write!(f, "the type has an unknown layout"),
260264
}
261265
}
262266
}
@@ -323,7 +327,8 @@ impl<'tcx> SizeSkeleton<'tcx> {
323327
Err(
324328
e @ LayoutError::Cycle
325329
| e @ LayoutError::SizeOverflow(_)
326-
| e @ LayoutError::NormalizationFailure(..),
330+
| e @ LayoutError::NormalizationFailure(..)
331+
| e @ LayoutError::ReferencesError(_),
327332
) => return Err(e),
328333
};
329334

compiler/rustc_transmute/src/layout/tree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub(crate) mod rustc {
195195
impl<'tcx> From<&LayoutError<'tcx>> for Err {
196196
fn from(err: &LayoutError<'tcx>) -> Self {
197197
match err {
198-
LayoutError::Unknown(..) => Self::UnknownLayout,
198+
LayoutError::Unknown(..) | LayoutError::ReferencesError(..) => Self::UnknownLayout,
199199
err => unimplemented!("{:?}", err),
200200
}
201201
}

compiler/rustc_ty_utils/src/layout.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ fn layout_of_uncached<'tcx>(
9696
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
9797
ty: Ty<'tcx>,
9898
) -> Result<Layout<'tcx>, &'tcx LayoutError<'tcx>> {
99+
// Types that reference `ty::Error` pessimistically don't have a meaningful layout.
100+
// The only side-effect of this is possibly worse diagnostics in case the layout
101+
// was actually computable (like if the `ty::Error` showed up only in a `PhantomData`).
102+
if let Err(guar) = ty.error_reported() {
103+
return Err(error(cx, LayoutError::ReferencesError(guar)));
104+
}
105+
99106
let tcx = cx.tcx;
100107
let param_env = cx.param_env;
101108
let dl = cx.data_layout();
@@ -564,11 +571,15 @@ fn layout_of_uncached<'tcx>(
564571
return Err(error(cx, LayoutError::Unknown(ty)));
565572
}
566573

567-
ty::Bound(..) | ty::GeneratorWitness(..) | ty::GeneratorWitnessMIR(..) | ty::Infer(_) => {
574+
ty::Bound(..)
575+
| ty::GeneratorWitness(..)
576+
| ty::GeneratorWitnessMIR(..)
577+
| ty::Infer(_)
578+
| ty::Error(_) => {
568579
bug!("Layout::compute: unexpected type `{}`", ty)
569580
}
570581

571-
ty::Placeholder(..) | ty::Param(_) | ty::Error(_) => {
582+
ty::Placeholder(..) | ty::Param(_) => {
572583
return Err(error(cx, LayoutError::Unknown(ty)));
573584
}
574585
})

src/librustdoc/html/templates/type_layout.html

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ <h2 id="layout" class="small-section-header"> {# #}
4444
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
4545
the type was too big. {# #}
4646
</p> {# #}
47+
{% when Err(LayoutError::ReferencesError(_)) %}
48+
<p> {# #}
49+
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
50+
the type references errors. {# #}
51+
</p> {# #}
4752
{% when Err(LayoutError::NormalizationFailure(_, _)) %}
4853
<p> {# #}
4954
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// issue: 113760
2+
3+
union W { s: dyn Iterator<Item = Missing> }
4+
//~^ ERROR cannot find type `Missing` in this scope
5+
6+
static ONCE: W = todo!();
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0412]: cannot find type `Missing` in this scope
2+
--> $DIR/malformed-unsized-type-in-union.rs:3:34
3+
|
4+
LL | union W { s: dyn Iterator<Item = Missing> }
5+
| ^^^^^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)