Skip to content

Commit 71bc112

Browse files
committed
Use delay_span_bug in validate-mir.
1 parent 27050c0 commit 71bc112

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use rustc_infer::traits::{Obligation, ObligationCause};
1111
use rustc_middle::mir::coverage::CoverageKind;
1212
use rustc_middle::mir::visit::{MutatingUseContext, NonUseContext, PlaceContext, Visitor};
1313
use rustc_middle::mir::*;
14+
use rustc_middle::span_bug;
1415
use rustc_middle::ty::adjustment::PointerCoercion;
1516
use rustc_middle::ty::print::with_no_trimmed_paths;
1617
use rustc_middle::ty::{
1718
self, CoroutineArgsExt, InstanceKind, ScalarInt, Ty, TyCtxt, TypeVisitableExt, Upcast, Variance,
1819
};
19-
use rustc_middle::{bug, span_bug};
2020
use rustc_mir_dataflow::debuginfo::debuginfo_locals;
2121
use rustc_trait_selection::traits::ObligationCtxt;
2222

@@ -122,18 +122,17 @@ struct CfgChecker<'a, 'tcx> {
122122

123123
impl<'a, 'tcx> CfgChecker<'a, 'tcx> {
124124
#[track_caller]
125-
fn fail(&self, location: Location, msg: impl AsRef<str>) {
125+
fn fail(&self, location: Location, msg: impl std::fmt::Display) {
126126
// We might see broken MIR when other errors have already occurred.
127-
if self.tcx.dcx().has_errors().is_none() {
128-
span_bug!(
129-
self.body.source_info(location).span,
127+
// But we may have some cases of errors happening *after* MIR construction,
128+
// for instance because of generic constants or coroutines.
129+
self.tcx.dcx().span_delayed_bug(
130+
self.body.source_info(location).span,
131+
format!(
130132
"broken MIR in {:?} ({}) at {:?}:\n{}",
131-
self.body.source.instance,
132-
self.when,
133-
location,
134-
msg.as_ref(),
135-
);
136-
}
133+
self.body.source.instance, self.when, location, msg,
134+
),
135+
);
137136
}
138137

139138
fn check_edge(&mut self, location: Location, bb: BasicBlock, edge_kind: EdgeKind) {
@@ -1637,7 +1636,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16371636
ty::Int(int) => int.normalize(target_width).bit_width().unwrap(),
16381637
ty::Char => 32,
16391638
ty::Bool => 1,
1640-
other => bug!("unhandled type: {:?}", other),
1639+
other => {
1640+
self.fail(location, format!("unhandled type in SwitchInt {other:?}"));
1641+
// Magic number to avoid ICEing.
1642+
1
1643+
}
16411644
});
16421645

16431646
for (value, _) in targets.iter() {

tests/crashes/140850.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ known-bug: #140850
22
//@ compile-flags: -Zvalidate-mir
3-
fn A() -> impl {
3+
fn A() -> impl Copy {
44
while A() {}
55
loop {}
66
}

tests/crashes/137916.rs renamed to tests/ui/coroutine/non-send-dyn-send-ice.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
//@ known-bug: #137916
1+
//! Regression test for ICE #137916
22
//@ edition: 2021
3+
//@ compile-flags: -Zvalidate-mir
4+
35
use std::ptr::null;
46

57
async fn a() -> Box<dyn Send> {
6-
Box::new(async {
8+
Box::new(async { //~ ERROR future cannot be sent between threads safely
79
let non_send = null::<()>();
810
&non_send;
911
async {}.await
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: future cannot be sent between threads safely
2+
--> $DIR/non-send-dyn-send-ice.rs:8:5
3+
|
4+
LL | / Box::new(async {
5+
LL | | let non_send = null::<()>();
6+
LL | | &non_send;
7+
LL | | async {}.await
8+
LL | | })
9+
| |______^ future created by async block is not `Send`
10+
|
11+
= help: within `{async block@$DIR/non-send-dyn-send-ice.rs:8:14: 8:19}`, the trait `Send` is not implemented for `*const ()`
12+
note: future is not `Send` as this value is used across an await
13+
--> $DIR/non-send-dyn-send-ice.rs:11:18
14+
|
15+
LL | let non_send = null::<()>();
16+
| -------- has type `*const ()` which is not `Send`
17+
LL | &non_send;
18+
LL | async {}.await
19+
| ^^^^^ await occurs here, with `non_send` maybe used later
20+
= note: required for the cast from `Box<{async block@$DIR/non-send-dyn-send-ice.rs:8:14: 8:19}>` to `Box<dyn Send>`
21+
22+
error: aborting due to 1 previous error
23+

tests/crashes/126680.rs renamed to tests/ui/type-alias-impl-trait/branch-closure-parameter.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
//@ known-bug: rust-lang/rust#126680
1+
//! Regression test for #126680
22
//@ compile-flags: -Zvalidate-mir
3+
34
#![feature(type_alias_impl_trait)]
45
type Bar = impl std::fmt::Display;
56

@@ -10,7 +11,7 @@ struct A {
1011
}
1112

1213
#[define_opaque(Bar)]
13-
fn foo() -> A {
14+
fn foo() -> A { //~ ERROR item does not constrain `Bar::{opaque#0}`
1415
A {
1516
func: |check, b| {
1617
if check {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: item does not constrain `Bar::{opaque#0}`
2+
--> $DIR/branch-closure-parameter.rs:14:4
3+
|
4+
LL | fn foo() -> A {
5+
| ^^^
6+
|
7+
= note: consider removing `#[define_opaque]` or adding an empty `#[define_opaque()]`
8+
note: this opaque type is supposed to be constrained
9+
--> $DIR/branch-closure-parameter.rs:5:12
10+
|
11+
LL | type Bar = impl std::fmt::Display;
12+
| ^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)