about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlcnr <rust@lcnr.de>2022-06-03 20:26:20 +0200
committerlcnr <rust@lcnr.de>2022-06-08 10:13:02 +0200
commitf8e73ede831727ed1808d245b381703c0142b9ba (patch)
treec2819420b372e45b7c68edae469a1b7f0b0cc8f5
parent2ea468e386230375229c68a09affcba23cd79487 (diff)
downloadrust-f8e73ede831727ed1808d245b381703c0142b9ba.tar.gz
rust-f8e73ede831727ed1808d245b381703c0142b9ba.zip
need_type_info: don't ICE when detected ty alias
fixes #97698
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs21
-rw-r--r--src/test/ui/inference/need_type_info/type-alias-indirect.rs18
-rw-r--r--src/test/ui/inference/need_type_info/type-alias-indirect.stderr9
-rw-r--r--src/test/ui/inference/need_type_info/type-alias.rs36
-rw-r--r--src/test/ui/inference/need_type_info/type-alias.stderr15
5 files changed, 93 insertions, 6 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
index 7c4477b6172..40b73eb670c 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
@@ -2,6 +2,7 @@ use crate::infer::type_variable::TypeVariableOriginKind;
 use crate::infer::InferCtxt;
 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
 use rustc_hir as hir;
+use rustc_hir::def::Res;
 use rustc_hir::def::{CtorOf, DefKind, Namespace};
 use rustc_hir::def_id::DefId;
 use rustc_hir::intravisit::{self, Visitor};
@@ -853,12 +854,20 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
                             hir::TyKind::Path(hir::QPath::Resolved(_self_ty, path)),
                         ) => {
                             if tcx.res_generics_def_id(path.res) != Some(def.did()) {
-                                bug!(
-                                    "unexpected path: def={:?} substs={:?} path={:?}",
-                                    def,
-                                    substs,
-                                    path,
-                                );
+                                match path.res {
+                                    Res::Def(DefKind::TyAlias, _) => {
+                                        // FIXME: Ideally we should support this. For that
+                                        // we have to map back from the self type to the
+                                        // type alias though. That's difficult.
+                                        //
+                                        // See the `need_type_info/type-alias.rs` test for
+                                        // some examples.
+                                    }
+                                    _ => warn!(
+                                        "unexpected path: def={:?} substs={:?} path={:?}",
+                                        def, substs, path,
+                                    ),
+                                }
                             } else {
                                 return Box::new(
                                     self.resolved_path_inferred_subst_iter(path, substs)
diff --git a/src/test/ui/inference/need_type_info/type-alias-indirect.rs b/src/test/ui/inference/need_type_info/type-alias-indirect.rs
new file mode 100644
index 00000000000..0ed02ddc5f3
--- /dev/null
+++ b/src/test/ui/inference/need_type_info/type-alias-indirect.rs
@@ -0,0 +1,18 @@
+// An addition to the `type-alias.rs` test,
+// see the FIXME in that file for why this test
+// exists.
+//
+// If there is none, feel free to remove this test
+// again.
+struct Ty<T>(T);
+impl<T> Ty<T> {
+    fn new() {}
+}
+
+type IndirectAlias<T> = Ty<Box<T>>;
+fn indirect_alias() {
+    IndirectAlias::new();
+    //~^ ERROR type annotations needed
+}
+
+fn main() {}
diff --git a/src/test/ui/inference/need_type_info/type-alias-indirect.stderr b/src/test/ui/inference/need_type_info/type-alias-indirect.stderr
new file mode 100644
index 00000000000..6161690df50
--- /dev/null
+++ b/src/test/ui/inference/need_type_info/type-alias-indirect.stderr
@@ -0,0 +1,9 @@
+error[E0282]: type annotations needed
+  --> $DIR/type-alias-indirect.rs:14:5
+   |
+LL |     IndirectAlias::new();
+   |     ^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `IndirectAlias`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.
diff --git a/src/test/ui/inference/need_type_info/type-alias.rs b/src/test/ui/inference/need_type_info/type-alias.rs
new file mode 100644
index 00000000000..f921b046b6c
--- /dev/null
+++ b/src/test/ui/inference/need_type_info/type-alias.rs
@@ -0,0 +1,36 @@
+// Test the inference errors in case the relevant path
+// uses a type alias.
+//
+// Regression test for #97698.
+struct Ty<T>(T);
+impl<T> Ty<T> {
+    fn new() {}
+}
+
+type DirectAlias<T> = Ty<T>;
+fn direct_alias() {
+    DirectAlias::new()
+    //~^ ERROR type annotations needed
+}
+
+type IndirectAlias<T> = Ty<Box<T>>;
+fn indirect_alias() {
+    IndirectAlias::new();
+    // FIXME: This should also emit an error.
+    //
+    // Added it separately as `type-alias-indirect.rs`
+    // where it does error.
+}
+
+struct TyDefault<T, U = u32>(T, U);
+impl<T> TyDefault<T> {
+    fn new() {}
+}
+
+type DirectButWithDefaultAlias<T> = TyDefault<T>;
+fn direct_but_with_default_alias() {
+    DirectButWithDefaultAlias::new();
+    //~^ ERROR type annotations needed
+}
+
+fn main() {}
diff --git a/src/test/ui/inference/need_type_info/type-alias.stderr b/src/test/ui/inference/need_type_info/type-alias.stderr
new file mode 100644
index 00000000000..a33f49baf54
--- /dev/null
+++ b/src/test/ui/inference/need_type_info/type-alias.stderr
@@ -0,0 +1,15 @@
+error[E0282]: type annotations needed
+  --> $DIR/type-alias.rs:12:5
+   |
+LL |     DirectAlias::new()
+   |     ^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
+
+error[E0282]: type annotations needed
+  --> $DIR/type-alias.rs:32:5
+   |
+LL |     DirectButWithDefaultAlias::new();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0282`.