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`
This commit is contained in:
Dylan DPC 2021-04-25 23:15:16 +02:00 committed by GitHub
commit 139749934b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 0 deletions

View File

@ -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");
}
}
}
_ => {}
}

View File

@ -0,0 +1,6 @@
fn foo<U>() {}
fn main() {
foo::<main>()
//~^ ERROR constant provided when a type was expected
}

View File

@ -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`.

View 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

View 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