Skip to content

Commit

Permalink
expand: Give reasonable NodeIds to lints associated with macro expans…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
petrochenkov committed Jun 9, 2020
1 parent feb3536 commit 73d5cb0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/librustc_builtin_macros/source_util.rs
Expand Up @@ -122,6 +122,7 @@ pub fn expand_include<'cx>(

struct ExpandResult<'a> {
p: Parser<'a>,
node_id: ast::NodeId,
}
impl<'a> base::MacResult for ExpandResult<'a> {
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
Expand All @@ -130,7 +131,7 @@ pub fn expand_include<'cx>(
self.p.sess.buffer_lint(
&INCOMPLETE_INCLUDE,
self.p.token.span,
ast::CRATE_NODE_ID,
self.node_id,
"include macro expected single expression in source",
);
}
Expand Down Expand Up @@ -158,7 +159,7 @@ pub fn expand_include<'cx>(
}
}

Box::new(ExpandResult { p })
Box::new(ExpandResult { p, node_id: cx.resolver.lint_node_id(cx.current_expansion.id) })
}

// include_str! : read the given file, insert it as a literal string expr
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_expand/base.rs
Expand Up @@ -915,6 +915,9 @@ pub trait Resolver {

fn check_unused_macros(&mut self);

/// Some parent node that is close enough to the given macro call.
fn lint_node_id(&mut self, expn_id: ExpnId) -> NodeId;

fn has_derive_copy(&self, expn_id: ExpnId) -> bool;
fn add_derive_copy(&mut self, expn_id: ExpnId);
fn cfg_accessible(&mut self, expn_id: ExpnId, path: &ast::Path) -> Result<bool, Indeterminate>;
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_hir/definitions.rs
Expand Up @@ -519,6 +519,12 @@ impl Definitions {
let old_index = self.placeholder_field_indices.insert(node_id, index);
assert!(old_index.is_none(), "placeholder field index is reset for a node ID");
}

pub fn lint_node_id(&mut self, expn_id: ExpnId) -> ast::NodeId {
self.invocation_parents
.get(&expn_id)
.map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[*id])
}
}

impl DefPathData {
Expand Down
18 changes: 14 additions & 4 deletions src/librustc_resolve/macros.rs
Expand Up @@ -288,7 +288,8 @@ impl<'a> base::Resolver for Resolver<'a> {

// Derives are not included when `invocations` are collected, so we have to add them here.
let parent_scope = &ParentScope { derives, ..parent_scope };
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?;
let node_id = self.lint_node_id(eager_expansion_root);
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, node_id, force)?;

let span = invoc.span();
invoc_id.set_expn_data(ext.expn_data(
Expand Down Expand Up @@ -338,6 +339,10 @@ impl<'a> base::Resolver for Resolver<'a> {
}
}

fn lint_node_id(&mut self, expn_id: ExpnId) -> NodeId {
self.definitions.lint_node_id(expn_id)
}

fn has_derive_copy(&self, expn_id: ExpnId) -> bool {
self.containers_deriving_copy.contains(&expn_id)
}
Expand Down Expand Up @@ -390,6 +395,7 @@ impl<'a> Resolver<'a> {
path: &ast::Path,
kind: MacroKind,
parent_scope: &ParentScope<'a>,
node_id: NodeId,
force: bool,
) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope, true, force)
Expand Down Expand Up @@ -430,7 +436,7 @@ impl<'a> Resolver<'a> {
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
};

self.check_stability_and_deprecation(&ext, path);
self.check_stability_and_deprecation(&ext, path, node_id);

Ok(if ext.macro_kind() != kind {
let expected = kind.descr_expected();
Expand Down Expand Up @@ -984,13 +990,17 @@ impl<'a> Resolver<'a> {
}
}

fn check_stability_and_deprecation(&mut self, ext: &SyntaxExtension, path: &ast::Path) {
fn check_stability_and_deprecation(
&mut self,
ext: &SyntaxExtension,
path: &ast::Path,
node_id: NodeId,
) {
let span = path.span;
if let Some(stability) = &ext.stability {
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
let feature = stability.feature;
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
let node_id = ast::CRATE_NODE_ID;
let lint_buffer = &mut self.lint_buffer;
let soft_handler =
|lint, span, msg: &_| lint_buffer.buffer_lint(lint, node_id, span, msg);
Expand Down
1 change: 1 addition & 0 deletions src/librustc_session/lint/builtin.rs
Expand Up @@ -606,6 +606,7 @@ declare_lint_pass! {
INLINE_NO_SANITIZE,
ASM_SUB_REGISTER,
UNSAFE_OP_IN_UNSAFE_FN,
INCOMPLETE_INCLUDE,
]
}

Expand Down

0 comments on commit 73d5cb0

Please sign in to comment.