From d3621eeb02652038a8185f60d78fb4791a732dc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 19:35:30 +0300 Subject: internal: refactor unimplemented builtin macro diagnostic --- crates/hir/src/diagnostics.rs | 30 ++++++------------------------ crates/hir/src/lib.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 26 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 28580eeb4..3908e67a2 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -32,14 +32,15 @@ macro_rules! diagnostics { } diagnostics![ - UnresolvedModule, + InactiveCode, + MacroError, + MissingFields, + UnimplementedBuiltinMacro, UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall, + UnresolvedModule, UnresolvedProcMacro, - MacroError, - MissingFields, - InactiveCode, ]; #[derive(Debug)] @@ -88,26 +89,7 @@ pub struct MacroError { #[derive(Debug)] pub struct UnimplementedBuiltinMacro { - pub file: HirFileId, - pub node: SyntaxNodePtr, -} - -impl Diagnostic for UnimplementedBuiltinMacro { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("unimplemented-builtin-macro") - } - - fn message(&self) -> String { - "unimplemented built-in macro".to_string() - } - - fn display_source(&self) -> InFile { - InFile::new(self.file, self.node.clone()) - } - - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } + pub node: InFile, } // Diagnostic: no-such-field diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d891d0ec1..a361158e0 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -606,8 +606,12 @@ impl Module { let node = ast.to_node(db.upcast()); // Must have a name, otherwise we wouldn't emit it. let name = node.name().expect("unimplemented builtin macro with no name"); - let ptr = SyntaxNodePtr::from(AstPtr::new(&name)); - sink.push(UnimplementedBuiltinMacro { file: ast.file_id, node: ptr }); + acc.push( + UnimplementedBuiltinMacro { + node: ast.with_value(SyntaxNodePtr::from(AstPtr::new(&name))), + } + .into(), + ); } } } -- cgit v1.2.3 From 7166e8549bad95b05f66acd508d07a6cd97d3dc0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 19:45:16 +0300 Subject: internal: refactor NoSuchField diagnostic --- crates/hir/src/diagnostics.rs | 25 ++----------------------- crates/hir/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 24 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 3908e67a2..c7702e09f 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -35,6 +35,7 @@ diagnostics![ InactiveCode, MacroError, MissingFields, + NoSuchField, UnimplementedBuiltinMacro, UnresolvedExternCrate, UnresolvedImport, @@ -92,31 +93,9 @@ pub struct UnimplementedBuiltinMacro { pub node: InFile, } -// Diagnostic: no-such-field -// -// This diagnostic is triggered if created structure does not have field provided in record. #[derive(Debug)] pub struct NoSuchField { - pub file: HirFileId, - pub field: AstPtr, -} - -impl Diagnostic for NoSuchField { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("no-such-field") - } - - fn message(&self) -> String { - "no such field".to_string() - } - - fn display_source(&self) -> InFile { - InFile::new(self.file, self.field.clone().into()) - } - - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } + pub field: InFile>, } // Diagnostic: break-outside-of-loop diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index a361158e0..7faf6ec1f 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1077,7 +1077,7 @@ impl Function { match d { hir_ty::InferenceDiagnostic::NoSuchField { expr } => { let field = source_map.field_syntax(*expr); - sink.push(NoSuchField { file: field.file_id, field: field.value }) + acc.push(NoSuchField { field }.into()) } hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => { let ptr = source_map -- cgit v1.2.3 From 886b66cd03cbe7cb13e248d7c7bbdeba66c7796a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 19:51:19 +0300 Subject: internal: refactor BreakOutsideOfLoop diagnostic --- crates/hir/src/diagnostics.rs | 22 ++-------------------- crates/hir/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 22 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index c7702e09f..47d17ba70 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -32,6 +32,7 @@ macro_rules! diagnostics { } diagnostics![ + BreakOutsideOfLoop, InactiveCode, MacroError, MissingFields, @@ -98,28 +99,9 @@ pub struct NoSuchField { pub field: InFile>, } -// Diagnostic: break-outside-of-loop -// -// This diagnostic is triggered if the `break` keyword is used outside of a loop. #[derive(Debug)] pub struct BreakOutsideOfLoop { - pub file: HirFileId, - pub expr: AstPtr, -} - -impl Diagnostic for BreakOutsideOfLoop { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("break-outside-of-loop") - } - fn message(&self) -> String { - "break outside of loop".to_string() - } - fn display_source(&self) -> InFile { - InFile { file_id: self.file, value: self.expr.clone().into() } - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } + pub expr: InFile>, } // Diagnostic: missing-unsafe diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 7faf6ec1f..2f507b83b 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1080,10 +1080,10 @@ impl Function { acc.push(NoSuchField { field }.into()) } hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => { - let ptr = source_map + let expr = source_map .expr_syntax(*expr) .expect("break outside of loop in synthetic syntax"); - sink.push(BreakOutsideOfLoop { file: ptr.file_id, expr: ptr.value }) + acc.push(BreakOutsideOfLoop { expr }.into()) } } } -- cgit v1.2.3 From bccf77f26cd504de14f7d7d03f9f2a85d0fabb3d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 20:00:27 +0300 Subject: internal: refactor missing unsafe diagnostic --- crates/hir/src/diagnostics.rs | 22 ++-------------------- crates/hir/src/lib.rs | 4 +--- 2 files changed, 3 insertions(+), 23 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 47d17ba70..f7bf63215 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -36,6 +36,7 @@ diagnostics![ InactiveCode, MacroError, MissingFields, + MissingUnsafe, NoSuchField, UnimplementedBuiltinMacro, UnresolvedExternCrate, @@ -104,28 +105,9 @@ pub struct BreakOutsideOfLoop { pub expr: InFile>, } -// Diagnostic: missing-unsafe -// -// This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block. #[derive(Debug)] pub struct MissingUnsafe { - pub file: HirFileId, - pub expr: AstPtr, -} - -impl Diagnostic for MissingUnsafe { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("missing-unsafe") - } - fn message(&self) -> String { - format!("This operation is unsafe and requires an unsafe function or block") - } - fn display_source(&self) -> InFile { - InFile { file_id: self.file, value: self.expr.clone().into() } - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } + pub expr: InFile>, } #[derive(Debug)] diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 2f507b83b..16f862707 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1090,9 +1090,7 @@ impl Function { for expr in hir_ty::diagnostics::missing_unsafe(db, self.id.into()) { match source_map.expr_syntax(expr) { - Ok(in_file) => { - sink.push(MissingUnsafe { file: in_file.file_id, expr: in_file.value }) - } + Ok(expr) => acc.push(MissingUnsafe { expr }.into()), Err(SyntheticSyntax) => { // FIXME: Here and eslwhere in this file, the `expr` was // desugared, report or assert that this doesn't happen. -- cgit v1.2.3 From 8d391ec981562785ec92ce3afe950972c523f925 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 20:06:25 +0300 Subject: internal: refactor mismatched args count diagnostic --- crates/hir/src/diagnostics.rs | 26 ++------------------------ crates/hir/src/lib.rs | 9 +++------ 2 files changed, 5 insertions(+), 30 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index f7bf63215..f839616ce 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -35,6 +35,7 @@ diagnostics![ BreakOutsideOfLoop, InactiveCode, MacroError, + MismatchedArgCount, MissingFields, MissingUnsafe, NoSuchField, @@ -143,36 +144,13 @@ impl Diagnostic for ReplaceFilterMapNextWithFindMap { } } -// Diagnostic: mismatched-arg-count -// -// This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. #[derive(Debug)] pub struct MismatchedArgCount { - pub file: HirFileId, - pub call_expr: AstPtr, + pub call_expr: InFile>, pub expected: usize, pub found: usize, } -impl Diagnostic for MismatchedArgCount { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("mismatched-arg-count") - } - fn message(&self) -> String { - let s = if self.expected == 1 { "" } else { "s" }; - format!("Expected {} argument{}, found {}", self.expected, s, self.found) - } - fn display_source(&self) -> InFile { - InFile { file_id: self.file, value: self.call_expr.clone().into() } - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } - fn is_experimental(&self) -> bool { - true - } -} - #[derive(Debug)] pub struct RemoveThisSemicolon { pub file: HirFileId, diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 16f862707..c1af5f097 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1176,12 +1176,9 @@ impl Function { } BodyValidationDiagnostic::MismatchedArgCount { call_expr, expected, found } => { match source_map.expr_syntax(call_expr) { - Ok(source_ptr) => sink.push(MismatchedArgCount { - file: source_ptr.file_id, - call_expr: source_ptr.value, - expected, - found, - }), + Ok(source_ptr) => acc.push( + MismatchedArgCount { call_expr: source_ptr, expected, found }.into(), + ), Err(SyntheticSyntax) => (), } } -- cgit v1.2.3