Skip to content

Commit

Permalink
Rollup merge of rust-lang#70845 - varkor:const-generics-derive-eq-dia…
Browse files Browse the repository at this point in the history
…gnostic, r=estebank

Make the `structural_match` error diagnostic for const generics clearer

The previous diagnostic caused confusion (rust-lang#70790), so this changes the message to be closer to the message for using non-`structural_match` constants in patterns, explicitly mentioning `#[derive(PartialEq, Eq)]`.

Fixes rust-lang#70790.

r? @estebank
  • Loading branch information
Dylan-DPC authored Apr 24, 2020
2 parents 14b1552 + f8b796b commit 45e04fe
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 19 deletions.
50 changes: 39 additions & 11 deletions src/librustc_typeck/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,45 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
if traits::search_for_structural_match_violation(param.hir_id, param.span, tcx, ty)
.is_some()
{
struct_span_err!(
tcx.sess,
hir_ty.span,
E0741,
"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();
// We use the same error code in both branches, because this is really the same
// issue: we just special-case the message for type parameters to make it
// clearer.
if let ty::Param(_) = ty.peel_refs().kind {
// Const parameters may not have type parameters as their types,
// because we cannot be sure that the type parameter derives `PartialEq`
// and `Eq` (just implementing them is not enough for `structural_match`).
struct_span_err!(
tcx.sess,
hir_ty.span,
E0741,
"`{}` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be \
used as the type of a const parameter",
ty,
)
.span_label(
hir_ty.span,
format!("`{}` may not derive both `PartialEq` and `Eq`", ty),
)
.note(
"it is not currently possible to use a type parameter as the type of a \
const parameter",
)
.emit();
} else {
struct_span_err!(
tcx.sess,
hir_ty.span,
E0741,
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
the type of a const parameter",
ty,
)
.span_label(
hir_ty.span,
format!("`{}` doesn't derive both `PartialEq` and `Eq`", ty),
)
.emit();
}
}
ty
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

struct B<T, const N: T>(PhantomData<[T; N]>); //~ ERROR const generics are unstable
//~^ ERROR the types of const generic parameters must derive `PartialEq` and `Eq`
//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ LL | struct B<T, const N: T>(PhantomData<[T; N]>);
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics)]` to the crate attributes to enable

error[E0741]: the types of const generic parameters must derive `PartialEq` and `Eq`
error[E0741]: `T` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be used as the type of a const parameter
--> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:22
|
LL | struct B<T, const N: T>(PhantomData<[T; N]>);
| ^ `T` doesn't derive both `PartialEq` and `Eq`
| ^ `T` may not derive both `PartialEq` and `Eq`
|
= note: it is not currently possible to use a type parameter as the type of a const parameter

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
// details.

pub struct Dependent<T, const X: T>([(); X]);
//~^ ERROR the types of const generic parameters must derive `PartialEq` and `Eq`
//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ LL | #![feature(const_generics)]
|
= note: `#[warn(incomplete_features)]` on by default

error[E0741]: the types of const generic parameters must derive `PartialEq` and `Eq`
error[E0741]: `T` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be used as the type of a const parameter
--> $DIR/const-param-type-depends-on-type-param.rs:9:34
|
LL | pub struct Dependent<T, const X: T>([(); X]);
| ^ `T` doesn't derive both `PartialEq` and `Eq`
| ^ `T` may not derive both `PartialEq` and `Eq`
|
= note: it is not currently possible to use a type parameter as the type of a const parameter

error: aborting due to previous error; 1 warning emitted

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ struct B<const X: A>; // ok

struct C;

struct D<const X: C>; //~ ERROR the types of const generic parameters must derive
struct D<const X: C>; //~ ERROR `C` must be annotated with `#[derive(PartialEq, Eq)]`

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | #![feature(const_generics)]
|
= note: `#[warn(incomplete_features)]` on by default

error[E0741]: the types of const generic parameters must derive `PartialEq` and `Eq`
error[E0741]: `C` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
--> $DIR/forbid-non-structural_match-types.rs:11:19
|
LL | struct D<const X: C>;
Expand Down

0 comments on commit 45e04fe

Please sign in to comment.