From 73d5cb0a456116f142e773a803715bbdb25988b1 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 9 Jun 2020 20:59:43 +0300 Subject: [PATCH] expand: Give reasonable NodeIds to lints associated with macro expansions --- src/librustc_builtin_macros/source_util.rs | 5 +++-- src/librustc_expand/base.rs | 3 +++ src/librustc_hir/definitions.rs | 6 ++++++ src/librustc_resolve/macros.rs | 18 ++++++++++++++---- src/librustc_session/lint/builtin.rs | 1 + 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/librustc_builtin_macros/source_util.rs b/src/librustc_builtin_macros/source_util.rs index 67145c6bf433..1b164eae5a34 100644 --- a/src/librustc_builtin_macros/source_util.rs +++ b/src/librustc_builtin_macros/source_util.rs @@ -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>) -> Option> { @@ -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 diff --git a/src/librustc_expand/base.rs b/src/librustc_expand/base.rs index 13637e58c936..a57ae798ffce 100644 --- a/src/librustc_expand/base.rs +++ b/src/librustc_expand/base.rs @@ -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; diff --git a/src/librustc_hir/definitions.rs b/src/librustc_hir/definitions.rs index 2dd5e27ead26..b63dd653c4dd 100644 --- a/src/librustc_hir/definitions.rs +++ b/src/librustc_hir/definitions.rs @@ -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 { diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 394d8dc4e113..1b49722355e5 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -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, 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); diff --git a/src/librustc_session/lint/builtin.rs b/src/librustc_session/lint/builtin.rs index bb0d6e1a47ea..58388bafbedd 100644 --- a/src/librustc_session/lint/builtin.rs +++ b/src/librustc_session/lint/builtin.rs @@ -606,6 +606,7 @@ declare_lint_pass! { INLINE_NO_SANITIZE, ASM_SUB_REGISTER, UNSAFE_OP_IN_UNSAFE_FN, + INCOMPLETE_INCLUDE, ] }