Skip to content

Consider callsite of macro expansions #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ struct DiagnosticSpan {
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
}

impl DiagnosticSpan {
/// Return the source span - this is either the supplied span, or the span for
/// the macro callsite that expanded to it.
fn callsite(&self) -> &DiagnosticSpan {
self.expansion.as_ref().map(|origin| origin.span.callsite()).unwrap_or(self)
}
}

#[derive(Deserialize, Clone)]
struct DiagnosticSpanMacroExpansion {
/// span where macro was applied to generate this code
Expand All @@ -48,6 +56,7 @@ struct DiagnosticSpanMacroExpansion {
macro_decl_name: String,
}


#[derive(Deserialize, Clone)]
struct DiagnosticCode {
/// The code itself.
Expand Down Expand Up @@ -89,15 +98,22 @@ fn push_expected_errors(expected_errors: &mut Vec<Error>,
diagnostic: &Diagnostic,
default_spans: &[&DiagnosticSpan],
file_name: &str) {
let spans_in_this_file: Vec<_> = diagnostic.spans
// In case of macro expansions, we need to get the span of the call site
let spans_info_in_this_file: Vec<_> = diagnostic.spans
.iter()
.filter(|span| Path::new(&span.file_name) == Path::new(&file_name))
.map(|span| (span.is_primary, span.callsite()))
.filter(|(_, span)| Path::new(&span.file_name) == Path::new(&file_name))
.collect();

let primary_spans: Vec<_> = spans_in_this_file.iter()
.cloned()
.filter(|span| span.is_primary)
let spans_in_this_file: Vec<_> = spans_info_in_this_file.iter()
.map(|(_, span)| span)
.collect();

let primary_spans: Vec<_> = spans_info_in_this_file.iter()
.filter(|(is_primary, _)| *is_primary)
.map(|(_, span)| span)
.take(1) // sometimes we have more than one showing up in the json; pick first
.cloned()
.collect();
let primary_spans = if primary_spans.is_empty() {
// subdiagnostics often don't have a span of their own;
Expand Down
9 changes: 9 additions & 0 deletions test-project/tests/compile-fail/macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
macro_rules! macro_with_error {
( ) => {
let x: u64 = true;
};
}

fn main() {
macro_with_error!(); //~ ERROR mismatched types
}