Skip to content

Commit fe8c4ed

Browse files
committed
Avoid emitting trait bound errors of incoherent traits
1 parent 11f32b7 commit fe8c4ed

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23652365
return e;
23662366
}
23672367

2368+
if let Err(guar) = self.tcx.ensure().coherent_trait(trait_ref.def_id()) {
2369+
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
2370+
// other `Foo` impls are incoherent.
2371+
return guar;
2372+
}
2373+
23682374
// This is kind of a hack: it frequently happens that some earlier
23692375
// error prevents types from being fully inferred, and then we get
23702376
// a bunch of uninteresting errors saying something like "<generic
@@ -2667,6 +2673,14 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
26672673
if let Some(e) = self.tainted_by_errors() {
26682674
return e;
26692675
}
2676+
2677+
if let Err(guar) =
2678+
self.tcx.ensure().coherent_trait(self.tcx.parent(data.projection_ty.def_id))
2679+
{
2680+
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
2681+
// other `Foo` impls are incoherent.
2682+
return guar;
2683+
}
26702684
let subst = data
26712685
.projection_ty
26722686
.args

tests/ui/async-await/in-trait/coherence-constrained.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ impl Foo for Bar {
1212
type T = ();
1313

1414
async fn foo(&self) {}
15-
//~^ ERROR type annotations needed: cannot satisfy `<Bar as Foo>::T == ()`
1615
}
1716

1817
impl Foo for Bar {
1918
//~^ ERROR conflicting implementations of trait `Foo` for type `Bar`
2019
type T = ();
2120

2221
async fn foo(&self) {}
23-
//~^ ERROR type annotations needed: cannot satisfy `<Bar as Foo>::T == ()`
2422
}
2523

2624
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
error[E0284]: type annotations needed: cannot satisfy `<Bar as Foo>::T == ()`
2-
--> $DIR/coherence-constrained.rs:14:5
3-
|
4-
LL | async fn foo(&self) {}
5-
| ^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Bar as Foo>::T == ()`
6-
7-
error[E0284]: type annotations needed: cannot satisfy `<Bar as Foo>::T == ()`
8-
--> $DIR/coherence-constrained.rs:22:5
9-
|
10-
LL | async fn foo(&self) {}
11-
| ^^^^^^^^^^^^^^^^^^^ cannot satisfy `<Bar as Foo>::T == ()`
12-
131
error[E0119]: conflicting implementations of trait `Foo` for type `Bar`
14-
--> $DIR/coherence-constrained.rs:18:1
2+
--> $DIR/coherence-constrained.rs:17:1
153
|
164
LL | impl Foo for Bar {
175
| ---------------- first implementation here
186
...
197
LL | impl Foo for Bar {
208
| ^^^^^^^^^^^^^^^^ conflicting implementation for `Bar`
219

22-
error: aborting due to 3 previous errors
10+
error: aborting due to 1 previous error
2311

24-
Some errors have detailed explanations: E0119, E0284.
25-
For more information about an error, try `rustc --explain E0119`.
12+
For more information about this error, try `rustc --explain E0119`.

0 commit comments

Comments
 (0)