diff --git a/compiler/rustc_hir_typeck/src/opaque_types.rs b/compiler/rustc_hir_typeck/src/opaque_types.rs index c96144d8499..da9162629ec 100644 --- a/compiler/rustc_hir_typeck/src/opaque_types.rs +++ b/compiler/rustc_hir_typeck/src/opaque_types.rs @@ -190,19 +190,24 @@ impl<'tcx> FnCtxt<'_, 'tcx> { .filter_map(ty::GenericArg::as_term) .find(|term| term.is_infer()) .unwrap_or_else(|| hidden_type.ty.into()); - self.err_ctxt() - .emit_inference_failure_err( - self.body_def_id, - hidden_type.span, - infer_var, - TypeAnnotationNeeded::E0282, - false, - ) - // The shared E0282 label only says "cannot infer type", which gives no - // hint that the ambiguity is in an opaque's hidden type rather than an - // ordinary local inference failure. - .with_note("cannot infer type of hidden type of opaque") - .emit_err() + let mut err = self.err_ctxt().emit_inference_failure_err( + self.body_def_id, + hidden_type.span, + infer_var, + TypeAnnotationNeeded::E0282, + false, + ); + // The shared E0282 label only says "cannot infer type", which gives no + // hint that the ambiguity is in an opaque's hidden type rather than an + // ordinary local inference failure. + let msg = "cannot infer type of hidden type of opaque"; + let opaque_span = self.tcx.def_span(def_id); + if err.span.primary_span().is_some_and(|sp| opaque_span.contains(sp)) { + err.note(msg); + } else { + err.span_note(opaque_span, msg); + } + err.emit_err() } } }; diff --git a/tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.next.stderr b/tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.next.stderr index d2ed88c1125..d7be74ee825 100644 --- a/tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.next.stderr +++ b/tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | cmp_eq | ^^^^^^ cannot infer type of the type parameter `A` declared on the function `cmp_eq` | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/ambig-hr-projection-issue-93340.rs:15:6 + | +LL | ) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider specifying the generic arguments | LL | cmp_eq:: diff --git a/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr index cd1f2aa14ac..24c412caf24 100644 --- a/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr +++ b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed for `>::Out<_>` LL | let x = *x; | ^ | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/norm-before-method-resolution-opaque-type.rs:18:12 + | +LL | type Foo = impl Sized; + | ^^^^^^^^^^ help: consider giving `x` an explicit type, where the placeholders `_` are specified | LL | let x: <_ as Trait<'static>>::Out<_> = *x; diff --git a/tests/ui/impl-trait/auto-trait-selection-freeze.next.stderr b/tests/ui/impl-trait/auto-trait-selection-freeze.next.stderr index 4f08d635cab..88ff9bd3201 100644 --- a/tests/ui/impl-trait/auto-trait-selection-freeze.next.stderr +++ b/tests/ui/impl-trait/auto-trait-selection-freeze.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | if false { is_trait(foo()) } else { Default::default() } | ^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `is_trait` | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/auto-trait-selection-freeze.rs:19:13 + | +LL | fn foo() -> impl Sized { + | ^^^^^^^^^^ help: consider specifying the generic arguments | LL | if false { is_trait::(foo()) } else { Default::default() } diff --git a/tests/ui/impl-trait/auto-trait-selection.next.stderr b/tests/ui/impl-trait/auto-trait-selection.next.stderr index dbad5680781..daf2f9a4617 100644 --- a/tests/ui/impl-trait/auto-trait-selection.next.stderr +++ b/tests/ui/impl-trait/auto-trait-selection.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | if false { is_trait(foo()) } else { Default::default() } | ^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `is_trait` | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/auto-trait-selection.rs:15:13 + | +LL | fn foo() -> impl Sized { + | ^^^^^^^^^^ help: consider specifying the generic arguments | LL | if false { is_trait::(foo()) } else { Default::default() } diff --git a/tests/ui/impl-trait/recursive-in-exhaustiveness.next.stderr b/tests/ui/impl-trait/recursive-in-exhaustiveness.next.stderr index 878e10557f2..63e53bd75bd 100644 --- a/tests/ui/impl-trait/recursive-in-exhaustiveness.next.stderr +++ b/tests/ui/impl-trait/recursive-in-exhaustiveness.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed for `(_,)` LL | let (x,) = (build(x),); | ^^^^ | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/recursive-in-exhaustiveness.rs:17:22 + | +LL | fn build(x: T) -> impl Sized { + | ^^^^^^^^^^ help: consider giving this pattern a type, where the placeholder `_` is specified | LL | let (x,): (_,) = (build(x),); @@ -16,7 +20,11 @@ error[E0282]: type annotations needed for `((_,),)` LL | let (x,) = (build2(x),); | ^^^^ | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/recursive-in-exhaustiveness.rs:27:23 + | +LL | fn build2(x: T) -> impl Sized { + | ^^^^^^^^^^ help: consider giving this pattern a type, where the placeholder `_` is specified | LL | let (x,): ((_,),) = (build2(x),); diff --git a/tests/ui/impl-trait/two_tait_defining_each_other2.next.stderr b/tests/ui/impl-trait/two_tait_defining_each_other2.next.stderr index a2d25fafb2a..488ad507f04 100644 --- a/tests/ui/impl-trait/two_tait_defining_each_other2.next.stderr +++ b/tests/ui/impl-trait/two_tait_defining_each_other2.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | fn muh(x: A) -> B { | ^ cannot infer type | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/two_tait_defining_each_other2.rs:6:10 + | +LL | type A = impl Foo; + | ^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.is_send.stderr b/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.is_send.stderr index fc285abc9fe..a2d31d4d51f 100644 --- a/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.is_send.stderr +++ b/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.is_send.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | needs_send::(); | ^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `needs_send` | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/dont-type_of-tait-in-defining-scope.rs:7:12 + | +LL | type Foo = impl Send; + | ^^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.not_send.stderr b/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.not_send.stderr index fc285abc9fe..5af23f405e0 100644 --- a/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.not_send.stderr +++ b/tests/ui/traits/next-solver/opaques/dont-type_of-tait-in-defining-scope.not_send.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | needs_send::(); | ^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `needs_send` | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/dont-type_of-tait-in-defining-scope.rs:10:12 + | +LL | type Foo = impl Sized; + | ^^^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/traits/next-solver/opaques/recursive-hidden-type-canonicalization.stderr b/tests/ui/traits/next-solver/opaques/recursive-hidden-type-canonicalization.stderr index d6fc9584ef5..3e931b93f5d 100644 --- a/tests/ui/traits/next-solver/opaques/recursive-hidden-type-canonicalization.stderr +++ b/tests/ui/traits/next-solver/opaques/recursive-hidden-type-canonicalization.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed for `*mut _` LL | let r = random_paulis().unwrap(); | ^ | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/recursive-hidden-type-canonicalization.rs:18:35 + | +LL | fn random_paulis() -> Option<*mut impl Sized> { + | ^^^^^^^^^^ help: consider giving `r` an explicit type, where the placeholder `_` is specified | LL | let r: *mut _ = random_paulis().unwrap(); diff --git a/tests/ui/type-alias-impl-trait/constrain_in_projection2.next.stderr b/tests/ui/type-alias-impl-trait/constrain_in_projection2.next.stderr index 3862479fee3..d91b4490abe 100644 --- a/tests/ui/type-alias-impl-trait/constrain_in_projection2.next.stderr +++ b/tests/ui/type-alias-impl-trait/constrain_in_projection2.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | let x = >::Assoc::default(); | ^^^ cannot infer type | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/constrain_in_projection2.rs:12:12 + | +LL | type Bar = impl Sized; + | ^^^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/type-alias-impl-trait/issue-84660-unsoundness.next.stderr b/tests/ui/type-alias-impl-trait/issue-84660-unsoundness.next.stderr index d025a821d1b..2836a83ea76 100644 --- a/tests/ui/type-alias-impl-trait/issue-84660-unsoundness.next.stderr +++ b/tests/ui/type-alias-impl-trait/issue-84660-unsoundness.next.stderr @@ -13,7 +13,11 @@ error[E0282]: type annotations needed LL | fn convert(_i: In) -> Self::Out { | ^^^^^^^^^ cannot infer type | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/issue-84660-unsoundness.rs:12:12 + | +LL | type Bar = impl Foo; + | ^^^^^^^^ error: aborting due to 2 previous errors diff --git a/tests/ui/type-alias-impl-trait/method_resolution_trait_method_from_opaque.next.stderr b/tests/ui/type-alias-impl-trait/method_resolution_trait_method_from_opaque.next.stderr index a2e5b3cc972..81b929b3704 100644 --- a/tests/ui/type-alias-impl-trait/method_resolution_trait_method_from_opaque.next.stderr +++ b/tests/ui/type-alias-impl-trait/method_resolution_trait_method_from_opaque.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | self.bar.next().unwrap(); | ^^^^ | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/method_resolution_trait_method_from_opaque.rs:18:13 + | +LL | type Tait = impl Iterator; + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try using a fully qualified path to specify the expected types | LL - self.bar.next().unwrap(); diff --git a/tests/ui/type-alias-impl-trait/nested-tait-inference2.next.stderr b/tests/ui/type-alias-impl-trait/nested-tait-inference2.next.stderr index 9e8857201e7..fac807b5bc3 100644 --- a/tests/ui/type-alias-impl-trait/nested-tait-inference2.next.stderr +++ b/tests/ui/type-alias-impl-trait/nested-tait-inference2.next.stderr @@ -4,7 +4,11 @@ error[E0282]: type annotations needed LL | fn foo() -> impl Foo { | ^^^^^^^^^^^^^^ cannot infer type | - = note: cannot infer type of hidden type of opaque +note: cannot infer type of hidden type of opaque + --> $DIR/nested-tait-inference2.rs:10:13 + | +LL | type FooX = impl Debug; + | ^^^^^^^^^^ error: aborting due to 1 previous error