Skip to content

Commit 2433526

Browse files
committed
use delay_span_bug instead of bug! when doing layout sanity check
It's possible that `is_object_safe` is called on a trait that is ill-formed, and we shouldn't ICE unless there are no errors being raised. Using `delay_span_bug` solves this. fixes #56806
1 parent 9723a49 commit 2433526

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/librustc/traits/object_safety.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,15 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
369369

370370
match abi_of_ty(unit_receiver_ty) {
371371
&Abi::Scalar(..) => (),
372-
abi => bug!("Receiver when Self = () should have a Scalar ABI, found {:?}", abi)
372+
abi => {
373+
self.sess.delay_span_bug(
374+
self.def_span(method.def_id),
375+
&format!(
376+
"Receiver when Self = () should have a Scalar ABI, found {:?}",
377+
abi
378+
),
379+
);
380+
}
373381
}
374382

375383
let trait_object_ty = self.object_ty_for_trait(
@@ -383,10 +391,15 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
383391

384392
match abi_of_ty(trait_object_receiver) {
385393
&Abi::ScalarPair(..) => (),
386-
abi => bug!(
387-
"Receiver when Self = {} should have a ScalarPair ABI, found {:?}",
388-
trait_object_ty, abi
389-
)
394+
abi => {
395+
self.sess.delay_span_bug(
396+
self.def_span(method.def_id),
397+
&format!(
398+
"Receiver when Self = {} should have a ScalarPair ABI, found {:?}",
399+
trait_object_ty, abi
400+
),
401+
);
402+
}
390403
}
391404
}
392405
}

src/test/ui/issues/issue-56806.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub trait Trait {
2+
fn dyn_instead_of_self(self: Box<dyn Trait>);
3+
//~^ ERROR invalid method receiver type: std::boxed::Box<(dyn Trait + 'static)>
4+
}
5+
6+
pub fn main() {
7+
}

src/test/ui/issues/issue-56806.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0307]: invalid method receiver type: std::boxed::Box<(dyn Trait + 'static)>
2+
--> $DIR/issue-56806.rs:2:34
3+
|
4+
LL | fn dyn_instead_of_self(self: Box<dyn Trait>);
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: type must be `Self` or a type that dereferences to it
8+
= help: consider changing to `self`, `&self`, `&mut self`, or `self: Box<Self>`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0307`.

0 commit comments

Comments
 (0)