Skip to content

Commit 3928289

Browse files
committed
Use callsite of macro expansions in 'push_expected_errors' (closes issue Manishearth#48)
1 parent cd0586b commit 3928289

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/json.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ struct DiagnosticSpan {
3939
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
4040
}
4141

42+
impl DiagnosticSpan {
43+
/// Return the source span - this is either the supplied span, or the span for
44+
/// the macro callsite that expanded to it.
45+
fn callsite(&self) -> &DiagnosticSpan {
46+
self.expansion.as_ref().map(|origin| origin.span.callsite()).unwrap_or(self)
47+
}
48+
}
49+
4250
#[derive(Deserialize, Clone)]
4351
struct DiagnosticSpanMacroExpansion {
4452
/// span where macro was applied to generate this code
@@ -48,6 +56,7 @@ struct DiagnosticSpanMacroExpansion {
4856
macro_decl_name: String,
4957
}
5058

59+
5160
#[derive(Deserialize, Clone)]
5261
struct DiagnosticCode {
5362
/// The code itself.
@@ -89,15 +98,22 @@ fn push_expected_errors(expected_errors: &mut Vec<Error>,
8998
diagnostic: &Diagnostic,
9099
default_spans: &[&DiagnosticSpan],
91100
file_name: &str) {
92-
let spans_in_this_file: Vec<_> = diagnostic.spans
101+
// In case of macro expansions, we need to get the span of the call site
102+
let spans_info_in_this_file: Vec<_> = diagnostic.spans
93103
.iter()
94-
.filter(|span| Path::new(&span.file_name) == Path::new(&file_name))
104+
.map(|span| (span.is_primary, span.callsite()))
105+
.filter(|(_, span)| Path::new(&span.file_name) == Path::new(&file_name))
95106
.collect();
96107

97-
let primary_spans: Vec<_> = spans_in_this_file.iter()
98-
.cloned()
99-
.filter(|span| span.is_primary)
108+
let spans_in_this_file: Vec<_> = spans_info_in_this_file.iter()
109+
.map(|(_, span)| span)
110+
.collect();
111+
112+
let primary_spans: Vec<_> = spans_info_in_this_file.iter()
113+
.filter(|(is_primary, _)| *is_primary)
114+
.map(|(_, span)| span)
100115
.take(1) // sometimes we have more than one showing up in the json; pick first
116+
.cloned()
101117
.collect();
102118
let primary_spans = if primary_spans.is_empty() {
103119
// subdiagnostics often don't have a span of their own;

0 commit comments

Comments
 (0)