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

improve span of erroneous empty macro invocation #27584

Merged
merged 2 commits into from
Aug 12, 2015
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
8 changes: 2 additions & 6 deletions src/librustc/middle/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use middle::ty::{self, ToPredicate, HasTypeFlags, ToPolyTraitRef, TraitRef};
use middle::ty_fold::TypeFoldable;
use std::collections::HashMap;
use std::fmt;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::codemap::Span;
use syntax::attr::{AttributeMethods, AttrMetaMethods};

pub fn report_fulfillment_errors<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
Expand Down Expand Up @@ -81,11 +81,7 @@ fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
let mut report = None;
for item in infcx.tcx.get_attrs(def_id).iter() {
if item.check_name("rustc_on_unimplemented") {
let err_sp = if item.meta().span == DUMMY_SP {
span
} else {
item.meta().span
};
let err_sp = item.meta().span.substitute_dummy(span);
let def = infcx.tcx.lookup_trait_def(def_id);
let trait_str = def.trait_ref.to_string();
if let Some(ref istring) = item.value_str() {
Expand Down
7 changes: 7 additions & 0 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
hi: BytePos(0),
expn_id: COMMAND_LINE_EXPN };

impl Span {
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
pub fn substitute_dummy(self, other: Span) -> Span {
if self == DUMMY_SP { other } else { self }
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub struct Spanned<T> {
pub node: T,
Expand Down
16 changes: 0 additions & 16 deletions src/libsyntax/ext/tt/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,6 @@ pub enum ParseResult<T> {
pub type NamedParseResult = ParseResult<HashMap<Ident, Rc<NamedMatch>>>;
pub type PositionalParseResult = ParseResult<Vec<Rc<NamedMatch>>>;

pub fn parse_or_else(sess: &ParseSess,
cfg: ast::CrateConfig,
rdr: TtReader,
ms: Vec<TokenTree> )
-> HashMap<Ident, Rc<NamedMatch>> {
match parse(sess, cfg, rdr, &ms[..]) {
Success(m) => m,
Failure(sp, str) => {
panic!(sess.span_diagnostic.span_fatal(sp, &str[..]))
}
Error(sp, str) => {
panic!(sess.span_diagnostic.span_fatal(sp, &str[..]))
}
}
}

/// Perform a token equality check, ignoring syntax context (that is, an
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this function could be changed to take in a substitute_span: Span and have the DUMMY_SP comparison internally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I choose to remove it because

  1. It was only called in one place
  2. It only forwarded it's arguments to parse and panicked on failure

Taking this into account I don't think the existence of this function is really justified, especially since I would have had to add another parameter to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your reasoning sounds good to me.

/// unhygienic comparison)
pub fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {
Expand Down
24 changes: 17 additions & 7 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ext::base::{ExtCtxt, MacResult, SyntaxExtension};
use ext::base::{NormalTT, TTMacroExpander};
use ext::tt::macro_parser::{Success, Error, Failure};
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
use ext::tt::macro_parser::{parse, parse_or_else};
use ext::tt::macro_parser::parse;
use parse::lexer::new_tt_reader;
use parse::parser::Parser;
use parse::token::{self, special_idents, gensym_ident, NtTT, Token};
Expand Down Expand Up @@ -211,13 +211,16 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
best_fail_spot = sp;
best_fail_msg = (*msg).clone();
},
Error(sp, ref msg) => panic!(cx.span_fatal(sp, &msg[..]))
Error(err_sp, ref msg) => {
panic!(cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..]))
}
}
}
_ => cx.bug("non-matcher found in parsed lhses")
}
}
panic!(cx.span_fatal(best_fail_spot, &best_fail_msg[..]));

panic!(cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]));
}

// Note that macro-by-example's input is also matched against a token tree:
Expand Down Expand Up @@ -266,10 +269,17 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
None,
None,
def.body.clone());
let argument_map = parse_or_else(cx.parse_sess(),
cx.cfg(),
arg_reader,
argument_gram);

let argument_map = match parse(cx.parse_sess(),
cx.cfg(),
arg_reader,
&argument_gram) {
Success(m) => m,
Failure(sp, str) | Error(sp, str) => {
panic!(cx.parse_sess().span_diagnostic
.span_fatal(sp.substitute_dummy(def.span), &str[..]));
}
};

// Extract the arguments:
let lhses = match **argument_map.get(&lhs_nm).unwrap() {
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-7970a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2015 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.

fn main() {
println!();
//~^ ERROR unexpected end of macro invocation
}
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-7970b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2015 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.

fn main() {}

macro_rules! test {}
//~^ ERROR unexpected end of macro invocation