aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 16:41:04 +0100
committerAleksey Kladov <[email protected]>2021-06-13 16:41:04 +0100
commit00303284b5cc3a82e32dc3ecbbcfeb2f99de6818 (patch)
tree685e3f21289eaeb25df597413528f25c6239813f /crates/hir/src
parent1e4aaee7bbc1d56698e70158aa35f578422623d9 (diff)
internal: refactor macro error
Diffstat (limited to 'crates/hir/src')
-rw-r--r--crates/hir/src/diagnostics.rs26
-rw-r--r--crates/hir/src/lib.rs20
2 files changed, 13 insertions, 33 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index 2039d2b43..28580eeb4 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -37,6 +37,7 @@ diagnostics![
37 UnresolvedImport, 37 UnresolvedImport,
38 UnresolvedMacroCall, 38 UnresolvedMacroCall,
39 UnresolvedProcMacro, 39 UnresolvedProcMacro,
40 MacroError,
40 MissingFields, 41 MissingFields,
41 InactiveCode, 42 InactiveCode,
42]; 43];
@@ -79,35 +80,12 @@ pub struct UnresolvedProcMacro {
79 pub macro_name: Option<String>, 80 pub macro_name: Option<String>,
80} 81}
81 82
82// Diagnostic: macro-error
83//
84// This diagnostic is shown for macro expansion errors.
85#[derive(Debug, Clone, Eq, PartialEq)] 83#[derive(Debug, Clone, Eq, PartialEq)]
86pub struct MacroError { 84pub struct MacroError {
87 pub file: HirFileId, 85 pub node: InFile<SyntaxNodePtr>,
88 pub node: SyntaxNodePtr,
89 pub message: String, 86 pub message: String,
90} 87}
91 88
92impl Diagnostic for MacroError {
93 fn code(&self) -> DiagnosticCode {
94 DiagnosticCode("macro-error")
95 }
96 fn message(&self) -> String {
97 self.message.clone()
98 }
99 fn display_source(&self) -> InFile<SyntaxNodePtr> {
100 InFile::new(self.file, self.node.clone())
101 }
102 fn as_any(&self) -> &(dyn Any + Send + 'static) {
103 self
104 }
105 fn is_experimental(&self) -> bool {
106 // Newly added and not very well-tested, might contain false positives.
107 true
108 }
109}
110
111#[derive(Debug)] 89#[derive(Debug)]
112pub struct UnimplementedBuiltinMacro { 90pub struct UnimplementedBuiltinMacro {
113 pub file: HirFileId, 91 pub file: HirFileId,
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 87a3db946..d891d0ec1 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -587,19 +587,19 @@ impl Module {
587 } 587 }
588 588
589 DefDiagnosticKind::MacroError { ast, message } => { 589 DefDiagnosticKind::MacroError { ast, message } => {
590 let (file, ast) = match ast { 590 let node = match ast {
591 MacroCallKind::FnLike { ast_id, .. } => { 591 MacroCallKind::FnLike { ast_id, .. } => {
592 let node = ast_id.to_node(db.upcast()); 592 let node = ast_id.to_node(db.upcast());
593 (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) 593 ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
594 } 594 }
595 MacroCallKind::Derive { ast_id, .. } 595 MacroCallKind::Derive { ast_id, .. }
596 | MacroCallKind::Attr { ast_id, .. } => { 596 | MacroCallKind::Attr { ast_id, .. } => {
597 // FIXME: point to the attribute instead, this creates very large diagnostics 597 // FIXME: point to the attribute instead, this creates very large diagnostics
598 let node = ast_id.to_node(db.upcast()); 598 let node = ast_id.to_node(db.upcast());
599 (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) 599 ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
600 } 600 }
601 }; 601 };
602 sink.push(MacroError { file, node: ast, message: message.clone() }); 602 acc.push(MacroError { node, message: message.clone() }.into());
603 } 603 }
604 604
605 DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => { 605 DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => {
@@ -1046,11 +1046,13 @@ impl Function {
1046 InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() } 1046 InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() }
1047 .into(), 1047 .into(),
1048 ), 1048 ),
1049 BodyDiagnostic::MacroError { node, message } => sink.push(MacroError { 1049 BodyDiagnostic::MacroError { node, message } => acc.push(
1050 file: node.file_id, 1050 MacroError {
1051 node: node.value.clone().into(), 1051 node: node.clone().map(|it| it.into()),
1052 message: message.to_string(), 1052 message: message.to_string(),
1053 }), 1053 }
1054 .into(),
1055 ),
1054 BodyDiagnostic::UnresolvedProcMacro { node } => acc.push( 1056 BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
1055 UnresolvedProcMacro { 1057 UnresolvedProcMacro {
1056 node: node.clone().map(|it| it.into()), 1058 node: node.clone().map(|it| it.into()),