From 97469cc4bf5d682ded196c60ee30d6a392b645aa Mon Sep 17 00:00:00 2001
From: bohan <bohan-zhang@foxmail.com>
Date: Mon, 29 Jul 2024 22:29:12 +0800
Subject: [PATCH] only accept adt type during infer source visitor

---
 .../error_reporting/infer/need_type_info.rs   |  8 ++---
 tests/crashes/121613-2.rs                     | 28 ----------------
 tests/crashes/121613.rs                       | 24 --------------
 .../incompat-call-after-qualified-path-0.rs   | 24 ++++++++++++++
 ...ncompat-call-after-qualified-path-0.stderr | 32 +++++++++++++++++++
 .../incompat-call-after-qualified-path-1.rs   | 28 ++++++++++++++++
 ...ncompat-call-after-qualified-path-1.stderr | 32 +++++++++++++++++++
 7 files changed, 120 insertions(+), 56 deletions(-)
 delete mode 100644 tests/crashes/121613-2.rs
 delete mode 100644 tests/crashes/121613.rs
 create mode 100644 tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.rs
 create mode 100644 tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.stderr
 create mode 100644 tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.rs
 create mode 100644 tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.stderr

diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs
index f667e4c80fd49..f6dd7898fb28e 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs
@@ -934,13 +934,13 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
             // which makes this somewhat difficult and prevents us from just
             // using `self.path_inferred_arg_iter` here.
             hir::ExprKind::Struct(&hir::QPath::Resolved(_self_ty, path), _, _)
-            // FIXME(TaKO8Ki): Ideally we should support this. For that
-            // we have to map back from the self type to the
-            // type alias though. That's difficult.
+            // FIXME(TaKO8Ki): Ideally we should support other kinds,
+            // such as `TyAlias` or `AssocTy`. For that we have to map
+            // back from the self type to the type alias though. That's difficult.
             //
             // See the `need_type_info/issue-103053.rs` test for
             // a example.
-            if !matches!(path.res, Res::Def(DefKind::TyAlias, _)) => {
+            if matches!(path.res, Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) => {
                 if let Some(ty) = self.opt_node_type(expr.hir_id)
                     && let ty::Adt(_, args) = ty.kind()
                 {
diff --git a/tests/crashes/121613-2.rs b/tests/crashes/121613-2.rs
deleted file mode 100644
index ddc4f37c96a2f..0000000000000
--- a/tests/crashes/121613-2.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-//@ known-bug: #121613
-fn main() {
-    // destructure through a qualified path
-    let <Foo as A>::Assoc { br } = StructStruct { br: 2 };
-    //~^ ERROR usage of qualified paths in this context is experimental
-    let _ = <Foo as A>::Assoc { br: 2 };
-    //~^ ERROR usage of qualified paths in this context is experimental
-    let <E>::V(..) = E::V(|a, b| a.cmp(b));
-    //~^ ERROR usage of qualified paths in this context is experimental
-}
-
-struct StructStruct {
-    br: i8,
-}
-
-struct Foo;
-
-trait A {
-    type Assoc;
-}
-
-impl A for Foo {
-    type Assoc = StructStruct;
-}
-
-enum E {
-    V(u8)
-}
diff --git a/tests/crashes/121613.rs b/tests/crashes/121613.rs
deleted file mode 100644
index ec9ba82a68c5f..0000000000000
--- a/tests/crashes/121613.rs
+++ /dev/null
@@ -1,24 +0,0 @@
-//@ known-bug: #121613
-fn main() {
-    let _ = <Foo as A>::Assoc { br: 2 };
-
-    let <E>::V(..) = E::V(|a, b| a.cmp(b));
-}
-
-struct StructStruct {
-    br: i8,
-}
-
-struct Foo;
-
-trait A {
-    type Assoc;
-}
-
-impl A for Foo {
-    type Assoc = StructStruct;
-}
-
-enum E {
-    V(u8),
-}
diff --git a/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.rs b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.rs
new file mode 100644
index 0000000000000..830a6390fce6d
--- /dev/null
+++ b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.rs
@@ -0,0 +1,24 @@
+// issue#121613
+
+#![feature(more_qualified_paths)]
+
+struct S {}
+
+struct Foo;
+
+trait A {
+    type Assoc;
+}
+
+impl A for Foo {
+    type Assoc = S;
+}
+
+fn f() {}
+
+fn main() {
+  <Foo as A>::Assoc {};
+  f(|a, b| a.cmp(b));
+  //~^ ERROR: type annotations needed
+  //~| ERROR: this function takes 0 arguments but 1 argument was supplied
+}
diff --git a/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.stderr b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.stderr
new file mode 100644
index 0000000000000..10056bdf3d4f4
--- /dev/null
+++ b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-0.stderr
@@ -0,0 +1,32 @@
+error[E0282]: type annotations needed
+  --> $DIR/incompat-call-after-qualified-path-0.rs:21:6
+   |
+LL |   f(|a, b| a.cmp(b));
+   |      ^     - type must be known at this point
+   |
+help: consider giving this closure parameter an explicit type
+   |
+LL |   f(|a: /* Type */, b| a.cmp(b));
+   |       ++++++++++++
+
+error[E0061]: this function takes 0 arguments but 1 argument was supplied
+  --> $DIR/incompat-call-after-qualified-path-0.rs:21:3
+   |
+LL |   f(|a, b| a.cmp(b));
+   |   ^ --------------- unexpected argument
+   |
+note: function defined here
+  --> $DIR/incompat-call-after-qualified-path-0.rs:17:4
+   |
+LL | fn f() {}
+   |    ^
+help: remove the extra argument
+   |
+LL -   f(|a, b| a.cmp(b));
+LL +   f();
+   |
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0061, E0282.
+For more information about an error, try `rustc --explain E0061`.
diff --git a/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.rs b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.rs
new file mode 100644
index 0000000000000..6b786332a8f43
--- /dev/null
+++ b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.rs
@@ -0,0 +1,28 @@
+// issue#121613
+
+#![feature(more_qualified_paths)]
+
+struct S<T> {
+    a: T
+}
+
+struct Foo;
+
+trait A {
+    type Assoc<T>;
+}
+
+impl A for Foo {
+    type Assoc<T> = S<T>;
+}
+
+fn f() {}
+
+fn main() {
+  <Foo as A>::Assoc::<i32> {
+    a: 1
+  };
+  f(|a, b| a.cmp(b));
+  //~^ ERROR: type annotations needed
+  //~| ERROR: this function takes 0 arguments but 1 argument was supplied
+}
diff --git a/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.stderr b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.stderr
new file mode 100644
index 0000000000000..632a9b99f84ef
--- /dev/null
+++ b/tests/ui/inference/need_type_info/incompat-call-after-qualified-path-1.stderr
@@ -0,0 +1,32 @@
+error[E0282]: type annotations needed
+  --> $DIR/incompat-call-after-qualified-path-1.rs:25:6
+   |
+LL |   f(|a, b| a.cmp(b));
+   |      ^     - type must be known at this point
+   |
+help: consider giving this closure parameter an explicit type
+   |
+LL |   f(|a: /* Type */, b| a.cmp(b));
+   |       ++++++++++++
+
+error[E0061]: this function takes 0 arguments but 1 argument was supplied
+  --> $DIR/incompat-call-after-qualified-path-1.rs:25:3
+   |
+LL |   f(|a, b| a.cmp(b));
+   |   ^ --------------- unexpected argument
+   |
+note: function defined here
+  --> $DIR/incompat-call-after-qualified-path-1.rs:19:4
+   |
+LL | fn f() {}
+   |    ^
+help: remove the extra argument
+   |
+LL -   f(|a, b| a.cmp(b));
+LL +   f();
+   |
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0061, E0282.
+For more information about an error, try `rustc --explain E0061`.