From 0919d0714ec49773d3a4c3a589b7651655b1b7c0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 24 Jul 2024 17:19:41 -0400 Subject: [PATCH] Don't ICE when auto trait has assoc ty in old solver --- .../src/traits/project.rs | 6 +++ tests/crashes/117829-2.rs | 14 ------- tests/crashes/117829.rs | 9 ----- tests/ui/auto-traits/assoc-ty.current.stderr | 40 +++++++++++++++++++ tests/ui/auto-traits/assoc-ty.next.stderr | 40 +++++++++++++++++++ tests/ui/auto-traits/assoc-ty.rs | 17 ++++++++ 6 files changed, 103 insertions(+), 23 deletions(-) delete mode 100644 tests/crashes/117829-2.rs delete mode 100644 tests/crashes/117829.rs create mode 100644 tests/ui/auto-traits/assoc-ty.current.stderr create mode 100644 tests/ui/auto-traits/assoc-ty.next.stderr create mode 100644 tests/ui/auto-traits/assoc-ty.rs diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 1d7a0515044..b96e0c8a977 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -1202,6 +1202,12 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( false } } + } else if tcx.trait_is_auto(trait_ref.def_id) { + tcx.dcx().span_delayed_bug( + tcx.def_span(obligation.predicate.def_id), + "associated types not allowed on auto traits", + ); + false } else { bug!("unexpected builtin trait with associated type: {trait_ref:?}") } diff --git a/tests/crashes/117829-2.rs b/tests/crashes/117829-2.rs deleted file mode 100644 index ecfd3148569..00000000000 --- a/tests/crashes/117829-2.rs +++ /dev/null @@ -1,14 +0,0 @@ -//@ known-bug: #117829 -#![feature(auto_traits)] - -trait B {} - -auto trait Z -where - T: Z, - >::W: B, -{ - type W; -} - -fn main() {} diff --git a/tests/crashes/117829.rs b/tests/crashes/117829.rs deleted file mode 100644 index 7544b5ec0fc..00000000000 --- a/tests/crashes/117829.rs +++ /dev/null @@ -1,9 +0,0 @@ -//@ known-bug: #117829 -auto trait Z<'a, T: ?Sized> -where - T: Z<'a, u16>, - - for<'b> >::W: Clone, -{ - type W: ?Sized; -} diff --git a/tests/ui/auto-traits/assoc-ty.current.stderr b/tests/ui/auto-traits/assoc-ty.current.stderr new file mode 100644 index 00000000000..77a1c8fb654 --- /dev/null +++ b/tests/ui/auto-traits/assoc-ty.current.stderr @@ -0,0 +1,40 @@ +error[E0380]: auto traits cannot have associated items + --> $DIR/assoc-ty.rs:10:10 + | +LL | auto trait Trait { + | ----- auto traits cannot have associated items +LL | +LL | type Output; + | -----^^^^^^- help: remove these associated items + +error[E0658]: auto traits are experimental and possibly buggy + --> $DIR/assoc-ty.rs:8:1 + | +LL | / auto trait Trait { +LL | | +LL | | type Output; +LL | | +LL | | } + | |_^ + | + = note: see issue #13231 for more information + = help: add `#![feature(auto_traits)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0308]: mismatched types + --> $DIR/assoc-ty.rs:15:36 + | +LL | let _: <() as Trait>::Output = (); + | --------------------- ^^ expected associated type, found `()` + | | + | expected due to this + | + = note: expected associated type `<() as Trait>::Output` + found unit type `()` + = help: consider constraining the associated type `<() as Trait>::Output` to `()` or calling a method that returns `<() as Trait>::Output` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0308, E0380, E0658. +For more information about an error, try `rustc --explain E0308`. diff --git a/tests/ui/auto-traits/assoc-ty.next.stderr b/tests/ui/auto-traits/assoc-ty.next.stderr new file mode 100644 index 00000000000..b9f56d6c99c --- /dev/null +++ b/tests/ui/auto-traits/assoc-ty.next.stderr @@ -0,0 +1,40 @@ +error[E0380]: auto traits cannot have associated items + --> $DIR/assoc-ty.rs:10:10 + | +LL | auto trait Trait { + | ----- auto traits cannot have associated items +LL | +LL | type Output; + | -----^^^^^^- help: remove these associated items + +error[E0658]: auto traits are experimental and possibly buggy + --> $DIR/assoc-ty.rs:8:1 + | +LL | / auto trait Trait { +LL | | +LL | | type Output; +LL | | +LL | | } + | |_^ + | + = note: see issue #13231 for more information + = help: add `#![feature(auto_traits)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0308]: mismatched types + --> $DIR/assoc-ty.rs:15:36 + | +LL | let _: <() as Trait>::Output = (); + | --------------------- ^^ types differ + | | + | expected due to this + | + = note: expected associated type `<() as Trait>::Output` + found unit type `()` + = help: consider constraining the associated type `<() as Trait>::Output` to `()` or calling a method that returns `<() as Trait>::Output` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0308, E0380, E0658. +For more information about an error, try `rustc --explain E0308`. diff --git a/tests/ui/auto-traits/assoc-ty.rs b/tests/ui/auto-traits/assoc-ty.rs new file mode 100644 index 00000000000..ada75147f6e --- /dev/null +++ b/tests/ui/auto-traits/assoc-ty.rs @@ -0,0 +1,17 @@ +//@ revisions: current next +//@[next] compile-flags: -Znext-solver +//@ ignore-compare-mode-next-solver (explicit revisions) + +// Tests that projection doesn't explode if we accidentally +// put an associated type on an auto trait. + +auto trait Trait { + //~^ ERROR auto traits are experimental and possibly buggy + type Output; + //~^ ERROR auto traits cannot have associated items +} + +fn main() { + let _: <() as Trait>::Output = (); + //~^ ERROR mismatched types +}