@@ -342,19 +342,45 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
342
342
if traits:: search_for_structural_match_violation ( param. hir_id , param. span , tcx, ty)
343
343
. is_some ( )
344
344
{
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
+ }
358
384
}
359
385
ty
360
386
}
0 commit comments