Skip to content

Commit 14e6947

Browse files
committed
Correct the polymorphic extern fn error for const parameters
1 parent 4894123 commit 14e6947

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,20 +1511,39 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item) {
15111511
} else {
15121512
for item in &m.items {
15131513
let generics = tcx.generics_of(tcx.hir().local_def_id(item.hir_id));
1514-
if generics.params.len() - generics.own_counts().lifetimes != 0 {
1514+
let own_counts = generics.own_counts();
1515+
if generics.params.len() - own_counts.lifetimes != 0 {
1516+
let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts) {
1517+
(_, 0) => ("type", "types", Some("u32")),
1518+
// We don't specify an example value, because we can't generate
1519+
// a valid value for any type.
1520+
(0, _) => ("const", "consts", None),
1521+
_ => ("type or const", "types or consts", None),
1522+
};
15151523
let mut err = struct_span_err!(
15161524
tcx.sess,
15171525
item.span,
15181526
E0044,
1519-
"foreign items may not have type parameters"
1527+
"foreign items may not have {} parameters",
1528+
kinds,
1529+
);
1530+
err.span_label(
1531+
item.span,
1532+
&format!("can't have {} parameters", kinds),
15201533
);
1521-
err.span_label(item.span, "can't have type parameters");
15221534
// FIXME: once we start storing spans for type arguments, turn this into a
15231535
// suggestion.
1524-
err.help(
1525-
"use specialization instead of type parameters by replacing them \
1526-
with concrete types like `u32`",
1527-
);
1536+
err.help(&format!(
1537+
"use specialization instead of {} parameters by replacing \
1538+
them with concrete {}{}",
1539+
kinds,
1540+
kinds_pl,
1541+
if let Some(egs) = egs {
1542+
format!(" like `{}`", egs)
1543+
} else {
1544+
"".to_string()
1545+
},
1546+
));
15281547
err.emit();
15291548
}
15301549

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
3+
4+
extern "C" {
5+
fn foo<const X: usize>(); //~ ERROR foreign items may not have const parameters
6+
7+
fn bar<T, const X: usize>(_: T); //~ ERROR foreign items may not have type or const parameters
8+
}
9+
10+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/foreign-item-const-parameter.rs:1:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error[E0044]: foreign items may not have const parameters
10+
--> $DIR/foreign-item-const-parameter.rs:5:5
11+
|
12+
LL | fn foo<const X: usize>();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't have const parameters
14+
|
15+
= help: use specialization instead of const parameters by replacing them with concrete consts
16+
17+
error[E0044]: foreign items may not have type or const parameters
18+
--> $DIR/foreign-item-const-parameter.rs:7:5
19+
|
20+
LL | fn bar<T, const X: usize>(_: T);
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't have type or const parameters
22+
|
23+
= help: use specialization instead of type or const parameters by replacing them with concrete types or consts
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0044`.

0 commit comments

Comments
 (0)