Skip to content

Commit f8f5019

Browse files
authored
Rollup merge of #100986 - TaKO8Ki:do-not-suggest-adding-generic-args-for-turbofish, r=compiler-errors
Stop suggesting adding generic args for turbofish Fixes #100137
2 parents dd8c3a8 + 4525796 commit f8f5019

File tree

6 files changed

+52
-27
lines changed

6 files changed

+52
-27
lines changed

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

+32-8
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
435435
generics_def_id,
436436
def_id: _,
437437
generic_args,
438+
have_turbofish,
438439
} => {
439440
let generics = self.tcx.generics_of(generics_def_id);
440441
let is_type = matches!(arg.unpack(), GenericArgKind::Type(_));
@@ -482,11 +483,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
482483
.unwrap()
483484
.into_buffer();
484485

485-
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
486-
span: insert_span,
487-
arg_count: generic_args.len(),
488-
args,
489-
});
486+
if !have_turbofish {
487+
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
488+
span: insert_span,
489+
arg_count: generic_args.len(),
490+
args,
491+
});
492+
}
490493
}
491494
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, substs, def_id } => {
492495
let printer = fmt_printer(self, Namespace::ValueNS);
@@ -616,6 +619,7 @@ enum InferSourceKind<'tcx> {
616619
generics_def_id: DefId,
617620
def_id: DefId,
618621
generic_args: &'tcx [GenericArg<'tcx>],
622+
have_turbofish: bool,
619623
},
620624
FullyQualifiedMethodCall {
621625
receiver: &'tcx Expr<'tcx>,
@@ -676,6 +680,7 @@ struct InsertableGenericArgs<'tcx> {
676680
substs: SubstsRef<'tcx>,
677681
generics_def_id: DefId,
678682
def_id: DefId,
683+
have_turbofish: bool,
679684
}
680685

681686
/// A visitor which searches for the "best" spot to use in the inference error.
@@ -916,6 +921,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
916921
substs,
917922
generics_def_id: def_id,
918923
def_id,
924+
have_turbofish: false,
919925
}
920926
};
921927
return Box::new(insertable.into_iter());
@@ -933,6 +939,9 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
933939
substs: SubstsRef<'tcx>,
934940
) -> impl Iterator<Item = InsertableGenericArgs<'tcx>> + 'a {
935941
let tcx = self.infcx.tcx;
942+
let have_turbofish = path.segments.iter().any(|segment| {
943+
segment.args.map_or(false, |args| args.args.iter().any(|arg| arg.is_ty_or_const()))
944+
});
936945
// The last segment of a path often has `Res::Err` and the
937946
// correct `Res` is the one of the whole path.
938947
//
@@ -942,7 +951,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
942951
let generics_def_id = tcx.res_generics_def_id(path.res)?;
943952
let generics = tcx.generics_of(generics_def_id);
944953
if generics.has_impl_trait() {
945-
None?
954+
None?;
946955
}
947956
let insert_span =
948957
path.segments.last().unwrap().ident.span.shrink_to_hi().with_hi(path.span.hi());
@@ -951,6 +960,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
951960
substs,
952961
generics_def_id,
953962
def_id: path.res.def_id(),
963+
have_turbofish,
954964
}
955965
};
956966

@@ -970,6 +980,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
970980
substs,
971981
generics_def_id,
972982
def_id: res.def_id(),
983+
have_turbofish,
973984
})
974985
})
975986
.chain(last_segment_using_path_data)
@@ -998,7 +1009,13 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
9981009
}
9991010
let span = tcx.hir().span(segment.hir_id);
10001011
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
1001-
InsertableGenericArgs { insert_span, substs, generics_def_id: def_id, def_id }
1012+
InsertableGenericArgs {
1013+
insert_span,
1014+
substs,
1015+
generics_def_id: def_id,
1016+
def_id,
1017+
have_turbofish: false,
1018+
}
10021019
};
10031020

10041021
let parent_def_id = generics.parent.unwrap();
@@ -1121,7 +1138,13 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
11211138

11221139
for args in self.expr_inferred_subst_iter(expr) {
11231140
debug!(?args);
1124-
let InsertableGenericArgs { insert_span, substs, generics_def_id, def_id } = args;
1141+
let InsertableGenericArgs {
1142+
insert_span,
1143+
substs,
1144+
generics_def_id,
1145+
def_id,
1146+
have_turbofish,
1147+
} = args;
11251148
let generics = tcx.generics_of(generics_def_id);
11261149
if let Some(argument_index) = generics
11271150
.own_substs(substs)
@@ -1144,6 +1167,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
11441167
generics_def_id,
11451168
def_id,
11461169
generic_args,
1170+
have_turbofish,
11471171
},
11481172
});
11491173
}

src/test/ui/inference/need_type_info/concrete-impl.stderr

-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ error[E0282]: type annotations needed
33
|
44
LL | <Struct as Ambiguous<_>>::method();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Self` declared on the trait `Ambiguous`
6-
|
7-
help: consider specifying the generic argument
8-
|
9-
LL | <Struct as Ambiguous::<_>>::method();
10-
| ~~~~~
116

127
error[E0283]: type annotations needed
138
--> $DIR/concrete-impl.rs:13:5
@@ -22,10 +17,6 @@ LL | impl Ambiguous<One> for Struct {}
2217
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2318
LL | impl Ambiguous<Two> for Struct {}
2419
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25-
help: consider specifying the generic argument
26-
|
27-
LL | <Struct as Ambiguous::<_>>::method();
28-
| ~~~~~
2920

3021
error: aborting due to 2 previous errors
3122

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
enum OhNo<T, U> {
2+
A(T),
3+
B(U),
4+
C,
5+
}
6+
7+
fn uwu() {
8+
OhNo::C::<u32, _>; //~ ERROR type annotations needed
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/do-not-suggest-generic-arguments-for-turbofish.rs:8:5
3+
|
4+
LL | OhNo::C::<u32, _>;
5+
| ^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `U` declared on the enum `OhNo`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.

src/test/ui/issues/issue-24013.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ error[E0282]: type annotations needed
33
|
44
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
55
| ^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `swap`
6-
|
7-
help: consider specifying the generic argument
8-
|
9-
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
10-
| ~~~~~~~~~~
116

127
error: aborting due to previous error
138

src/test/ui/issues/issue-47486.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ error[E0282]: type annotations needed
99
|
1010
LL | [0u8; std::mem::size_of::<_>()];
1111
| ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `size_of`
12-
|
13-
help: consider specifying the generic argument
14-
|
15-
LL | [0u8; std::mem::size_of::<_>()];
16-
| ~~~~~
1712

1813
error: aborting due to 2 previous errors
1914

0 commit comments

Comments
 (0)