rust/src/librustc/traits
kennytm 6c58b842d6
Rollup merge of #57229 - mikeyhew:fix-56806, r=varkor
Fix #56806 by using `delay_span_bug` in object safety layout sanity checks

It's possible that `is_object_safe` is called on a trait method that with an invalid receiver type. This caused an ICE in #56806, because `receiver_is_dispatchable` returns `true` for `self: Box<dyn Trait>`, which causes one of the layout sanity checks in object_safety.rs to fail. Replacing `bug!` with `delay_span_bug` solves this.

The fact that `receiver_is_dispatchable` returns `true` here could be considered a bug. It passes the check that the method implements, though: `Box<dyn Trait>` implements `DispatchFromDyn<Box<dyn Trait>>` because `dyn Trait` implements `Unsize<dyn Trait>`. It would be good to hear what @eddyb and @nikomatsakis think.

Note that I only added a test for the case encountered in #56806. I could not come up with a case that triggered an ICE from the other check, `bug!("receiver when Self = dyn Trait should be ScalarPair, found Scalar")`. There is no way, to my knowledge, that you can make `receiver_is_dispatchable` return true but still have a `Scalar` ABI when `Self = dyn Trait`.

One other case I encountered while debugging #56806 was that if you have a type parameter `T` that implements `Deref<Target=Self>` and `DispatchFromDyn<T>`, and use it as a method receiver, it will cause an ICE during `is_object_safe` because `T` has no layout ([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d9b7497b3be0ca8382fa7d9497263214)):

```rust
trait Trait<T: Deref<Target=Self> + DispatchFromDyn<T>> {
    fn foo(self: T) -> dyn Trait<T>;
}
```

I don't intend to remove the ICE there because it is a pathological case, especially since there is no way to implement `DispatchFromDyn<T>` for `T` — the checks in typeck/coherence/builtin.rs do not allow that.

fixes #56806
r? @varkor
2019-01-05 23:56:44 +08:00
..
codegen Remove licenses 2018-12-25 21:08:33 -07:00
query Auto merge of #56723 - oli-obk:lazy_const, r=nikomatsakis 2019-01-04 17:01:24 +00:00
specialize implement a hack to make traitobject 0.1.0 compile 2019-01-04 01:50:35 +02:00
auto_trait.rs universe transition 2019-01-02 17:35:05 -05:00
chalk_fulfill.rs Integrate chalk engine 2018-12-27 19:21:16 +01:00
coherence.rs Auto merge of #56837 - arielb1:nonprincipal-trait-objects, r=nikomatsakis 2019-01-05 03:36:31 +00:00
engine.rs Integrate chalk engine 2018-12-27 19:21:16 +01:00
error_reporting.rs Auto merge of #56837 - arielb1:nonprincipal-trait-objects, r=nikomatsakis 2019-01-05 03:36:31 +00:00
fulfill.rs universe transition 2019-01-02 17:35:05 -05:00
mod.rs implement a hack to make traitobject 0.1.0 compile 2019-01-04 01:50:35 +02:00
object_safety.rs Rollup merge of #57229 - mikeyhew:fix-56806, r=varkor 2019-01-05 23:56:44 +08:00
on_unimplemented.rs Various changes to string format diagnostics 2018-12-26 14:29:43 -08:00
project.rs Auto merge of #56723 - oli-obk:lazy_const, r=nikomatsakis 2019-01-04 17:01:24 +00:00
select.rs implement a hack to make traitobject 0.1.0 compile 2019-01-04 01:50:35 +02:00
structural_impls.rs Handle sub-typing in chalk-engine 2018-12-27 19:21:16 +01:00
util.rs Remove licenses 2018-12-25 21:08:33 -07:00