]> git.lizzy.rs Git - rust.git/commitdiff
Improve diagnostics for function passed when a type was expected.
authorHameer Abbasi <einstein.edison@gmail.com>
Sat, 24 Apr 2021 14:25:36 +0000 (16:25 +0200)
committerHameer Abbasi <einstein.edison@gmail.com>
Sun, 25 Apr 2021 06:56:50 +0000 (08:56 +0200)
compiler/rustc_typeck/src/astconv/generics.rs
src/test/ui/generics/generic-function-item-where-type.rs [new file with mode: 0644]
src/test/ui/generics/generic-function-item-where-type.stderr [new file with mode: 0644]
src/test/ui/privacy/privacy-ns1.stderr
src/test/ui/privacy/privacy-ns2.stderr

index 7a297f2c65f13fb68df8c90867fbaea8c079fecf..748704988627ff7b510394af7e6dab0f8d53055d 100644 (file)
@@ -109,6 +109,20 @@ fn generic_arg_mismatch_err(
                     );
                 }
             }
+            (GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
+                let body = tcx.hir().body(cnst.value.body);
+                if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) =
+                    body.value.kind
+                {
+                    if let Res::Def(DefKind::Fn { .. }, id) = path.res {
+                        err.help(&format!(
+                            "`{}` is a function item, not a type",
+                            tcx.item_name(id)
+                        ));
+                        err.help("function item types cannot be named directly");
+                    }
+                }
+            }
             _ => {}
         }
 
diff --git a/src/test/ui/generics/generic-function-item-where-type.rs b/src/test/ui/generics/generic-function-item-where-type.rs
new file mode 100644 (file)
index 0000000..e1b0578
--- /dev/null
@@ -0,0 +1,6 @@
+fn foo<U>() {}
+
+fn main() {
+    foo::<main>()
+    //~^ ERROR constant provided when a type was expected
+}
diff --git a/src/test/ui/generics/generic-function-item-where-type.stderr b/src/test/ui/generics/generic-function-item-where-type.stderr
new file mode 100644 (file)
index 0000000..8859412
--- /dev/null
@@ -0,0 +1,12 @@
+error[E0747]: constant provided when a type was expected
+  --> $DIR/generic-function-item-where-type.rs:4:11
+   |
+LL |     foo::<main>()
+   |           ^^^^
+   |
+   = help: `main` is a function item, not a type
+   = help: function item types cannot be named directly
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0747`.
index 714f28941f11ff49f188e2bd954079a09fed1a03..d09a8aae7480108eee0c477cc6e7493bdbd71d3c 100644 (file)
@@ -57,6 +57,9 @@ error[E0747]: constant provided when a type was expected
    |
 LL |     let _x: Box<Bar>;
    |                 ^^^
+   |
+   = help: `Bar` is a function item, not a type
+   = help: function item types cannot be named directly
 
 error: aborting due to 4 previous errors
 
index c7ad8ec503654c7e0b378840200705977fffeaf4..fdf0549cf50bc5fd5dcf4318b9a1f7a28d581fb1 100644 (file)
@@ -83,12 +83,18 @@ error[E0747]: constant provided when a type was expected
    |
 LL |     let _x : Box<Bar>;
    |                  ^^^
+   |
+   = help: `Bar` is a function item, not a type
+   = help: function item types cannot be named directly
 
 error[E0747]: constant provided when a type was expected
   --> $DIR/privacy-ns2.rs:48:17
    |
 LL |     let _x: Box<Bar>;
    |                 ^^^
+   |
+   = help: `Bar` is a function item, not a type
+   = help: function item types cannot be named directly
 
 error: aborting due to 8 previous errors