expand: Give reasonable NodeIds to lints associated with macro expansions

This commit is contained in:
Vadim Petrochenkov 2020-06-09 20:59:43 +03:00
parent feb3536eba
commit 73d5cb0a45
5 changed files with 27 additions and 6 deletions

View file

@ -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>> {
@ -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",
);
}
@ -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

View file

@ -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>;

View file

@ -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 {

View file

@ -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(
@ -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)
}
@ -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)
@ -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();
@ -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);

View file

@ -606,6 +606,7 @@ declare_lint_pass! {
INLINE_NO_SANITIZE,
ASM_SUB_REGISTER,
UNSAFE_OP_IN_UNSAFE_FN,
INCOMPLETE_INCLUDE,
]
}