Skip to content

Commit d5fd37f

Browse files
committed
Auto merge of rust-lang#86338 - JohnTitor:issue-86162, r=estebank
Do not suggest impl traits as type arguments Fixes rust-lang#86162
2 parents 2939249 + b84d08d commit d5fd37f

File tree

8 files changed

+72
-27
lines changed

8 files changed

+72
-27
lines changed

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

+4-16
Original file line numberDiff line numberDiff line change
@@ -753,23 +753,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
753753
if let (UnderspecifiedArgKind::Const { .. }, Some(parent_data)) =
754754
(&arg_data.kind, &arg_data.parent)
755755
{
756-
let has_impl_trait =
757-
self.tcx.generics_of(parent_data.def_id).params.iter().any(|param| {
758-
matches!(
759-
param.kind,
760-
ty::GenericParamDefKind::Type {
761-
synthetic: Some(
762-
hir::SyntheticTyParamKind::ImplTrait
763-
| hir::SyntheticTyParamKind::FromAttr,
764-
),
765-
..
766-
}
767-
)
768-
});
769-
770756
// (#83606): Do not emit a suggestion if the parent has an `impl Trait`
771757
// as an argument otherwise it will cause the E0282 error.
772-
if !has_impl_trait || self.tcx.features().explicit_generic_args_with_impl_trait {
758+
if !self.tcx.generics_of(parent_data.def_id).has_impl_trait()
759+
|| self.tcx.features().explicit_generic_args_with_impl_trait
760+
{
773761
err.span_suggestion_verbose(
774762
span,
775763
"consider specifying the const argument",
@@ -814,7 +802,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
814802
let borrow = typeck_results.borrow();
815803
if let Some((DefKind::AssocFn, did)) = borrow.type_dependent_def(e.hir_id) {
816804
let generics = self.tcx.generics_of(did);
817-
if !generics.params.is_empty() {
805+
if !generics.params.is_empty() && !generics.has_impl_trait() {
818806
err.span_suggestion_verbose(
819807
segment.ident.span.shrink_to_hi(),
820808
&format!(

compiler/rustc_middle/src/ty/generics.rs

+15
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ impl<'tcx> Generics {
198198
_ => bug!("expected const parameter, but found another generic parameter"),
199199
}
200200
}
201+
202+
/// Returns `true` if `params` has `impl Trait`.
203+
pub fn has_impl_trait(&'tcx self) -> bool {
204+
self.params.iter().any(|param| {
205+
matches!(
206+
param.kind,
207+
ty::GenericParamDefKind::Type {
208+
synthetic: Some(
209+
hir::SyntheticTyParamKind::ImplTrait | hir::SyntheticTyParamKind::FromAttr,
210+
),
211+
..
212+
}
213+
)
214+
})
215+
}
201216
}
202217

203218
/// Bounds on generics.

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
16031603
let generics = self.tcx.generics_of(*def_id);
16041604
if generics.params.iter().any(|p| p.name != kw::SelfUpper)
16051605
&& !snippet.ends_with('>')
1606+
&& !generics.has_impl_trait()
16061607
{
16071608
// FIXME: To avoid spurious suggestions in functions where type arguments
16081609
// where already supplied, we check the snippet to make sure it doesn't

compiler/rustc_typeck/src/astconv/generics.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -647,17 +647,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
647647
return false;
648648
}
649649

650-
let impl_trait = generics.params.iter().any(|param| {
651-
matches!(
652-
param.kind,
653-
ty::GenericParamDefKind::Type {
654-
synthetic: Some(
655-
hir::SyntheticTyParamKind::ImplTrait | hir::SyntheticTyParamKind::FromAttr,
656-
),
657-
..
658-
}
659-
)
660-
});
650+
let impl_trait = generics.has_impl_trait();
661651

662652
if impl_trait {
663653
let spans = seg
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test of #86162.
2+
3+
fn foo(x: impl Clone) {}
4+
fn gen<T>() -> T { todo!() }
5+
6+
fn main() {
7+
foo(gen()); //<- Do not suggest `foo::<impl Clone>()`!
8+
//~^ ERROR: type annotations needed
9+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/issue-86162-1.rs:7:5
3+
|
4+
LL | fn foo(x: impl Clone) {}
5+
| ----- required by this bound in `foo`
6+
...
7+
LL | foo(gen()); //<- Do not suggest `foo::<impl Clone>()`!
8+
| ^^^ cannot infer type for type parameter `impl Clone` declared on the function `foo`
9+
|
10+
= note: cannot satisfy `_: Clone`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0283`.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Regression test of #86162.
2+
3+
fn gen<T>() -> T { todo!() }
4+
5+
struct Foo;
6+
7+
impl Foo {
8+
fn bar(x: impl Clone) {}
9+
}
10+
11+
fn main() {
12+
Foo::bar(gen()); //<- Do not suggest `Foo::bar::<impl Clone>()`!
13+
//~^ ERROR: type annotations needed
14+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/issue-86162-2.rs:12:5
3+
|
4+
LL | fn bar(x: impl Clone) {}
5+
| ----- required by this bound in `Foo::bar`
6+
...
7+
LL | Foo::bar(gen()); //<- Do not suggest `Foo::bar::<impl Clone>()`!
8+
| ^^^^^^^^ cannot infer type for type parameter `impl Clone` declared on the associated function `bar`
9+
|
10+
= note: cannot satisfy `_: Clone`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0283`.

0 commit comments

Comments
 (0)