]> git.lizzy.rs Git - rust.git/commitdiff
Add error message for using type parameter as the type of a const parameter
authorvarkor <github@varkor.com>
Mon, 20 Apr 2020 13:01:59 +0000 (14:01 +0100)
committervarkor <github@varkor.com>
Mon, 20 Apr 2020 13:01:59 +0000 (14:01 +0100)
src/librustc_typeck/collect/type_of.rs
src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs
src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr
src/test/ui/const-generics/const-param-type-depends-on-type-param.rs
src/test/ui/const-generics/const-param-type-depends-on-type-param.stderr

index ddf0b247ae3a39ca10ca3d4cfb938dfccc136c9d..5ebf86ef101261d49111bcfc3bc6f94918b0aace 100644 (file)
@@ -342,19 +342,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,
-                        "`{}` 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();
+                    // 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
             }
index 59bb06690a1c91b6735dc582dc09cd0e36e4ce1d..86ab8075896aa6c51fc503bd809f6d989ed7ebfa 100644 (file)
@@ -1,6 +1,6 @@
 use std::marker::PhantomData;
 
 struct B<T, const N: T>(PhantomData<[T; N]>); //~ ERROR const generics are unstable
-//~^ ERROR `T` must be annotated with `#[derive(PartialEq, Eq)]`
+//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`
 
 fn main() {}
index 70f9fc4184c2c6ebe54bfb13b90b87ea9d579ca9..92a7edf96bccb504f52437e35a89fd5b9c0a24c1 100644 (file)
@@ -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]: `T` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
+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
 
index 4dcda3b87a8545424c1b2058728850ca8ebd279e..7468020366cecb6fb4493d808cc777483c00d565 100644 (file)
@@ -7,6 +7,6 @@
 // details.
 
 pub struct Dependent<T, const X: T>([(); X]);
-//~^ ERROR `T` must be annotated with `#[derive(PartialEq, Eq)]`
+//~^ ERROR `T` is not guaranteed to `#[derive(PartialEq, Eq)]`
 
 fn main() {}
index 290d53c4e1971402937182324570a920c538bbef..9f20b06813e378361f7f6d79102ffbe8f5368b84 100644 (file)
@@ -6,11 +6,13 @@ LL | #![feature(const_generics)]
    |
    = note: `#[warn(incomplete_features)]` on by default
 
-error[E0741]: `T` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
+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