Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d5fd37f

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

8 files changed

+72-27Lines changed: 72 additions & 27 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
+4-16Lines changed: 4 additions & 16 deletions
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!(
Collapse file

‎compiler/rustc_middle/src/ty/generics.rs‎

Copy file name to clipboardExpand all lines: compiler/rustc_middle/src/ty/generics.rs
+15Lines changed: 15 additions & 0 deletions
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.
Collapse file

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

Copy file name to clipboardExpand all lines: compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
+1Lines changed: 1 addition & 0 deletions
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
Collapse file

‎compiler/rustc_typeck/src/astconv/generics.rs‎

Copy file name to clipboardExpand all lines: compiler/rustc_typeck/src/astconv/generics.rs
+1-11Lines changed: 1 addition & 11 deletions
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
Collapse file
+9Lines changed: 9 additions & 0 deletions
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+
}
Collapse file
+14Lines changed: 14 additions & 0 deletions
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`.
Collapse file
+14Lines changed: 14 additions & 0 deletions
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+
}
Collapse file
+14Lines changed: 14 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.