Skip to content

Commit c6df564

Browse files
authored
Rollup merge of #113310 - jieyouxu:dont-suggest-impl-trait-in-paths, r=lcnr
Don't suggest `impl Trait` in path position Fixes #113264.
2 parents 3f73a7d + b5208b3 commit c6df564

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,18 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
162162
let mut infcx_inner = infcx.inner.borrow_mut();
163163
let ty_vars = infcx_inner.type_variables();
164164
let var_origin = ty_vars.var_origin(ty_vid);
165-
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) = var_origin.kind
165+
if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) = var_origin.kind
166166
&& !var_origin.span.from_expansion()
167167
{
168-
Some(name)
168+
let generics = infcx.tcx.generics_of(infcx.tcx.parent(def_id));
169+
let idx = generics.param_def_id_to_index(infcx.tcx, def_id).unwrap();
170+
let generic_param_def = generics.param_at(idx as usize, infcx.tcx);
171+
if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param_def.kind
172+
{
173+
None
174+
} else {
175+
Some(name)
176+
}
169177
} else {
170178
None
171179
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait T {}
2+
3+
struct S {}
4+
5+
impl S {
6+
fn owo(&self, _: Option<&impl T>) {}
7+
}
8+
9+
fn main() {
10+
(S {}).owo(None)
11+
//~^ ERROR type annotations needed
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-113264-incorrect-impl-trait-in-path-suggestion.rs:10:16
3+
|
4+
LL | (S {}).owo(None)
5+
| ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
6+
|
7+
help: consider specifying the generic argument
8+
|
9+
LL | (S {}).owo(None::<&_>)
10+
| ++++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)