Forbid non-structural_match types in const generics
This commit is contained in:
parent
600607f45a
commit
bbd53deaeb
4 changed files with 65 additions and 0 deletions
|
|
@ -1532,6 +1532,17 @@ pub fn checked_type_of(tcx: TyCtxt<'_>, def_id: DefId, fail: bool) -> Option<Ty<
|
|||
);
|
||||
};
|
||||
}
|
||||
if ty::search_for_adt_without_structural_match(tcx, ty).is_some() {
|
||||
struct_span_err!(
|
||||
tcx.sess,
|
||||
hir_ty.span,
|
||||
E0739,
|
||||
"the types of const generic parameters must derive `PartialEq` and `Eq`",
|
||||
).span_label(
|
||||
hir_ty.span,
|
||||
format!("`{}` doesn't derive both `PartialEq` and `Eq`", ty),
|
||||
).emit();
|
||||
}
|
||||
ty
|
||||
}
|
||||
x => {
|
||||
|
|
|
|||
|
|
@ -4978,6 +4978,30 @@ the future, [RFC 2091] prohibits their implementation without a follow-up RFC.
|
|||
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
|
||||
"##,
|
||||
|
||||
E0739: r##"
|
||||
Only `structural_match` types (that is, types that derive `PartialEq` and `Eq`)
|
||||
may be used as the types of const generic parameters.
|
||||
|
||||
```compile_fail,E0739
|
||||
#![feature(const_generics)]
|
||||
|
||||
struct A;
|
||||
|
||||
struct B<const X: A>; // error!
|
||||
```
|
||||
|
||||
To fix this example, we derive `PartialEq` and `Eq`.
|
||||
|
||||
```
|
||||
#![feature(const_generics)]
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct A;
|
||||
|
||||
struct B<const X: A>; // ok!
|
||||
```
|
||||
"##,
|
||||
|
||||
;
|
||||
// E0035, merged into E0087/E0089
|
||||
// E0036, merged into E0087/E0089
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
#![feature(const_generics)]
|
||||
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct A;
|
||||
|
||||
struct B<const X: A>; // ok
|
||||
|
||||
struct C;
|
||||
|
||||
struct D<const X: C>; //~ ERROR the types of const generic parameters must derive
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
|
||||
--> $DIR/forbid-non-structural_match-types.rs:1:12
|
||||
|
|
||||
LL | #![feature(const_generics)]
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `#[warn(incomplete_features)]` on by default
|
||||
|
||||
error[E0739]: the types of const generic parameters must derive `PartialEq` and `Eq`
|
||||
--> $DIR/forbid-non-structural_match-types.rs:11:19
|
||||
|
|
||||
LL | struct D<const X: C>;
|
||||
| ^ `C` doesn't derive both `PartialEq` and `Eq`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0739`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue