Skip to content

Commit 0030328

Browse files
committed
internal: refactor macro error
1 parent 1e4aaee commit 0030328

File tree

7 files changed

+178
-198
lines changed

7 files changed

+178
-198
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ diagnostics![
3737
UnresolvedImport,
3838
UnresolvedMacroCall,
3939
UnresolvedProcMacro,
40+
MacroError,
4041
MissingFields,
4142
InactiveCode,
4243
];
@@ -79,35 +80,12 @@ pub struct UnresolvedProcMacro {
7980
pub macro_name: Option<String>,
8081
}
8182

82-
// Diagnostic: macro-error
83-
//
84-
// This diagnostic is shown for macro expansion errors.
8583
#[derive(Debug, Clone, Eq, PartialEq)]
8684
pub struct MacroError {
87-
pub file: HirFileId,
88-
pub node: SyntaxNodePtr,
85+
pub node: InFile<SyntaxNodePtr>,
8986
pub message: String,
9087
}
9188

92-
impl 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-
11189
#[derive(Debug)]
11290
pub struct UnimplementedBuiltinMacro {
11391
pub file: HirFileId,

crates/hir/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -587,19 +587,19 @@ impl Module {
587587
}
588588

589589
DefDiagnosticKind::MacroError { ast, message } => {
590-
let (file, ast) = match ast {
590+
let node = match ast {
591591
MacroCallKind::FnLike { ast_id, .. } => {
592592
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)))
594594
}
595595
MacroCallKind::Derive { ast_id, .. }
596596
| MacroCallKind::Attr { ast_id, .. } => {
597597
// FIXME: point to the attribute instead, this creates very large diagnostics
598598
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)))
600600
}
601601
};
602-
sink.push(MacroError { file, node: ast, message: message.clone() });
602+
acc.push(MacroError { node, message: message.clone() }.into());
603603
}
604604

605605
DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => {
@@ -1046,11 +1046,13 @@ impl Function {
10461046
InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() }
10471047
.into(),
10481048
),
1049-
BodyDiagnostic::MacroError { node, message } => sink.push(MacroError {
1050-
file: node.file_id,
1051-
node: node.value.clone().into(),
1052-
message: message.to_string(),
1053-
}),
1049+
BodyDiagnostic::MacroError { node, message } => acc.push(
1050+
MacroError {
1051+
node: node.clone().map(|it| it.into()),
1052+
message: message.to_string(),
1053+
}
1054+
.into(),
1055+
),
10541056
BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
10551057
UnresolvedProcMacro {
10561058
node: node.clone().map(|it| it.into()),

crates/hir_def/src/body/tests.rs

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -88,67 +88,6 @@ mod m {
8888
);
8989
}
9090

91-
#[test]
92-
fn macro_diag_builtin() {
93-
check_diagnostics(
94-
r#"
95-
#[rustc_builtin_macro]
96-
macro_rules! env {}
97-
98-
#[rustc_builtin_macro]
99-
macro_rules! include {}
100-
101-
#[rustc_builtin_macro]
102-
macro_rules! compile_error {}
103-
104-
#[rustc_builtin_macro]
105-
macro_rules! format_args {
106-
() => {}
107-
}
108-
109-
fn f() {
110-
// Test a handful of built-in (eager) macros:
111-
112-
include!(invalid);
113-
//^^^^^^^^^^^^^^^^^ could not convert tokens
114-
include!("does not exist");
115-
//^^^^^^^^^^^^^^^^^^^^^^^^^^ failed to load file `does not exist`
116-
117-
env!(invalid);
118-
//^^^^^^^^^^^^^ could not convert tokens
119-
120-
env!("OUT_DIR");
121-
//^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "run build scripts" to fix
122-
123-
compile_error!("compile_error works");
124-
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ compile_error works
125-
126-
// Lazy:
127-
128-
format_args!();
129-
//^^^^^^^^^^^^^^ no rule matches input tokens
130-
}
131-
"#,
132-
);
133-
}
134-
135-
#[test]
136-
fn macro_rules_diag() {
137-
check_diagnostics(
138-
r#"
139-
macro_rules! m {
140-
() => {};
141-
}
142-
fn f() {
143-
m!();
144-
145-
m!(hi);
146-
//^^^^^^ leftover tokens
147-
}
148-
"#,
149-
);
150-
}
151-
15291
#[test]
15392
fn unresolved_macro_diag() {
15493
check_diagnostics(
@@ -161,30 +100,3 @@ fn f() {
161100
);
162101
}
163102

164-
#[test]
165-
fn dollar_crate_in_builtin_macro() {
166-
check_diagnostics(
167-
r#"
168-
#[macro_export]
169-
#[rustc_builtin_macro]
170-
macro_rules! format_args {}
171-
172-
#[macro_export]
173-
macro_rules! arg {
174-
() => {}
175-
}
176-
177-
#[macro_export]
178-
macro_rules! outer {
179-
() => {
180-
$crate::format_args!( "", $crate::arg!(1) )
181-
};
182-
}
183-
184-
fn f() {
185-
outer!();
186-
//^^^^^^^^ leftover tokens
187-
}
188-
"#,
189-
)
190-
}

crates/hir_def/src/nameres/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod globs;
22
mod incremental;
33
mod macros;
44
mod mod_resolution;
5-
mod diagnostics;
65
mod primitives;
76

87
use std::sync::Arc;

crates/hir_def/src/nameres/tests/diagnostics.rs

Lines changed: 0 additions & 76 deletions
This file was deleted.

crates/ide/src/diagnostics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod unresolved_extern_crate;
99
mod unresolved_import;
1010
mod unresolved_macro_call;
1111
mod unresolved_proc_macro;
12+
mod macro_error;
1213
mod inactive_code;
1314
mod missing_fields;
1415

@@ -229,6 +230,7 @@ pub(crate) fn diagnostics(
229230
AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d),
230231
AnyDiagnostic::UnresolvedProcMacro(d) => unresolved_proc_macro::unresolved_proc_macro(&ctx, &d),
231232
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
233+
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
232234

233235
AnyDiagnostic::InactiveCode(d) => match inactive_code::inactive_code(&ctx, &d) {
234236
Some(it) => it,

0 commit comments

Comments
 (0)