Skip to content

Commit 73a4275

Browse files
committed
Auto merge of #17131 - bzy-debug:issue-17107, r=lnicola
different error code of "no such field" error based on variant type fix #17107 Pass variant information down to diagnostic, so that we can get different error code based on variant type. After fix: - structure <img width="868" alt="Screenshot 2024-04-23 at 21 03 27" src="https://github.com/rust-lang/rust-analyzer/assets/71200607/c2d1e389-5b62-4e9a-a133-9ae41f80615f"> - enum's structure variant <img width="937" alt="Screenshot 2024-04-23 at 21 04 41" src="https://github.com/rust-lang/rust-analyzer/assets/71200607/ad8558a7-d809-4968-b290-2f6bbb8d6b8c">
2 parents e31c9f3 + 5c88e98 commit 73a4275

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

crates/hir-ty/src/infer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ pub enum InferenceDiagnostic {
198198
NoSuchField {
199199
field: ExprOrPatId,
200200
private: bool,
201+
variant: VariantId,
201202
},
202203
PrivateField {
203204
expr: ExprId,

crates/hir-ty/src/infer/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ impl InferenceContext<'_> {
563563
InferenceDiagnostic::NoSuchField {
564564
field: field.expr.into(),
565565
private: true,
566+
variant: def,
566567
},
567568
);
568569
}
@@ -572,6 +573,7 @@ impl InferenceContext<'_> {
572573
self.push_diagnostic(InferenceDiagnostic::NoSuchField {
573574
field: field.expr.into(),
574575
private: false,
576+
variant: def,
575577
});
576578
None
577579
}

crates/hir-ty/src/infer/pat.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ impl InferenceContext<'_> {
177177
self.push_diagnostic(InferenceDiagnostic::NoSuchField {
178178
field: inner.into(),
179179
private: true,
180+
variant: def,
180181
});
181182
}
182183
let f = field_types[local_id].clone();
@@ -190,6 +191,7 @@ impl InferenceContext<'_> {
190191
self.push_diagnostic(InferenceDiagnostic::NoSuchField {
191192
field: inner.into(),
192193
private: false,
194+
variant: def,
193195
});
194196
self.err_ty()
195197
}

crates/hir/src/diagnostics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use hir_ty::{db::HirDatabase, diagnostics::BodyValidationDiagnostic, InferenceDi
99
use base_db::CrateId;
1010
use cfg::{CfgExpr, CfgOptions};
1111
use either::Either;
12+
pub use hir_def::VariantId;
1213
use hir_def::{body::SyntheticSyntax, hir::ExprOrPatId, path::ModPath, AssocItemId, DefWithBodyId};
1314
use hir_expand::{name::Name, HirFileId, InFile};
1415
use syntax::{ast, AstPtr, SyntaxError, SyntaxNodePtr, TextRange};
@@ -200,6 +201,7 @@ pub struct MalformedDerive {
200201
pub struct NoSuchField {
201202
pub field: InFile<AstPtr<Either<ast::RecordExprField, ast::RecordPatField>>>,
202203
pub private: bool,
204+
pub variant: VariantId,
203205
}
204206

205207
#[derive(Debug)]
@@ -525,7 +527,7 @@ impl AnyDiagnostic {
525527
source_map.pat_syntax(pat).inspect_err(|_| tracing::error!("synthetic syntax")).ok()
526528
};
527529
Some(match d {
528-
&InferenceDiagnostic::NoSuchField { field: expr, private } => {
530+
&InferenceDiagnostic::NoSuchField { field: expr, private, variant } => {
529531
let expr_or_pat = match expr {
530532
ExprOrPatId::ExprId(expr) => {
531533
source_map.field_syntax(expr).map(AstPtr::wrap_left)
@@ -534,7 +536,7 @@ impl AnyDiagnostic {
534536
source_map.pat_field_syntax(pat).map(AstPtr::wrap_right)
535537
}
536538
};
537-
NoSuchField { field: expr_or_pat, private }.into()
539+
NoSuchField { field: expr_or_pat, private, variant }.into()
538540
}
539541
&InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
540542
MismatchedArgCount { call_expr: expr_syntax(call_expr)?, expected, found }.into()

crates/ide-diagnostics/src/handlers/no_such_field.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use either::Either;
2-
use hir::{db::ExpandDatabase, HasSource, HirDisplay, HirFileIdExt, Semantics};
2+
use hir::{db::ExpandDatabase, HasSource, HirDisplay, HirFileIdExt, Semantics, VariantId};
33
use ide_db::{base_db::FileId, source_change::SourceChange, RootDatabase};
44
use syntax::{
55
ast::{self, edit::IndentLevel, make},
@@ -25,7 +25,10 @@ pub(crate) fn no_such_field(ctx: &DiagnosticsContext<'_>, d: &hir::NoSuchField)
2525
} else {
2626
Diagnostic::new_with_syntax_node_ptr(
2727
ctx,
28-
DiagnosticCode::RustcHardError("E0559"),
28+
match d.variant {
29+
VariantId::EnumVariantId(_) => DiagnosticCode::RustcHardError("E0559"),
30+
_ => DiagnosticCode::RustcHardError("E0560"),
31+
},
2932
"no such field",
3033
node,
3134
)

0 commit comments

Comments
 (0)