Skip to content

Commit 60ca03e

Browse files
bors[bot]matklad
andauthored
Merge #9252
9252: internal: refactor mismatched args count diagnostic r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 8e5f469 + 8d391ec commit 60ca03e

File tree

9 files changed

+752
-762
lines changed

9 files changed

+752
-762
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 14 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ macro_rules! diagnostics {
3232
}
3333

3434
diagnostics![
35-
UnresolvedModule,
35+
BreakOutsideOfLoop,
36+
InactiveCode,
37+
MacroError,
38+
MismatchedArgCount,
39+
MissingFields,
40+
MissingUnsafe,
41+
NoSuchField,
42+
UnimplementedBuiltinMacro,
3643
UnresolvedExternCrate,
3744
UnresolvedImport,
3845
UnresolvedMacroCall,
46+
UnresolvedModule,
3947
UnresolvedProcMacro,
40-
MacroError,
41-
MissingFields,
42-
InactiveCode,
4348
];
4449

4550
#[derive(Debug)]
@@ -88,101 +93,22 @@ pub struct MacroError {
8893

8994
#[derive(Debug)]
9095
pub struct UnimplementedBuiltinMacro {
91-
pub file: HirFileId,
92-
pub node: SyntaxNodePtr,
93-
}
94-
95-
impl Diagnostic for UnimplementedBuiltinMacro {
96-
fn code(&self) -> DiagnosticCode {
97-
DiagnosticCode("unimplemented-builtin-macro")
98-
}
99-
100-
fn message(&self) -> String {
101-
"unimplemented built-in macro".to_string()
102-
}
103-
104-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
105-
InFile::new(self.file, self.node.clone())
106-
}
107-
108-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
109-
self
110-
}
96+
pub node: InFile<SyntaxNodePtr>,
11197
}
11298

113-
// Diagnostic: no-such-field
114-
//
115-
// This diagnostic is triggered if created structure does not have field provided in record.
11699
#[derive(Debug)]
117100
pub struct NoSuchField {
118-
pub file: HirFileId,
119-
pub field: AstPtr<ast::RecordExprField>,
101+
pub field: InFile<AstPtr<ast::RecordExprField>>,
120102
}
121103

122-
impl Diagnostic for NoSuchField {
123-
fn code(&self) -> DiagnosticCode {
124-
DiagnosticCode("no-such-field")
125-
}
126-
127-
fn message(&self) -> String {
128-
"no such field".to_string()
129-
}
130-
131-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
132-
InFile::new(self.file, self.field.clone().into())
133-
}
134-
135-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
136-
self
137-
}
138-
}
139-
140-
// Diagnostic: break-outside-of-loop
141-
//
142-
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
143104
#[derive(Debug)]
144105
pub struct BreakOutsideOfLoop {
145-
pub file: HirFileId,
146-
pub expr: AstPtr<ast::Expr>,
106+
pub expr: InFile<AstPtr<ast::Expr>>,
147107
}
148108

149-
impl Diagnostic for BreakOutsideOfLoop {
150-
fn code(&self) -> DiagnosticCode {
151-
DiagnosticCode("break-outside-of-loop")
152-
}
153-
fn message(&self) -> String {
154-
"break outside of loop".to_string()
155-
}
156-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
157-
InFile { file_id: self.file, value: self.expr.clone().into() }
158-
}
159-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
160-
self
161-
}
162-
}
163-
164-
// Diagnostic: missing-unsafe
165-
//
166-
// This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block.
167109
#[derive(Debug)]
168110
pub struct MissingUnsafe {
169-
pub file: HirFileId,
170-
pub expr: AstPtr<ast::Expr>,
171-
}
172-
173-
impl Diagnostic for MissingUnsafe {
174-
fn code(&self) -> DiagnosticCode {
175-
DiagnosticCode("missing-unsafe")
176-
}
177-
fn message(&self) -> String {
178-
format!("This operation is unsafe and requires an unsafe function or block")
179-
}
180-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
181-
InFile { file_id: self.file, value: self.expr.clone().into() }
182-
}
183-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
184-
self
185-
}
111+
pub expr: InFile<AstPtr<ast::Expr>>,
186112
}
187113

188114
#[derive(Debug)]
@@ -218,36 +144,13 @@ impl Diagnostic for ReplaceFilterMapNextWithFindMap {
218144
}
219145
}
220146

221-
// Diagnostic: mismatched-arg-count
222-
//
223-
// This diagnostic is triggered if a function is invoked with an incorrect amount of arguments.
224147
#[derive(Debug)]
225148
pub struct MismatchedArgCount {
226-
pub file: HirFileId,
227-
pub call_expr: AstPtr<ast::Expr>,
149+
pub call_expr: InFile<AstPtr<ast::Expr>>,
228150
pub expected: usize,
229151
pub found: usize,
230152
}
231153

232-
impl Diagnostic for MismatchedArgCount {
233-
fn code(&self) -> DiagnosticCode {
234-
DiagnosticCode("mismatched-arg-count")
235-
}
236-
fn message(&self) -> String {
237-
let s = if self.expected == 1 { "" } else { "s" };
238-
format!("Expected {} argument{}, found {}", self.expected, s, self.found)
239-
}
240-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
241-
InFile { file_id: self.file, value: self.call_expr.clone().into() }
242-
}
243-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
244-
self
245-
}
246-
fn is_experimental(&self) -> bool {
247-
true
248-
}
249-
}
250-
251154
#[derive(Debug)]
252155
pub struct RemoveThisSemicolon {
253156
pub file: HirFileId,

crates/hir/src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,12 @@ impl Module {
606606
let node = ast.to_node(db.upcast());
607607
// Must have a name, otherwise we wouldn't emit it.
608608
let name = node.name().expect("unimplemented builtin macro with no name");
609-
let ptr = SyntaxNodePtr::from(AstPtr::new(&name));
610-
sink.push(UnimplementedBuiltinMacro { file: ast.file_id, node: ptr });
609+
acc.push(
610+
UnimplementedBuiltinMacro {
611+
node: ast.with_value(SyntaxNodePtr::from(AstPtr::new(&name))),
612+
}
613+
.into(),
614+
);
611615
}
612616
}
613617
}
@@ -1073,22 +1077,20 @@ impl Function {
10731077
match d {
10741078
hir_ty::InferenceDiagnostic::NoSuchField { expr } => {
10751079
let field = source_map.field_syntax(*expr);
1076-
sink.push(NoSuchField { file: field.file_id, field: field.value })
1080+
acc.push(NoSuchField { field }.into())
10771081
}
10781082
hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => {
1079-
let ptr = source_map
1083+
let expr = source_map
10801084
.expr_syntax(*expr)
10811085
.expect("break outside of loop in synthetic syntax");
1082-
sink.push(BreakOutsideOfLoop { file: ptr.file_id, expr: ptr.value })
1086+
acc.push(BreakOutsideOfLoop { expr }.into())
10831087
}
10841088
}
10851089
}
10861090

10871091
for expr in hir_ty::diagnostics::missing_unsafe(db, self.id.into()) {
10881092
match source_map.expr_syntax(expr) {
1089-
Ok(in_file) => {
1090-
sink.push(MissingUnsafe { file: in_file.file_id, expr: in_file.value })
1091-
}
1093+
Ok(expr) => acc.push(MissingUnsafe { expr }.into()),
10921094
Err(SyntheticSyntax) => {
10931095
// FIXME: Here and eslwhere in this file, the `expr` was
10941096
// desugared, report or assert that this doesn't happen.
@@ -1174,12 +1176,9 @@ impl Function {
11741176
}
11751177
BodyValidationDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
11761178
match source_map.expr_syntax(call_expr) {
1177-
Ok(source_ptr) => sink.push(MismatchedArgCount {
1178-
file: source_ptr.file_id,
1179-
call_expr: source_ptr.value,
1180-
expected,
1181-
found,
1182-
}),
1179+
Ok(source_ptr) => acc.push(
1180+
MismatchedArgCount { call_expr: source_ptr, expected, found }.into(),
1181+
),
11831182
Err(SyntheticSyntax) => (),
11841183
}
11851184
}

0 commit comments

Comments
 (0)