Rollup merge of #164309 - Albab-Hasan:point-at-opaque-for-unconstrained-hidden-type, r=lcnr

point at the opaque when its hidden type cannot be inferred

the note added in https://github.com/rust-lang/rust/pull/162172 now points at the opaque definition, unless the error already points inside it.

r? @lcnr
This commit is contained in:
Jonathan Brouwer
2026-10-14 19:29:37 +02:00
committed by GitHub
14 changed files with 88 additions and 27 deletions

View File

@@ -190,19 +190,24 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
.filter_map(ty::GenericArg::as_term) .filter_map(ty::GenericArg::as_term)
.find(|term| term.is_infer()) .find(|term| term.is_infer())
.unwrap_or_else(|| hidden_type.ty.into()); .unwrap_or_else(|| hidden_type.ty.into());
self.err_ctxt() let mut err = self.err_ctxt().emit_inference_failure_err(
.emit_inference_failure_err( self.body_def_id,
self.body_def_id, hidden_type.span,
hidden_type.span, infer_var,
infer_var, TypeAnnotationNeeded::E0282,
TypeAnnotationNeeded::E0282, false,
false, );
) // The shared E0282 label only says "cannot infer type", which gives no
// 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
// hint that the ambiguity is in an opaque's hidden type rather than an // ordinary local inference failure.
// ordinary local inference failure. let msg = "cannot infer type of hidden type of opaque";
.with_note("cannot infer type of hidden type of opaque") let opaque_span = self.tcx.def_span(def_id);
.emit_err() 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()
} }
} }
}; };

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | cmp_eq LL | cmp_eq
| ^^^^^^ cannot infer type of the type parameter `A` declared on the function `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 help: consider specifying the generic arguments
| |
LL | cmp_eq::<A, B, O> LL | cmp_eq::<A, B, O>

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed for `<X as Trait<'static>>::Out<_>`
LL | let x = *x; 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 help: consider giving `x` an explicit type, where the placeholders `_` are specified
| |
LL | let x: <_ as Trait<'static>>::Out<_> = *x; LL | let x: <_ as Trait<'static>>::Out<_> = *x;

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | if false { is_trait(foo()) } else { Default::default() } LL | if false { is_trait(foo()) } else { Default::default() }
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `is_trait` | ^^^^^^^^ 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 help: consider specifying the generic arguments
| |
LL | if false { is_trait::<T, U>(foo()) } else { Default::default() } LL | if false { is_trait::<T, U>(foo()) } else { Default::default() }

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | if false { is_trait(foo()) } else { Default::default() } LL | if false { is_trait(foo()) } else { Default::default() }
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `is_trait` | ^^^^^^^^ 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 help: consider specifying the generic arguments
| |
LL | if false { is_trait::<T, U>(foo()) } else { Default::default() } LL | if false { is_trait::<T, U>(foo()) } else { Default::default() }

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed for `(_,)`
LL | let (x,) = (build(x),); 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<T>(x: T) -> impl Sized {
| ^^^^^^^^^^
help: consider giving this pattern a type, where the placeholder `_` is specified help: consider giving this pattern a type, where the placeholder `_` is specified
| |
LL | let (x,): (_,) = (build(x),); LL | let (x,): (_,) = (build(x),);
@@ -16,7 +20,11 @@ error[E0282]: type annotations needed for `((_,),)`
LL | let (x,) = (build2(x),); 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<T>(x: T) -> impl Sized {
| ^^^^^^^^^^
help: consider giving this pattern a type, where the placeholder `_` is specified help: consider giving this pattern a type, where the placeholder `_` is specified
| |
LL | let (x,): ((_,),) = (build2(x),); LL | let (x,): ((_,),) = (build2(x),);

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | fn muh(x: A) -> B { LL | fn muh(x: A) -> B {
| ^ cannot infer type | ^ 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 error: aborting due to 1 previous error

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | needs_send::<Foo>(); LL | needs_send::<Foo>();
| ^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `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 error: aborting due to 1 previous error

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | needs_send::<Foo>(); LL | needs_send::<Foo>();
| ^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `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 error: aborting due to 1 previous error

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed for `*mut _`
LL | let r = random_paulis().unwrap(); 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 help: consider giving `r` an explicit type, where the placeholder `_` is specified
| |
LL | let r: *mut _ = random_paulis().unwrap(); LL | let r: *mut _ = random_paulis().unwrap();

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | let x = <Foo as Trait<Bar>>::Assoc::default(); LL | let x = <Foo as Trait<Bar>>::Assoc::default();
| ^^^ cannot infer type | ^^^ 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 error: aborting due to 1 previous error

View File

@@ -13,7 +13,11 @@ error[E0282]: type annotations needed
LL | fn convert(_i: In) -> Self::Out { LL | fn convert(_i: In) -> Self::Out {
| ^^^^^^^^^ cannot infer type | ^^^^^^^^^ 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 error: aborting due to 2 previous errors

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | self.bar.next().unwrap(); 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<Item = ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: try using a fully qualified path to specify the expected types help: try using a fully qualified path to specify the expected types
| |
LL - self.bar.next().unwrap(); LL - self.bar.next().unwrap();

View File

@@ -4,7 +4,11 @@ error[E0282]: type annotations needed
LL | fn foo() -> impl Foo<FooX> { LL | fn foo() -> impl Foo<FooX> {
| ^^^^^^^^^^^^^^ cannot infer type | ^^^^^^^^^^^^^^ 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 error: aborting due to 1 previous error