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
This commit is contained in:
Dylan DPC 2022-10-05 17:27:31 +05:30 committed by GitHub
commit f8f501997a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 27 deletions

View File

@ -435,6 +435,7 @@ pub fn emit_inference_failure_err(
generics_def_id,
def_id: _,
generic_args,
have_turbofish,
} => {
let generics = self.tcx.generics_of(generics_def_id);
let is_type = matches!(arg.unpack(), GenericArgKind::Type(_));
@ -482,12 +483,14 @@ pub fn emit_inference_failure_err(
.unwrap()
.into_buffer();
if !have_turbofish {
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
span: insert_span,
arg_count: generic_args.len(),
args,
});
}
}
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, substs, def_id } => {
let printer = fmt_printer(self, Namespace::ValueNS);
let def_path = printer.print_def_path(def_id, substs).unwrap().into_buffer();
@ -616,6 +619,7 @@ enum InferSourceKind<'tcx> {
generics_def_id: DefId,
def_id: DefId,
generic_args: &'tcx [GenericArg<'tcx>],
have_turbofish: bool,
},
FullyQualifiedMethodCall {
receiver: &'tcx Expr<'tcx>,
@ -676,6 +680,7 @@ struct InsertableGenericArgs<'tcx> {
substs: SubstsRef<'tcx>,
generics_def_id: DefId,
def_id: DefId,
have_turbofish: bool,
}
/// A visitor which searches for the "best" spot to use in the inference error.
@ -916,6 +921,7 @@ fn expr_inferred_subst_iter(
substs,
generics_def_id: def_id,
def_id,
have_turbofish: false,
}
};
return Box::new(insertable.into_iter());
@ -933,6 +939,9 @@ fn resolved_path_inferred_subst_iter(
substs: SubstsRef<'tcx>,
) -> impl Iterator<Item = InsertableGenericArgs<'tcx>> + 'a {
let tcx = self.infcx.tcx;
let have_turbofish = path.segments.iter().any(|segment| {
segment.args.map_or(false, |args| args.args.iter().any(|arg| arg.is_ty_or_const()))
});
// The last segment of a path often has `Res::Err` and the
// correct `Res` is the one of the whole path.
//
@ -942,7 +951,7 @@ fn resolved_path_inferred_subst_iter(
let generics_def_id = tcx.res_generics_def_id(path.res)?;
let generics = tcx.generics_of(generics_def_id);
if generics.has_impl_trait() {
None?
None?;
}
let insert_span =
path.segments.last().unwrap().ident.span.shrink_to_hi().with_hi(path.span.hi());
@ -951,6 +960,7 @@ fn resolved_path_inferred_subst_iter(
substs,
generics_def_id,
def_id: path.res.def_id(),
have_turbofish,
}
};
@ -970,6 +980,7 @@ fn resolved_path_inferred_subst_iter(
substs,
generics_def_id,
def_id: res.def_id(),
have_turbofish,
})
})
.chain(last_segment_using_path_data)
@ -998,7 +1009,13 @@ fn path_inferred_subst_iter(
}
let span = tcx.hir().span(segment.hir_id);
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
InsertableGenericArgs { insert_span, substs, generics_def_id: def_id, def_id }
InsertableGenericArgs {
insert_span,
substs,
generics_def_id: def_id,
def_id,
have_turbofish: false,
}
};
let parent_def_id = generics.parent.unwrap();
@ -1121,7 +1138,13 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
for args in self.expr_inferred_subst_iter(expr) {
debug!(?args);
let InsertableGenericArgs { insert_span, substs, generics_def_id, def_id } = args;
let InsertableGenericArgs {
insert_span,
substs,
generics_def_id,
def_id,
have_turbofish,
} = args;
let generics = tcx.generics_of(generics_def_id);
if let Some(argument_index) = generics
.own_substs(substs)
@ -1144,6 +1167,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
generics_def_id,
def_id,
generic_args,
have_turbofish,
},
});
}

View File

@ -3,11 +3,6 @@ error[E0282]: type annotations needed
|
LL | <Struct as Ambiguous<_>>::method();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Self` declared on the trait `Ambiguous`
|
help: consider specifying the generic argument
|
LL | <Struct as Ambiguous::<_>>::method();
| ~~~~~
error[E0283]: type annotations needed
--> $DIR/concrete-impl.rs:13:5
@ -22,10 +17,6 @@ LL | impl Ambiguous<One> for Struct {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | impl Ambiguous<Two> for Struct {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying the generic argument
|
LL | <Struct as Ambiguous::<_>>::method();
| ~~~~~
error: aborting due to 2 previous errors

View File

@ -0,0 +1,11 @@
enum OhNo<T, U> {
A(T),
B(U),
C,
}
fn uwu() {
OhNo::C::<u32, _>; //~ ERROR type annotations needed
}
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0282]: type annotations needed
--> $DIR/do-not-suggest-generic-arguments-for-turbofish.rs:8:5
|
LL | OhNo::C::<u32, _>;
| ^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `U` declared on the enum `OhNo`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0282`.

View File

@ -3,11 +3,6 @@ error[E0282]: type annotations needed
|
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
| ^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `swap`
|
help: consider specifying the generic argument
|
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
| ~~~~~~~~~~
error: aborting due to previous error

View File

@ -9,11 +9,6 @@ error[E0282]: type annotations needed
|
LL | [0u8; std::mem::size_of::<_>()];
| ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `size_of`
|
help: consider specifying the generic argument
|
LL | [0u8; std::mem::size_of::<_>()];
| ~~~~~
error: aborting due to 2 previous errors