Skip to content

Commit 693f703

Browse files
committed
Make E0599 a structured error
1 parent d88ffcd commit 693f703

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

Diff for: compiler/rustc_hir_analysis/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ hir_analysis_must_implement_not_function_span_note = required by this annotation
387387
388388
hir_analysis_must_implement_one_of_attribute = the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args
389389
390+
hir_analysis_no_variant_named = no variant named `{$ident}` found for enum `{$ty}`
391+
390392
hir_analysis_not_supported_delegation = {$descr}
391393
.label = callee defined here
392394

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,15 @@ pub(crate) struct CrossCrateTraitsDefined {
14171417
pub traits: String,
14181418
}
14191419

1420+
#[derive(Diagnostic)]
1421+
#[diag(hir_analysis_no_variant_named, code = E0599)]
1422+
pub struct NoVariantNamed<'tcx> {
1423+
#[primary_span]
1424+
pub span: Span,
1425+
pub ident: Ident,
1426+
pub ty: Ty<'tcx>,
1427+
}
1428+
14201429
// FIXME(fmease): Deduplicate:
14211430

14221431
#[derive(Diagnostic)]

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ use tracing::{debug, instrument};
5353

5454
use crate::bounds::Bounds;
5555
use crate::check::check_abi_fn_ptr;
56-
use crate::errors::{AmbiguousLifetimeBound, BadReturnTypeNotation, InvalidBaseType};
56+
use crate::errors::{
57+
AmbiguousLifetimeBound, BadReturnTypeNotation, InvalidBaseType, NoVariantNamed,
58+
};
5759
use crate::hir_ty_lowering::errors::{GenericsArgsErrExtend, prohibit_assoc_item_constraint};
5860
use crate::hir_ty_lowering::generics::{check_generic_arg_count, lower_generic_args};
5961
use crate::middle::resolve_bound_vars as rbv;
@@ -1188,14 +1190,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11881190
let msg = format!("expected type, found variant `{assoc_ident}`");
11891191
self.dcx().span_err(span, msg)
11901192
} else if qself_ty.is_enum() {
1191-
let mut err = struct_span_code_err!(
1192-
self.dcx(),
1193-
assoc_ident.span,
1194-
E0599,
1195-
"no variant named `{}` found for enum `{}`",
1196-
assoc_ident,
1197-
qself_ty,
1198-
);
1193+
let mut err = self.dcx().create_err(NoVariantNamed {
1194+
span: assoc_ident.span,
1195+
ident: assoc_ident,
1196+
ty: qself_ty,
1197+
});
11991198

12001199
let adt_def = qself_ty.ty_adt_def().expect("enum is not an ADT");
12011200
if let Some(variant_name) = find_best_match_for_name(

Diff for: compiler/rustc_hir_analysis/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ mod impl_wf_check;
9393
mod outlives;
9494
mod variance;
9595

96+
pub use errors::NoVariantNamed;
9697
use rustc_abi::ExternAbi;
9798
use rustc_hir as hir;
9899
use rustc_hir::def::DefKind;

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_hir::def_id::DefId;
1919
use rustc_hir::intravisit::Visitor;
2020
use rustc_hir::lang_items::LangItem;
2121
use rustc_hir::{ExprKind, HirId, QPath};
22+
use rustc_hir_analysis::NoVariantNamed;
2223
use rustc_hir_analysis::hir_ty_lowering::{FeedConstTy, HirTyLowerer as _};
2324
use rustc_infer::infer;
2425
use rustc_infer::infer::{DefineOpaqueTypes, InferOk};
@@ -3837,15 +3838,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38373838
.iter_enumerated()
38383839
.find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident)
38393840
else {
3840-
type_error_struct!(
3841-
self.dcx(),
3842-
ident.span,
3843-
container,
3844-
E0599,
3845-
"no variant named `{ident}` found for enum `{container}`",
3846-
)
3847-
.with_span_label(field.span, "variant not found")
3848-
.emit();
3841+
self.dcx()
3842+
.create_err(NoVariantNamed { span: ident.span, ident, ty: container })
3843+
.with_span_label(field.span, "variant not found")
3844+
.emit_unless(container.references_error());
38493845
break;
38503846
};
38513847
let Some(&subfield) = fields.next() else {

0 commit comments

Comments
 (0)