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

Point macros 1.1 errors to the input item #36308

Merged
merged 3 commits into from
Sep 12, 2016
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
23 changes: 18 additions & 5 deletions src/libsyntax_ext/deriving/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

use std::panic;

use errors::FatalError;
use rustc_macro::{TokenStream, __internal};
use syntax::ast::{self, ItemKind};
use syntax::codemap::Span;
use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan, Span};
use syntax::ext::base::*;
use syntax::fold::{self, Folder};
use errors::FatalError;
use syntax::parse::token::intern;
use syntax::print::pprust;

pub struct CustomDerive {
inner: fn(TokenStream) -> TokenStream,
Expand All @@ -31,7 +33,7 @@ impl MultiItemModifier for CustomDerive {
fn expand(&self,
ecx: &mut ExtCtxt,
span: Span,
_meta_item: &ast::MetaItem,
meta_item: &ast::MetaItem,
item: Annotatable)
-> Vec<Annotatable> {
let item = match item {
Expand All @@ -53,6 +55,17 @@ impl MultiItemModifier for CustomDerive {
}
}

let input_span = Span {
expn_id: ecx.codemap().record_expansion(ExpnInfo {
call_site: span,
callee: NameAndSpan {
format: MacroAttribute(intern(&pprust::meta_item_to_string(meta_item))),
span: Some(span),
allow_internal_unstable: true,
},
}),
..item.span
};
let input = __internal::new_token_stream(item);
let res = __internal::set_parse_sess(&ecx.parse_sess, || {
let inner = self.inner;
Expand All @@ -77,9 +90,9 @@ impl MultiItemModifier for CustomDerive {

// Right now we have no knowledge of spans at all in custom derive
// macros, everything is just parsed as a string. Reassign all spans to
// the #[derive] attribute for better errors here.
// the input `item` for better errors here.
item.into_iter().flat_map(|item| {
ChangeSpan { span: span }.fold_item(item)
ChangeSpan { span: input_span }.fold_item(item)
}).map(Annotatable::Item).collect()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail-fulldeps/rustc-macro/append-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ trait Append {
#[derive(PartialEq,
Append,
Eq)]
//~^^ ERROR: the semantics of constant patterns is not yet settled
struct A {
//~^ ERROR: the semantics of constant patterns is not yet settled
inner: u32,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
extern crate derive_unstable_2;

#[derive(Unstable)]
//~^ ERROR: reserved for internal compiler
struct A;
//~^ ERROR: reserved for internal compiler

fn main() {
foo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
extern crate derive_unstable;

#[derive(Unstable)]
//~^ ERROR: use of unstable library feature
struct A;
//~^ ERROR: use of unstable library feature

fn main() {
unsafe { foo(); }
Expand Down