Skip to content
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

Match errors using the callsite of macro expansions #119

Merged
merged 1 commit into from
Jul 26, 2018
Merged
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
33 changes: 28 additions & 5 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ struct DiagnosticSpan {
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
}

impl DiagnosticSpan {
/// Returns the deepest source span in the macro call stack with a given file name.
/// This is either the supplied span, or the span for some macro callsite that expanded to it.
fn first_callsite_in_file(&self, file_name: &str) -> &DiagnosticSpan {
if self.file_name == file_name {
self
} else {
self.expansion
.as_ref()
.map(|origin| origin.span.first_callsite_in_file(file_name))
.unwrap_or(self)
}
}
}

#[derive(Deserialize, Clone)]
struct DiagnosticSpanMacroExpansion {
/// span where macro was applied to generate this code
Expand Down Expand Up @@ -89,15 +104,23 @@ 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 callsite
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.first_callsite_in_file(file_name)))
.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
6 changes: 5 additions & 1 deletion src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,10 @@ actual:\n\
self.fatal_proc_rec("process did not return an error status", proc_res);
}

// On Windows, keep all '\' path separators to match the paths reported in the JSON output
// from the compiler
let os_file_name = self.testpaths.file.display().to_string();

let file_name =
format!("{}", self.testpaths.file.display())
.replace(r"\", "/"); // on windows, translate all '\' path separators to '/'
Expand All @@ -1020,7 +1024,7 @@ actual:\n\
let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));

// Parse the JSON output from the compiler and extract out the messages.
let actual_errors = json::parse_output(&file_name, &proc_res.stderr, proc_res);
let actual_errors = json::parse_output(&os_file_name, &proc_res.stderr, proc_res);
let mut unexpected = Vec::new();
let mut found = vec![false; expected_errors.len()];
for actual_error in &actual_errors {
Expand Down
27 changes: 27 additions & 0 deletions test-project/tests/compile-fail/macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! macro_with_error {
( ) => {
println!("{"); //~ ERROR invalid
};
}

fn foo() {

}

fn main() {
macro_with_error!();
//^ In case of a local macro we want the error to be matched in the macro definition, not here

println!("}"); //~ ERROR invalid
//^ In case of an external macro we want the error to be matched here
}