Skip to content

Commit

Permalink
expand: Give reasonable NodeIds to lints associated with macro defini…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
petrochenkov committed Jun 9, 2020
1 parent 73d5cb0 commit 217a745
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/librustc_expand/mbe/macro_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
//! bound.
use crate::mbe::{KleeneToken, TokenTree};

use rustc_ast::ast::NodeId;
use rustc_ast::ast::{NodeId, DUMMY_NODE_ID};
use rustc_ast::token::{DelimToken, Token, TokenKind};
use rustc_data_structures::fx::FxHashMap;
use rustc_session::lint::builtin::META_VARIABLE_MISUSE;
Expand Down Expand Up @@ -626,5 +626,8 @@ fn ops_is_prefix(
}

fn buffer_lint(sess: &ParseSess, span: MultiSpan, node_id: NodeId, message: &str) {
sess.buffer_lint(&META_VARIABLE_MISUSE, span, node_id, message);
// Macros loaded from other crates have dummy node ids.
if node_id != DUMMY_NODE_ID {
sess.buffer_lint(&META_VARIABLE_MISUSE, span, node_id, message);
}
}
4 changes: 2 additions & 2 deletions src/librustc_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
}
}
TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => {
if sess.missing_fragment_specifiers.borrow_mut().remove(&span) {
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
return Err((span, "missing fragment specifier".to_string()));
}
}
Expand Down Expand Up @@ -566,7 +566,7 @@ fn inner_parse_loop<'root, 'tt>(

// We need to match a metavar (but the identifier is invalid)... this is an error
TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => {
if sess.missing_fragment_specifiers.borrow_mut().remove(&span) {
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
return Error(span, "missing fragment specifier".to_string());
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/librustc_expand/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ pub fn compile_declarative_macro(
.map(|m| {
if let MatchedNonterminal(ref nt) = *m {
if let NtTT(ref tt) = **nt {
let tt = mbe::quoted::parse(tt.clone().into(), true, sess).pop().unwrap();
let tt = mbe::quoted::parse(tt.clone().into(), true, sess, def.id)
.pop()
.unwrap();
valid &= check_lhs_nt_follows(sess, features, &def.attrs, &tt);
return tt;
}
Expand All @@ -491,7 +493,9 @@ pub fn compile_declarative_macro(
.map(|m| {
if let MatchedNonterminal(ref nt) = *m {
if let NtTT(ref tt) = **nt {
return mbe::quoted::parse(tt.clone().into(), false, sess).pop().unwrap();
return mbe::quoted::parse(tt.clone().into(), false, sess, def.id)
.pop()
.unwrap();
}
}
sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
Expand All @@ -509,9 +513,7 @@ pub fn compile_declarative_macro(
valid &= check_lhs_no_empty_seq(sess, slice::from_ref(lhs));
}

// We use CRATE_NODE_ID instead of `def.id` otherwise we may emit buffered lints for a node id
// that is not lint-checked and trigger the "failed to process buffered lint here" bug.
valid &= macro_check::check_meta_variables(sess, ast::CRATE_NODE_ID, def.span, &lhses, &rhses);
valid &= macro_check::check_meta_variables(sess, def.id, def.span, &lhses, &rhses);

let (transparency, transparency_error) = attr::find_transparency(&def.attrs, macro_rules);
match transparency_error {
Expand Down
14 changes: 10 additions & 4 deletions src/librustc_expand/mbe/quoted.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::mbe::macro_parser;
use crate::mbe::{Delimited, KleeneOp, KleeneToken, SequenceRepetition, TokenTree};

use rustc_ast::ast::{NodeId, DUMMY_NODE_ID};
use rustc_ast::token::{self, Token};
use rustc_ast::tokenstream;
use rustc_ast_pretty::pprust;
Expand Down Expand Up @@ -36,6 +37,7 @@ pub(super) fn parse(
input: tokenstream::TokenStream,
expect_matchers: bool,
sess: &ParseSess,
node_id: NodeId,
) -> Vec<TokenTree> {
// Will contain the final collection of `self::TokenTree`
let mut result = Vec::new();
Expand All @@ -46,7 +48,7 @@ pub(super) fn parse(
while let Some(tree) = trees.next() {
// Given the parsed tree, if there is a metavar and we are expecting matchers, actually
// parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
let tree = parse_tree(tree, &mut trees, expect_matchers, sess);
let tree = parse_tree(tree, &mut trees, expect_matchers, sess, node_id);
match tree {
TokenTree::MetaVar(start_sp, ident) if expect_matchers => {
let span = match trees.next() {
Expand All @@ -65,7 +67,10 @@ pub(super) fn parse(
}
tree => tree.as_ref().map(tokenstream::TokenTree::span).unwrap_or(start_sp),
};
sess.missing_fragment_specifiers.borrow_mut().insert(span);
if node_id != DUMMY_NODE_ID {
// Macros loaded from other crates have dummy node ids.
sess.missing_fragment_specifiers.borrow_mut().insert(span, node_id);
}
result.push(TokenTree::MetaVarDecl(span, ident, Ident::invalid()));
}

Expand Down Expand Up @@ -96,6 +101,7 @@ fn parse_tree(
trees: &mut impl Iterator<Item = tokenstream::TokenTree>,
expect_matchers: bool,
sess: &ParseSess,
node_id: NodeId,
) -> TokenTree {
// Depending on what `tree` is, we could be parsing different parts of a macro
match tree {
Expand All @@ -111,7 +117,7 @@ fn parse_tree(
sess.span_diagnostic.span_err(span.entire(), &msg);
}
// Parse the contents of the sequence itself
let sequence = parse(tts, expect_matchers, sess);
let sequence = parse(tts, expect_matchers, sess, node_id);
// Get the Kleene operator and optional separator
let (separator, kleene) = parse_sep_and_kleene_op(trees, span.entire(), sess);
// Count the number of captured "names" (i.e., named metavars)
Expand Down Expand Up @@ -158,7 +164,7 @@ fn parse_tree(
// descend into the delimited set and further parse it.
tokenstream::TokenTree::Delimited(span, delim, tts) => TokenTree::Delimited(
span,
Lrc::new(Delimited { delim, tts: parse(tts, expect_matchers, sess) }),
Lrc::new(Delimited { delim, tts: parse(tts, expect_matchers, sess, node_id) }),
),
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,21 @@ fn configure_and_expand_inner<'a>(
ecx.check_unused_macros();
});

let mut missing_fragment_specifiers: Vec<_> =
ecx.parse_sess.missing_fragment_specifiers.borrow().iter().cloned().collect();
missing_fragment_specifiers.sort();
let mut missing_fragment_specifiers: Vec<_> = ecx
.parse_sess
.missing_fragment_specifiers
.borrow()
.iter()
.map(|(span, node_id)| (*span, *node_id))
.collect();
missing_fragment_specifiers.sort_unstable_by_key(|(span, _)| *span);

let recursion_limit_hit = ecx.reduced_recursion_limit.is_some();

for span in missing_fragment_specifiers {
for (span, node_id) in missing_fragment_specifiers {
let lint = lint::builtin::MISSING_FRAGMENT_SPECIFIER;
let msg = "missing fragment specifier";
resolver.lint_buffer().buffer_lint(lint, ast::CRATE_NODE_ID, span, msg);
resolver.lint_buffer().buffer_lint(lint, node_id, span, msg);
}
if cfg!(windows) {
env::set_var("PATH", &old_path);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_session/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub struct ParseSess {
pub unstable_features: UnstableFeatures,
pub config: CrateConfig,
pub edition: Edition,
pub missing_fragment_specifiers: Lock<FxHashSet<Span>>,
pub missing_fragment_specifiers: Lock<FxHashMap<Span, NodeId>>,
/// Places where raw identifiers were used. This is used for feature-gating raw identifiers.
pub raw_identifier_spans: Lock<Vec<Span>>,
/// Used to determine and report recursive module inclusions.
Expand Down Expand Up @@ -150,7 +150,7 @@ impl ParseSess {
unstable_features: UnstableFeatures::from_environment(),
config: FxHashSet::default(),
edition: ExpnId::root().expn_data().edition,
missing_fragment_specifiers: Lock::new(FxHashSet::default()),
missing_fragment_specifiers: Default::default(),
raw_identifier_spans: Lock::new(Vec::new()),
included_mod_stack: Lock::new(vec![]),
source_map,
Expand Down

0 comments on commit 217a745

Please sign in to comment.