Skip to content

Commit 456007a

Browse files
committed
Emit error instead of ICE when optimized MIR is missing
Closes 51388.
1 parent e877e2a commit 456007a

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

compiler/rustc_monomorphize/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ monomorphize_large_assignments =
1414
.label = value moved from here
1515
.note = The current maximum size is {$limit}, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]`
1616
17+
monomorphize_no_optimized_mir =
18+
missing optimized MIR for an item in the crate `{$crate_name}`
19+
.note = missing optimized MIR for this item (was the crate `{$crate_name}` compiled with `--emit=metadata`?)
20+
1721
monomorphize_recursion_limit =
1822
reached the recursion limit while instantiating `{$shrunk}`
1923
.note = `{$def_path_str}` defined here

compiler/rustc_monomorphize/src/collector.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ use rustc_target::abi::Size;
192192
use std::path::PathBuf;
193193

194194
use crate::errors::{
195-
EncounteredErrorWhileInstantiating, LargeAssignmentsLint, RecursionLimit, TypeLengthLimit,
195+
EncounteredErrorWhileInstantiating, LargeAssignmentsLint, NoOptimizedMir, RecursionLimit,
196+
TypeLengthLimit,
196197
};
197198

198199
#[derive(PartialEq)]
@@ -950,7 +951,10 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
950951
}
951952

952953
if !tcx.is_mir_available(def_id) {
953-
bug!("no MIR available for {:?}", def_id);
954+
tcx.sess.emit_fatal(NoOptimizedMir {
955+
span: tcx.def_span(def_id),
956+
crate_name: tcx.crate_name(def_id.krate),
957+
});
954958
}
955959

956960
true

compiler/rustc_monomorphize/src/errors.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::fluent_generated as fluent;
44
use rustc_errors::ErrorGuaranteed;
55
use rustc_errors::IntoDiagnostic;
66
use rustc_macros::{Diagnostic, LintDiagnostic};
7-
use rustc_span::Span;
7+
use rustc_span::{Span, Symbol};
88

99
#[derive(Diagnostic)]
1010
#[diag(monomorphize_recursion_limit)]
@@ -33,6 +33,14 @@ pub struct TypeLengthLimit {
3333
pub type_length: usize,
3434
}
3535

36+
#[derive(Diagnostic)]
37+
#[diag(monomorphize_no_optimized_mir)]
38+
pub struct NoOptimizedMir {
39+
#[note]
40+
pub span: Span,
41+
pub crate_name: Symbol,
42+
}
43+
3644
pub struct UnusedGenericParamsHint {
3745
pub span: Span,
3846
pub param_spans: Vec<Span>,

tests/ui/rmeta/auxiliary/rmeta-meta.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
pub struct Foo {
77
pub field: i32,
88
}
9+
10+
pub fn missing_optimized_mir() {
11+
println!("indeed");
12+
}

tests/ui/rmeta/no_optitimized_mir.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// aux-build:rmeta-meta.rs
2+
// no-prefer-dynamic
3+
// build-fail
4+
5+
// Check that we do not ICE when we need optimized MIR but it is missing.
6+
7+
extern crate rmeta_meta;
8+
9+
fn main() {
10+
rmeta_meta::missing_optimized_mir();
11+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: missing optimized MIR for an item in the crate `rmeta_meta`
2+
|
3+
note: missing optimized MIR for this item (was the crate `rmeta_meta` compiled with `--emit=metadata`?)
4+
--> $DIR/auxiliary/rmeta-meta.rs:10:1
5+
|
6+
LL | pub fn missing_optimized_mir() {
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)