Skip to content

Commit 7de7c36

Browse files
committed
Explain that in paths generics belong on the enum, not the variant
``` error[E0109]: type arguments are not allowed on tuple variant `TSVariant` --> $DIR/enum-variant-generic-args.rs:54:29 | LL | TSVariant::<()>(()); | --------- ^^ type argument not allowed | | | not allowed on tuple variant `TSVariant` | = note: type arguments are not allowed for enum variants; specify them on the enum itself ``` Fix #93993.
1 parent 7a0cde9 commit 7de7c36

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{
66
Applicability, Diag, ErrorGuaranteed, MultiSpan, pluralize, struct_span_code_err,
77
};
88
use rustc_hir as hir;
9-
use rustc_hir::def::{DefKind, Res};
9+
use rustc_hir::def::{CtorOf, DefKind, Res};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_middle::bug;
1212
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
@@ -1048,15 +1048,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
10481048
} else {
10491049
Some((
10501050
match segment.res {
1051-
hir::def::Res::PrimTy(ty) => {
1051+
Res::PrimTy(ty) => {
10521052
format!("{} `{}`", segment.res.descr(), ty.name())
10531053
}
1054-
hir::def::Res::Def(_, def_id)
1054+
Res::Def(_, def_id)
10551055
if let Some(name) = self.tcx().opt_item_name(def_id) =>
10561056
{
10571057
format!("{} `{name}`", segment.res.descr())
10581058
}
1059-
hir::def::Res::Err => "this type".to_string(),
1059+
Res::Err => "this type".to_string(),
10601060
_ => segment.res.descr().to_string(),
10611061
},
10621062
segment.ident.span,
@@ -1118,6 +1118,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11181118
for (what, span) in types_and_spans {
11191119
err.span_label(span, format!("not allowed on {what}"));
11201120
}
1121+
if segments.clone().any(|segment| {
1122+
!segment.args().args.is_empty()
1123+
&& matches!(segment.res, Res::Def(DefKind::Ctor(CtorOf::Variant, _), _))
1124+
}) {
1125+
let msg = "generic arguments are not allowed for enum variants; specify them on the \
1126+
enum itself";
1127+
err.note(msg);
1128+
}
11211129
generics_args_err_extend(self.tcx(), segments, &mut err, err_extend);
11221130
err.emit()
11231131
}

tests/ui/type-alias-enum-variants/enum-variant-generic-args.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ LL | Enum::<()>::TSVariant::<()>(());
279279
| --------- ^^ type argument not allowed
280280
| |
281281
| not allowed on tuple variant `TSVariant`
282+
|
283+
= note: generic arguments are not allowed for enum variants; specify them on the enum itself
282284

283285
error[E0109]: type arguments are not allowed on this type
284286
--> $DIR/enum-variant-generic-args.rs:57:24
@@ -445,6 +447,8 @@ LL | Enum::<()>::UVariant::<()>;
445447
| -------- ^^ type argument not allowed
446448
| |
447449
| not allowed on unit variant `UVariant`
450+
|
451+
= note: generic arguments are not allowed for enum variants; specify them on the enum itself
448452

449453
error[E0109]: type arguments are not allowed on this type
450454
--> $DIR/enum-variant-generic-args.rs:93:23

0 commit comments

Comments
 (0)