Skip to content

Commit f8b796b

Browse files
committed
Add error message for using type parameter as the type of a const parameter
1 parent 3eea7b3 commit f8b796b

5 files changed

+49
-19
lines changed

src/librustc_typeck/collect/type_of.rs

+39-13
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,45 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
342342
if traits::search_for_structural_match_violation(param.hir_id, param.span, tcx, ty)
343343
.is_some()
344344
{
345-
struct_span_err!(
346-
tcx.sess,
347-
hir_ty.span,
348-
E0741,
349-
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the \
350-
type of a const parameter",
351-
ty,
352-
)
353-
.span_label(
354-
hir_ty.span,
355-
format!("`{}` doesn't derive both `PartialEq` and `Eq`", ty),
356-
)
357-
.emit();
345+
// We use the same error code in both branches, because this is really the same
346+
// issue: we just special-case the message for type parameters to make it
347+
// clearer.
348+
if let ty::Param(_) = ty.peel_refs().kind {
349+
// Const parameters may not have type parameters as their types,
350+
// because we cannot be sure that the type parameter derives `PartialEq`
351+
// and `Eq` (just implementing them is not enough for `structural_match`).
352+
struct_span_err!(
353+
tcx.sess,
354+
hir_ty.span,
355+
E0741,
356+
"`{}` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be \
357+
used as the type of a const parameter",
358+
ty,
359+
)
360+
.span_label(
361+
hir_ty.span,
362+
format!("`{}` may not derive both `PartialEq` and `Eq`", ty),
363+
)
364+
.note(
365+
"it is not currently possible to use a type parameter as the type of a \
366+
const parameter",
367+
)
368+
.emit();
369+
} else {
370+
struct_span_err!(
371+
tcx.sess,
372+
hir_ty.span,
373+
E0741,
374+
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
375+
the type of a const parameter",
376+
ty,
377+
)
378+
.span_label(
379+
hir_ty.span,
380+
format!("`{}` doesn't derive both `PartialEq` and `Eq`", ty),
381+
)
382+
.emit();
383+
}
358384
}
359385
ty
360386
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::marker::PhantomData;
22

33
struct B<T, const N: T>(PhantomData<[T; N]>); //~ ERROR const generics are unstable
4-
//~^ ERROR `T` must be annotated with `#[derive(PartialEq, Eq)]`
4+
//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`
55

66
fn main() {}

src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ LL | struct B<T, const N: T>(PhantomData<[T; N]>);
77
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
88
= help: add `#![feature(const_generics)]` to the crate attributes to enable
99

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

1618
error: aborting due to 2 previous errors
1719

src/test/ui/const-generics/const-param-type-depends-on-type-param.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
// details.
88

99
pub struct Dependent<T, const X: T>([(); X]);
10-
//~^ ERROR `T` must be annotated with `#[derive(PartialEq, Eq)]`
10+
//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`
1111

1212
fn main() {}

src/test/ui/const-generics/const-param-type-depends-on-type-param.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ LL | #![feature(const_generics)]
66
|
77
= note: `#[warn(incomplete_features)]` on by default
88

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

1517
error: aborting due to previous error; 1 warning emitted
1618

0 commit comments

Comments
 (0)