diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2021-04-25 23:15:16 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-25 23:15:16 +0200 |
| commit | 139749934b174cbe556d4e026a76461e9aa8d0b3 (patch) | |
| tree | 88aec737f399f4e93d62b8224fa6b222f00f246f | |
| parent | ad3389a6dfa5c239df8db7e55e4a341bdd6d5f66 (diff) | |
| parent | e558ddbb3aa8a787aac030969af7575aceed63cb (diff) | |
| download | rust-139749934b174cbe556d4e026a76461e9aa8d0b3.tar.gz rust-139749934b174cbe556d4e026a76461e9aa8d0b3.zip | |
Rollup merge of #84520 - hameerabbasi:fn-as-ty, r=lcnr
Improve diagnostics for function passed when a type was expected. This PR improves diagnostics, it provides more information when a function is passed where a type is expected. r? `@lcnr`
| -rw-r--r-- | compiler/rustc_typeck/src/astconv/generics.rs | 14 | ||||
| -rw-r--r-- | src/test/ui/generics/generic-function-item-where-type.rs | 6 | ||||
| -rw-r--r-- | src/test/ui/generics/generic-function-item-where-type.stderr | 12 | ||||
| -rw-r--r-- | src/test/ui/privacy/privacy-ns1.stderr | 3 | ||||
| -rw-r--r-- | src/test/ui/privacy/privacy-ns2.stderr | 6 |
5 files changed, 41 insertions, 0 deletions
diff --git a/compiler/rustc_typeck/src/astconv/generics.rs b/compiler/rustc_typeck/src/astconv/generics.rs index 2bbb38c294d..a3804e468da 100644 --- a/compiler/rustc_typeck/src/astconv/generics.rs +++ b/compiler/rustc_typeck/src/astconv/generics.rs @@ -109,6 +109,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { ); } } + (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 index 00000000000..e1b0578cadb --- /dev/null +++ b/src/test/ui/generics/generic-function-item-where-type.rs @@ -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 index 00000000000..88594129caa --- /dev/null +++ b/src/test/ui/generics/generic-function-item-where-type.stderr @@ -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`. diff --git a/src/test/ui/privacy/privacy-ns1.stderr b/src/test/ui/privacy/privacy-ns1.stderr index 714f28941f1..d09a8aae748 100644 --- a/src/test/ui/privacy/privacy-ns1.stderr +++ b/src/test/ui/privacy/privacy-ns1.stderr @@ -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 diff --git a/src/test/ui/privacy/privacy-ns2.stderr b/src/test/ui/privacy/privacy-ns2.stderr index c7ad8ec5036..fdf0549cf50 100644 --- a/src/test/ui/privacy/privacy-ns2.stderr +++ b/src/test/ui/privacy/privacy-ns2.stderr @@ -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 |
