Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types defined with type_alias_impl_trait can be used for dispatch in trait impls. Which is unsound. #84660

Closed
steffahn opened this issue Apr 28, 2021 · 1 comment · Fixed by #95973
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-traits Area: Trait system A-typesystem Area: The type system C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Apr 28, 2021

Let’s define a type:

trait Foo {}
impl Foo for () {}
type Bar = impl Foo;
fn _defining_use() -> Bar {}

Well, a straightforward trait implementation does not work:

trait Trait {}

impl Trait for Bar {}
   Compiling playground v0.0.1 (/playground)
error: cannot implement trait on type alias impl trait
  --> src/lib.rs:10:1
   |
10 | impl Trait for Bar {}
   | ^^^^^^^^^^^^^^^^^^
   |
note: type alias impl trait defined here
  --> src/lib.rs:5:12
   |
5  | type Bar = impl Foo;
   |            ^^^^^^^^

error: aborting due to previous error

(playground)

But rustc doesn’t complain about anything slighty more complex at the moment, e.g.

trait Trait {}
impl Trait for (Bar,) {}

trait TraitArg<T> {}
impl TraitArg<Bar> for () {}

(playground)

Which turns out to be problematic:

trait TraitArg<T> {
    fn f();
}

impl TraitArg<Bar> for () {
    fn f() {
        println!("ho");
    }
}

fn main() {
    <() as TraitArg<Bar>>::f();
}
   Compiling playground v0.0.1 (/playground)
thread 'rustc' panicked at 'called `Result::unwrap()` on an `Err` value: ErrorReported', compiler/rustc_mir/src/monomorphize/collector.rs:827:84
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.53.0-nightly (727d10156 2021-04-27) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: internal compiler error: Encountered error `Unimplemented` selecting `Binder(<() as TraitArg<()>>, [])` during codegen
  |
  = note: delayed at compiler/rustc_trait_selection/src/traits/codegen.rs:68:32

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1018:13
stack backtrace:
   0:     0x7f995e14cfd0 - std::backtrace_rs::backtrace::libunwind::trace::hdcf4f90f85129e83
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x7f995e14cfd0 - std::backtrace_rs::backtrace::trace_unsynchronized::h2669e30cb82f6732
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f995e14cfd0 - std::sys_common::backtrace::_print_fmt::hfbda19e17f6db318
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7f995e14cfd0 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1a8751bf59281272
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x7f995e1be31f - core::fmt::write::h7aa6cd0067dca82a
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/core/src/fmt/mod.rs:1094:17
   5:     0x7f995e1418d5 - std::io::Write::write_fmt::hd7dd3a1df9b6befb
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/io/mod.rs:1584:15
   6:     0x7f995e150d2b - std::sys_common::backtrace::_print::h551e9ec8a9fa8106
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x7f995e150d2b - std::sys_common::backtrace::print::ha4b1c5e95fa040b3
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x7f995e150d2b - std::panicking::default_hook::{{closure}}::h0b34c9ab7fb9f857
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/panicking.rs:208:50
   9:     0x7f995e15080d - std::panicking::default_hook::h3067e8318decd17a
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/panicking.rs:225:9
  10:     0x7f995e91382d - rustc_driver::report_ice::hf065daa4796f245b
  11:     0x7f995e151440 - std::panicking::rust_panic_with_hook::h81b8facc50f34daa
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/panicking.rs:595:17
  12:     0x7f995e151017 - std::panicking::begin_panic_handler::{{closure}}::ha376ab85d95a000e
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/panicking.rs:497:13
  13:     0x7f995e14d48c - std::sys_common::backtrace::__rust_end_short_backtrace::h6795c8afdd1a77e6
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/sys_common/backtrace.rs:141:18
  14:     0x7f995e150f79 - rust_begin_unwind
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/panicking.rs:493:5
  15:     0x7f995e11cdbb - std::panicking::begin_panic_fmt::hf43a0025042538e2
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/panicking.rs:435:5
  16:     0x7f9960d9e029 - rustc_errors::HandlerInner::flush_delayed::h340bd563bba016cc
  17:     0x7f9960d9cb1b - <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop::h56e016ed68f55f61
  18:     0x7f9960406cd6 - core::ptr::drop_in_place<rustc_session::parse::ParseSess>::hf9273749d5982cb2
  19:     0x7f99604157e1 - <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop::h2bbea2bbb380f5f7
  20:     0x7f99603fba0d - core::ptr::drop_in_place<rustc_interface::interface::Compiler>::h01f06b96a4e05fdc
  21:     0x7f99603fb5e4 - rustc_span::with_source_map::hafa0f290d807fcb7
  22:     0x7f99603fd161 - rustc_interface::interface::create_compiler_and_run::h31928174eaa223b1
  23:     0x7f99604168e8 - scoped_tls::ScopedKey<T>::set::h332c2cb9377dc4c6
  24:     0x7f9960416e03 - std::sys_common::backtrace::__rust_begin_short_backtrace::h3f92abbf0f820c98
  25:     0x7f996041a625 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h3affa87e0b1d695f
  26:     0x7f995e15fe57 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3aa31cb6360b59d9
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/alloc/src/boxed.rs:1546:9
  27:     0x7f995e15fe57 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h7719d3c7c5841461
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/alloc/src/boxed.rs:1546:9
  28:     0x7f995e15fe57 - std::sys::unix::thread::Thread::new::thread_start::hfbe13ead469fd0bc
                               at /rustc/727d101561f9b1e81c6282943292d990288ca479/library/std/src/sys/unix/thread.rs:71:17
  29:     0x7f995e09c609 - start_thread
  30:     0x7f995dfb0293 - clone
  31:                0x0 - <unknown>

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.53.0-nightly (727d10156 2021-04-27) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
thread panicked while panicking. aborting.
error: could not compile `playground`

(playground)


Unsurprisingly, this is actually not only resulting in ICEs but also in unsoundness:

#![feature(min_type_alias_impl_trait)]

trait Foo {}
impl Foo for () {}
type Bar = impl Foo;
fn _defining_use() -> Bar {}

trait Trait<T, In> {
    type Out;
    fn convert(i: In) -> Self::Out;
}

impl<In, Out> Trait<Bar, In> for Out {
    type Out = Out;
    fn convert(_i: In) -> Self::Out {
        unreachable!();
    }
}

impl<In, Out> Trait<(), In> for Out {
    type Out = In;
    fn convert(i: In) -> Self::Out {
        i
    }
}

fn transmute<In, Out>(i: In) -> Out {
    <Out as Trait<Bar, In>>::convert(i)
}

fn main() {
    let d;
    {
        let x = "Hello World".to_string();
        d = transmute::<&String, &String>(&x);
    }
    println!("{}", d);
}
�@U

(playground)

@rustbot modify labels: A-typesystem, A-traits, A-impl-trait, F-type_alias_impl_trait, T-compiler, requires-nightly
and someone please add “I-unsound 💥”.

@steffahn steffahn added the C-bug Category: This is a bug. label Apr 28, 2021
@rustbot rustbot added A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-traits Area: Trait system A-typesystem Area: The type system F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 28, 2021
@steffahn steffahn changed the title Types defined with type_alias_impl_trait can be used in traits. Which is unsound. Types defined with type_alias_impl_trait can be used for dispatch in trait impls. Which is unsound. Apr 28, 2021
@rustbot rustbot removed the A-lifetimes Area: lifetime related label Apr 28, 2021
@jonas-schievink jonas-schievink added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Apr 28, 2021
@jackh726 jackh726 removed the F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` label Jun 29, 2021
@lqd
Copy link
Member

lqd commented Jun 29, 2021

just noting that this issue and #86411 are likely duplicates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-traits Area: Trait system A-typesystem Area: The type system C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
7 participants