Skip to content

Commit

Permalink
Avoid follow up "type annotations needed" errors in case there were a…
Browse files Browse the repository at this point in the history
…lready coherence errors for the same trait
  • Loading branch information
oli-obk committed Feb 1, 2024
1 parent ad42fa1 commit 08e7773
Show file tree
Hide file tree
Showing 65 changed files with 135 additions and 1,120 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,11 @@ fn check_impl<'tcx>(
// therefore don't need to be WF (the trait's `Self: Trait` predicate
// won't hold).
let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
if let Err(guar) = tcx.ensure().coherent_trait(trait_ref.def_id) {
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
// other `Foo` impls are incoherent.
wfcx.infcx.set_tainted_by_errors(guar);
}
let trait_ref = wfcx.normalize(
ast_trait_ref.path.span,
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/async-await/in-trait/coherence-constrained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ trait Foo {
struct Bar;

impl Foo for Bar {
//~^ ERROR type annotations needed
type T = ();
//~^ ERROR type annotations needed

Expand All @@ -22,7 +21,6 @@ impl Foo for Bar {

impl Foo for Bar {
//~^ ERROR conflicting implementations of trait `Foo` for type `Bar`
//~| ERROR type annotations needed
type T = ();
//~^ ERROR type annotations needed

Expand Down
48 changes: 9 additions & 39 deletions tests/ui/async-await/in-trait/coherence-constrained.stderr
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
error[E0284]: type annotations needed: cannot satisfy `<Bar as Foo>::T == ()`
--> $DIR/coherence-constrained.rs:18:5
--> $DIR/coherence-constrained.rs:17:5
|
LL | async fn foo(&self) {}
| ^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Bar as Foo>::T == ()`

error[E0284]: type annotations needed: cannot satisfy `<Bar as Foo>::T == ()`
--> $DIR/coherence-constrained.rs:29:5
--> $DIR/coherence-constrained.rs:27:5
|
LL | async fn foo(&self) {}
| ^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Bar as Foo>::T == ()`

error[E0119]: conflicting implementations of trait `Foo` for type `Bar`
--> $DIR/coherence-constrained.rs:23:1
--> $DIR/coherence-constrained.rs:22:1
|
LL | impl Foo for Bar {
| ---------------- first implementation here
...
LL | impl Foo for Bar {
| ^^^^^^^^^^^^^^^^ conflicting implementation for `Bar`

error[E0283]: type annotations needed: cannot satisfy `Bar: Foo`
--> $DIR/coherence-constrained.rs:13:14
|
LL | impl Foo for Bar {
| ^^^
|
note: multiple `impl`s satisfying `Bar: Foo` found
--> $DIR/coherence-constrained.rs:13:1
|
LL | impl Foo for Bar {
| ^^^^^^^^^^^^^^^^
...
LL | impl Foo for Bar {
| ^^^^^^^^^^^^^^^^

error[E0284]: type annotations needed
--> $DIR/coherence-constrained.rs:15:14
--> $DIR/coherence-constrained.rs:14:14
|
LL | type T = ();
| ^^ cannot infer type
|
= note: cannot satisfy `<Bar as Foo>::T == _`

error[E0284]: type annotations needed: cannot satisfy `<Bar as Foo>::{opaque#0}<'_> == impl Future<Output = ()>`
--> $DIR/coherence-constrained.rs:18:5
--> $DIR/coherence-constrained.rs:17:5
|
LL | async fn foo(&self) {}
| ^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Bar as Foo>::{opaque#0}<'_> == impl Future<Output = ()>`
Expand All @@ -56,31 +41,16 @@ LL | async fn foo(&self) -> Self::T;
|
= note: cannot satisfy `<Bar as Foo>::{opaque#0}<'_> == _`

error[E0283]: type annotations needed: cannot satisfy `Bar: Foo`
--> $DIR/coherence-constrained.rs:23:14
|
LL | impl Foo for Bar {
| ^^^
|
note: multiple `impl`s satisfying `Bar: Foo` found
--> $DIR/coherence-constrained.rs:13:1
|
LL | impl Foo for Bar {
| ^^^^^^^^^^^^^^^^
...
LL | impl Foo for Bar {
| ^^^^^^^^^^^^^^^^

error[E0284]: type annotations needed
--> $DIR/coherence-constrained.rs:26:14
--> $DIR/coherence-constrained.rs:24:14
|
LL | type T = ();
| ^^ cannot infer type
|
= note: cannot satisfy `<Bar as Foo>::T == _`

error[E0284]: type annotations needed: cannot satisfy `<Bar as Foo>::{opaque#0}<'_> == impl Future<Output = ()>`
--> $DIR/coherence-constrained.rs:29:5
--> $DIR/coherence-constrained.rs:27:5
|
LL | async fn foo(&self) {}
| ^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Bar as Foo>::{opaque#0}<'_> == impl Future<Output = ()>`
Expand All @@ -94,7 +64,7 @@ LL | async fn foo(&self) -> Self::T;
= note: cannot satisfy `<Bar as Foo>::{opaque#0}<'_> == _`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 11 previous errors
error: aborting due to 9 previous errors

Some errors have detailed explanations: E0119, E0283, E0284.
Some errors have detailed explanations: E0119, E0284.
For more information about an error, try `rustc --explain E0119`.
3 changes: 0 additions & 3 deletions tests/ui/async-await/issue-67651.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ trait From {
}

impl From for () {
//~^ ERROR type annotations needed
fn from() {}
}

impl From for () {
//~^ ERROR conflicting implementations of trait
//~| ERROR type annotations needed
fn from() {}
}

fn bar() -> impl core::future::Future<Output = ()> {
Expand Down
38 changes: 4 additions & 34 deletions tests/ui/async-await/issue-67651.stderr
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
error[E0119]: conflicting implementations of trait `From` for type `()`
--> $DIR/issue-67651.rs:12:1
--> $DIR/issue-67651.rs:11:1
|
LL | impl From for () {
| ---------------- first implementation here
...
LL | impl From for () {
| ^^^^^^^^^^^^^^^^ conflicting implementation for `()`

error[E0283]: type annotations needed: cannot satisfy `(): From`
--> $DIR/issue-67651.rs:7:15
|
LL | impl From for () {
| ^^
|
note: multiple `impl`s satisfying `(): From` found
--> $DIR/issue-67651.rs:7:1
|
LL | impl From for () {
| ^^^^^^^^^^^^^^^^
...
LL | impl From for () {
| ^^^^^^^^^^^^^^^^

error[E0283]: type annotations needed: cannot satisfy `(): From`
--> $DIR/issue-67651.rs:12:15
|
LL | impl From for () {
| ^^
|
note: multiple `impl`s satisfying `(): From` found
--> $DIR/issue-67651.rs:7:1
|
LL | impl From for () {
| ^^^^^^^^^^^^^^^^
...
LL | impl From for () {
| ^^^^^^^^^^^^^^^^

error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
--> $DIR/issue-67651.rs:19:18
--> $DIR/issue-67651.rs:16:18
|
LL | fn from();
| ---------- `From::from` defined here
Expand All @@ -51,7 +21,7 @@ help: use a fully-qualified path to a specific available implementation
LL | async move { </* self type */ as From>::from() }
| +++++++++++++++++++ +

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0119, E0283, E0790.
Some errors have detailed explanations: E0119, E0790.
For more information about an error, try `rustc --explain E0119`.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ impl Go for MyThingy {

impl GoMut for MyThingy {
//~^ ERROR E0119
//~| ERROR E0283
fn go_mut(&mut self, arg: isize) { }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ LL | impl GoMut for MyThingy {
- impl<G> GoMut for G
where G: Go;

error[E0283]: type annotations needed: cannot satisfy `MyThingy: GoMut`
--> $DIR/coherence-blanket-conflicts-with-specific-cross-crate.rs:15:16
|
LL | impl GoMut for MyThingy {
| ^^^^^^^^
|
note: multiple `impl`s satisfying `MyThingy: GoMut` found
--> $DIR/coherence-blanket-conflicts-with-specific-cross-crate.rs:15:1
|
LL | impl GoMut for MyThingy {
| ^^^^^^^^^^^^^^^^^^^^^^^
= note: and another `impl` found in the `go_trait` crate:
- impl<G> GoMut for G
where G: Go;

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0119, E0283.
For more information about an error, try `rustc --explain E0119`.
For more information about this error, try `rustc --explain E0119`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ struct MyType {

impl MyTrait<MyType> for MyType {
//~^ ERROR E0119
//~| ERROR type annotations needed
fn get(&self) -> usize { (*self).clone() }
//~^ ERROR incompatible type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,8 @@ LL | impl<T> MyTrait<T> for T {
LL | impl MyTrait<MyType> for MyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `MyType`

error[E0283]: type annotations needed: cannot satisfy `MyType: MyTrait<MyType>`
--> $DIR/coherence-blanket-conflicts-with-specific-multidispatch.rs:22:26
|
LL | impl MyTrait<MyType> for MyType {
| ^^^^^^
|
note: multiple `impl`s satisfying `MyType: MyTrait<MyType>` found
--> $DIR/coherence-blanket-conflicts-with-specific-multidispatch.rs:11:1
|
LL | impl<T> MyTrait<T> for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | impl MyTrait<MyType> for MyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0053]: method `get` has an incompatible type for trait
--> $DIR/coherence-blanket-conflicts-with-specific-multidispatch.rs:25:22
--> $DIR/coherence-blanket-conflicts-with-specific-multidispatch.rs:24:22
|
LL | fn get(&self) -> usize { (*self).clone() }
| ^^^^^
Expand All @@ -39,7 +24,7 @@ LL | fn get(&self) -> T;
= note: expected signature `fn(&MyType) -> MyType`
found signature `fn(&MyType) -> usize`

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0053, E0119, E0283.
Some errors have detailed explanations: E0053, E0119.
For more information about an error, try `rustc --explain E0053`.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ struct MyType {

impl MyTrait for MyType {
//~^ ERROR E0119
//~| ERROR type annotations needed
fn get(&self) -> usize { self.dummy }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ LL | impl<T:OtherTrait> MyTrait for T {
LL | impl MyTrait for MyType {
| ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `MyType`

error[E0283]: type annotations needed: cannot satisfy `MyType: MyTrait`
--> $DIR/coherence-blanket-conflicts-with-specific-trait.rs:20:18
|
LL | impl MyTrait for MyType {
| ^^^^^^
|
note: multiple `impl`s satisfying `MyType: MyTrait` found
--> $DIR/coherence-blanket-conflicts-with-specific-trait.rs:12:1
|
LL | impl<T:OtherTrait> MyTrait for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | impl MyTrait for MyType {
| ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0119, E0283.
For more information about an error, try `rustc --explain E0119`.
For more information about this error, try `rustc --explain E0119`.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct MyType {

impl MyTrait for MyType {
//~^ ERROR E0119
//~| ERROR type annotations needed
fn get(&self) -> usize { self.dummy }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ LL | impl<T> MyTrait for T {
LL | impl MyTrait for MyType {
| ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `MyType`

error[E0283]: type annotations needed: cannot satisfy `MyType: MyTrait`
--> $DIR/coherence-blanket-conflicts-with-specific.rs:19:18
|
LL | impl MyTrait for MyType {
| ^^^^^^
|
note: multiple `impl`s satisfying `MyType: MyTrait` found
--> $DIR/coherence-blanket-conflicts-with-specific.rs:11:1
|
LL | impl<T> MyTrait for T {
| ^^^^^^^^^^^^^^^^^^^^^
...
LL | impl MyTrait for MyType {
| ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0119, E0283.
For more information about an error, try `rustc --explain E0119`.
For more information about this error, try `rustc --explain E0119`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ trait MyTrait {}
struct TestType<T>(::std::marker::PhantomData<T>);

unsafe impl<T: MyTrait + 'static> Send for TestType<T> {}
//~^ ERROR: type annotations needed

impl<T: MyTrait> !Send for TestType<T> {} //~ ERROR found both positive and negative implementation

Expand Down
Loading

0 comments on commit 08e7773

Please sign in to comment.