Skip to content

Commit 73d5cb0

Browse files
committed
expand: Give reasonable NodeIds to lints associated with macro expansions
1 parent feb3536 commit 73d5cb0

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/librustc_builtin_macros/source_util.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub fn expand_include<'cx>(
122122

123123
struct ExpandResult<'a> {
124124
p: Parser<'a>,
125+
node_id: ast::NodeId,
125126
}
126127
impl<'a> base::MacResult for ExpandResult<'a> {
127128
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
@@ -130,7 +131,7 @@ pub fn expand_include<'cx>(
130131
self.p.sess.buffer_lint(
131132
&INCOMPLETE_INCLUDE,
132133
self.p.token.span,
133-
ast::CRATE_NODE_ID,
134+
self.node_id,
134135
"include macro expected single expression in source",
135136
);
136137
}
@@ -158,7 +159,7 @@ pub fn expand_include<'cx>(
158159
}
159160
}
160161

161-
Box::new(ExpandResult { p })
162+
Box::new(ExpandResult { p, node_id: cx.resolver.lint_node_id(cx.current_expansion.id) })
162163
}
163164

164165
// include_str! : read the given file, insert it as a literal string expr

src/librustc_expand/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,9 @@ pub trait Resolver {
915915

916916
fn check_unused_macros(&mut self);
917917

918+
/// Some parent node that is close enough to the given macro call.
919+
fn lint_node_id(&mut self, expn_id: ExpnId) -> NodeId;
920+
918921
fn has_derive_copy(&self, expn_id: ExpnId) -> bool;
919922
fn add_derive_copy(&mut self, expn_id: ExpnId);
920923
fn cfg_accessible(&mut self, expn_id: ExpnId, path: &ast::Path) -> Result<bool, Indeterminate>;

src/librustc_hir/definitions.rs

+6
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,12 @@ impl Definitions {
519519
let old_index = self.placeholder_field_indices.insert(node_id, index);
520520
assert!(old_index.is_none(), "placeholder field index is reset for a node ID");
521521
}
522+
523+
pub fn lint_node_id(&mut self, expn_id: ExpnId) -> ast::NodeId {
524+
self.invocation_parents
525+
.get(&expn_id)
526+
.map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[*id])
527+
}
522528
}
523529

524530
impl DefPathData {

src/librustc_resolve/macros.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ impl<'a> base::Resolver for Resolver<'a> {
288288

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

293294
let span = invoc.span();
294295
invoc_id.set_expn_data(ext.expn_data(
@@ -338,6 +339,10 @@ impl<'a> base::Resolver for Resolver<'a> {
338339
}
339340
}
340341

342+
fn lint_node_id(&mut self, expn_id: ExpnId) -> NodeId {
343+
self.definitions.lint_node_id(expn_id)
344+
}
345+
341346
fn has_derive_copy(&self, expn_id: ExpnId) -> bool {
342347
self.containers_deriving_copy.contains(&expn_id)
343348
}
@@ -390,6 +395,7 @@ impl<'a> Resolver<'a> {
390395
path: &ast::Path,
391396
kind: MacroKind,
392397
parent_scope: &ParentScope<'a>,
398+
node_id: NodeId,
393399
force: bool,
394400
) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
395401
let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope, true, force)
@@ -430,7 +436,7 @@ impl<'a> Resolver<'a> {
430436
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
431437
};
432438

433-
self.check_stability_and_deprecation(&ext, path);
439+
self.check_stability_and_deprecation(&ext, path, node_id);
434440

435441
Ok(if ext.macro_kind() != kind {
436442
let expected = kind.descr_expected();
@@ -984,13 +990,17 @@ impl<'a> Resolver<'a> {
984990
}
985991
}
986992

987-
fn check_stability_and_deprecation(&mut self, ext: &SyntaxExtension, path: &ast::Path) {
993+
fn check_stability_and_deprecation(
994+
&mut self,
995+
ext: &SyntaxExtension,
996+
path: &ast::Path,
997+
node_id: NodeId,
998+
) {
988999
let span = path.span;
9891000
if let Some(stability) = &ext.stability {
9901001
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
9911002
let feature = stability.feature;
9921003
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
993-
let node_id = ast::CRATE_NODE_ID;
9941004
let lint_buffer = &mut self.lint_buffer;
9951005
let soft_handler =
9961006
|lint, span, msg: &_| lint_buffer.buffer_lint(lint, node_id, span, msg);

src/librustc_session/lint/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ declare_lint_pass! {
606606
INLINE_NO_SANITIZE,
607607
ASM_SUB_REGISTER,
608608
UNSAFE_OP_IN_UNSAFE_FN,
609+
INCOMPLETE_INCLUDE,
609610
]
610611
}
611612

0 commit comments

Comments
 (0)