Skip to content

Commit bbd53de

Browse files
committed
Forbid non-structural_match types in const generics
1 parent 600607f commit bbd53de

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/librustc_typeck/collect.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,17 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
15321532
);
15331533
};
15341534
}
1535+
if ty::search_for_adt_without_structural_match(tcx, ty).is_some() {
1536+
struct_span_err!(
1537+
tcx.sess,
1538+
hir_ty.span,
1539+
E0739,
1540+
"the types of const generic parameters must derive `PartialEq` and `Eq`",
1541+
).span_label(
1542+
hir_ty.span,
1543+
format!("`{}` doesn't derive both `PartialEq` and `Eq`", ty),
1544+
).emit();
1545+
}
15351546
ty
15361547
}
15371548
x => {

src/librustc_typeck/error_codes.rs

+24
Original file line numberDiff line numberDiff line change
@@ -4978,6 +4978,30 @@ the future, [RFC 2091] prohibits their implementation without a follow-up RFC.
49784978
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
49794979
"##,
49804980

4981+
E0739: r##"
4982+
Only `structural_match` types (that is, types that derive `PartialEq` and `Eq`)
4983+
may be used as the types of const generic parameters.
4984+
4985+
```compile_fail,E0739
4986+
#![feature(const_generics)]
4987+
4988+
struct A;
4989+
4990+
struct B<const X: A>; // error!
4991+
```
4992+
4993+
To fix this example, we derive `PartialEq` and `Eq`.
4994+
4995+
```
4996+
#![feature(const_generics)]
4997+
4998+
#[derive(PartialEq, Eq)]
4999+
struct A;
5000+
5001+
struct B<const X: A>; // ok!
5002+
```
5003+
"##,
5004+
49815005
;
49825006
// E0035, merged into E0087/E0089
49835007
// E0036, merged into E0087/E0089
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
3+
4+
#[derive(PartialEq, Eq)]
5+
struct A;
6+
7+
struct B<const X: A>; // ok
8+
9+
struct C;
10+
11+
struct D<const X: C>; //~ ERROR the types of const generic parameters must derive
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/forbid-non-structural_match-types.rs:1:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error[E0739]: the types of const generic parameters must derive `PartialEq` and `Eq`
10+
--> $DIR/forbid-non-structural_match-types.rs:11:19
11+
|
12+
LL | struct D<const X: C>;
13+
| ^ `C` doesn't derive both `PartialEq` and `Eq`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0739`.

0 commit comments

Comments
 (0)