about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-09-05 07:15:15 +0200
committerGitHub <noreply@github.com>2023-09-05 07:15:15 +0200
commita73c663ec4b1adc3cf9f82604c8cb552d3b593b6 (patch)
tree488da6ce4b19b7be53765b46788449fba11c851d
parent9c609ae158aab27f1e7df446de6f2c09414012a9 (diff)
parent456007af125d3822285135659ea5ea0b67084b66 (diff)
downloadrust-a73c663ec4b1adc3cf9f82604c8cb552d3b593b6.tar.gz
rust-a73c663ec4b1adc3cf9f82604c8cb552d3b593b6.zip
Rollup merge of #115353 - Enselic:no-optimized-mir, r=oli-obk
Emit error instead of ICE when optimized MIR is missing

Closes #51388
-rw-r--r--compiler/rustc_monomorphize/messages.ftl4
-rw-r--r--compiler/rustc_monomorphize/src/collector.rs8
-rw-r--r--compiler/rustc_monomorphize/src/errors.rs10
-rw-r--r--tests/ui/rmeta/auxiliary/rmeta-meta.rs4
-rw-r--r--tests/ui/rmeta/no_optitimized_mir.rs11
-rw-r--r--tests/ui/rmeta/no_optitimized_mir.stderr10
6 files changed, 44 insertions, 3 deletions
diff --git a/compiler/rustc_monomorphize/messages.ftl b/compiler/rustc_monomorphize/messages.ftl
index fdd47e6f79b..2b7d9bd3413 100644
--- a/compiler/rustc_monomorphize/messages.ftl
+++ b/compiler/rustc_monomorphize/messages.ftl
@@ -14,6 +14,10 @@ monomorphize_large_assignments =
     .label = value moved from here
     .note = The current maximum size is {$limit}, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]`
 
+monomorphize_no_optimized_mir =
+    missing optimized MIR for an item in the crate `{$crate_name}`
+    .note = missing optimized MIR for this item (was the crate `{$crate_name}` compiled with `--emit=metadata`?)
+
 monomorphize_recursion_limit =
     reached the recursion limit while instantiating `{$shrunk}`
     .note = `{$def_path_str}` defined here
diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs
index bf88360a8c1..36cc0204dff 100644
--- a/compiler/rustc_monomorphize/src/collector.rs
+++ b/compiler/rustc_monomorphize/src/collector.rs
@@ -192,7 +192,8 @@ use rustc_target::abi::Size;
 use std::path::PathBuf;
 
 use crate::errors::{
-    EncounteredErrorWhileInstantiating, LargeAssignmentsLint, RecursionLimit, TypeLengthLimit,
+    EncounteredErrorWhileInstantiating, LargeAssignmentsLint, NoOptimizedMir, RecursionLimit,
+    TypeLengthLimit,
 };
 
 #[derive(PartialEq)]
@@ -960,7 +961,10 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
     }
 
     if !tcx.is_mir_available(def_id) {
-        bug!("no MIR available for {:?}", def_id);
+        tcx.sess.emit_fatal(NoOptimizedMir {
+            span: tcx.def_span(def_id),
+            crate_name: tcx.crate_name(def_id.krate),
+        });
     }
 
     true
diff --git a/compiler/rustc_monomorphize/src/errors.rs b/compiler/rustc_monomorphize/src/errors.rs
index 495a73490a2..fdcc95f137f 100644
--- a/compiler/rustc_monomorphize/src/errors.rs
+++ b/compiler/rustc_monomorphize/src/errors.rs
@@ -4,7 +4,7 @@ use crate::fluent_generated as fluent;
 use rustc_errors::ErrorGuaranteed;
 use rustc_errors::IntoDiagnostic;
 use rustc_macros::{Diagnostic, LintDiagnostic};
-use rustc_span::Span;
+use rustc_span::{Span, Symbol};
 
 #[derive(Diagnostic)]
 #[diag(monomorphize_recursion_limit)]
@@ -33,6 +33,14 @@ pub struct TypeLengthLimit {
     pub type_length: usize,
 }
 
+#[derive(Diagnostic)]
+#[diag(monomorphize_no_optimized_mir)]
+pub struct NoOptimizedMir {
+    #[note]
+    pub span: Span,
+    pub crate_name: Symbol,
+}
+
 pub struct UnusedGenericParamsHint {
     pub span: Span,
     pub param_spans: Vec<Span>,
diff --git a/tests/ui/rmeta/auxiliary/rmeta-meta.rs b/tests/ui/rmeta/auxiliary/rmeta-meta.rs
index 6d8ed95bd38..6d435049527 100644
--- a/tests/ui/rmeta/auxiliary/rmeta-meta.rs
+++ b/tests/ui/rmeta/auxiliary/rmeta-meta.rs
@@ -6,3 +6,7 @@
 pub struct Foo {
     pub field: i32,
 }
+
+pub fn missing_optimized_mir() {
+    println!("indeed");
+}
diff --git a/tests/ui/rmeta/no_optitimized_mir.rs b/tests/ui/rmeta/no_optitimized_mir.rs
new file mode 100644
index 00000000000..c503005f16b
--- /dev/null
+++ b/tests/ui/rmeta/no_optitimized_mir.rs
@@ -0,0 +1,11 @@
+// aux-build:rmeta-meta.rs
+// no-prefer-dynamic
+// build-fail
+
+// Check that we do not ICE when we need optimized MIR but it is missing.
+
+extern crate rmeta_meta;
+
+fn main() {
+    rmeta_meta::missing_optimized_mir();
+}
diff --git a/tests/ui/rmeta/no_optitimized_mir.stderr b/tests/ui/rmeta/no_optitimized_mir.stderr
new file mode 100644
index 00000000000..a17024c5310
--- /dev/null
+++ b/tests/ui/rmeta/no_optitimized_mir.stderr
@@ -0,0 +1,10 @@
+error: missing optimized MIR for an item in the crate `rmeta_meta`
+   |
+note: missing optimized MIR for this item (was the crate `rmeta_meta` compiled with `--emit=metadata`?)
+  --> $DIR/auxiliary/rmeta-meta.rs:10:1
+   |
+LL | pub fn missing_optimized_mir() {
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to previous error
+