Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve diagnostics for function passed when a type was expected. #84520

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/rustc_typeck/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
_ => {}
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/generics/generic-function-item-where-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn foo<U>() {}

fn main() {
foo::<main>()
//~^ ERROR constant provided when a type was expected
}
12 changes: 12 additions & 0 deletions src/test/ui/generics/generic-function-item-where-type.stderr
Original file line number Diff line number Diff line change
@@ -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`.
3 changes: 3 additions & 0 deletions src/test/ui/privacy/privacy-ns1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/privacy/privacy-ns2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down