Skip to content

Commit e558ddb

Browse files
committed
Improve diagnostics for function passed when a type was expected.
1 parent e11a9fa commit e558ddb

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

compiler/rustc_typeck/src/astconv/generics.rs

+14
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
109109
);
110110
}
111111
}
112+
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
113+
let body = tcx.hir().body(cnst.value.body);
114+
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) =
115+
body.value.kind
116+
{
117+
if let Res::Def(DefKind::Fn { .. }, id) = path.res {
118+
err.help(&format!(
119+
"`{}` is a function item, not a type",
120+
tcx.item_name(id)
121+
));
122+
err.help("function item types cannot be named directly");
123+
}
124+
}
125+
}
112126
_ => {}
113127
}
114128

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn foo<U>() {}
2+
3+
fn main() {
4+
foo::<main>()
5+
//~^ ERROR constant provided when a type was expected
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0747]: constant provided when a type was expected
2+
--> $DIR/generic-function-item-where-type.rs:4:11
3+
|
4+
LL | foo::<main>()
5+
| ^^^^
6+
|
7+
= help: `main` is a function item, not a type
8+
= help: function item types cannot be named directly
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0747`.

src/test/ui/privacy/privacy-ns1.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ error[E0747]: constant provided when a type was expected
5757
|
5858
LL | let _x: Box<Bar>;
5959
| ^^^
60+
|
61+
= help: `Bar` is a function item, not a type
62+
= help: function item types cannot be named directly
6063

6164
error: aborting due to 4 previous errors
6265

src/test/ui/privacy/privacy-ns2.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,18 @@ error[E0747]: constant provided when a type was expected
8383
|
8484
LL | let _x : Box<Bar>;
8585
| ^^^
86+
|
87+
= help: `Bar` is a function item, not a type
88+
= help: function item types cannot be named directly
8689

8790
error[E0747]: constant provided when a type was expected
8891
--> $DIR/privacy-ns2.rs:48:17
8992
|
9093
LL | let _x: Box<Bar>;
9194
| ^^^
95+
|
96+
= help: `Bar` is a function item, not a type
97+
= help: function item types cannot be named directly
9298

9399
error: aborting due to 8 previous errors
94100

0 commit comments

Comments
 (0)