diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index e3504a5638e47..bc133ebebaaad 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -396,8 +396,10 @@ pub fn visit_attr_args(args: &mut AttrArgs, vis: &mut T) { vis.visit_span(eq_span); vis.visit_expr(expr); } - AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => { - unreachable!("in literal form when visiting mac args eq: {:?}", lit) + AttrArgs::Eq(eq_span, AttrArgsEq::Hir(lit)) => { + vis.visit_span(eq_span); + let MetaItemLit { symbol: _, suffix: _, kind: _, span } = lit; + vis.visit_span(span); } } } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index ddbbf5a10bcdd..1083a0149d1b1 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -206,8 +206,14 @@ pub trait Visitor<'ast>: Sized { fn visit_path(&mut self, path: &'ast Path, _id: NodeId) { walk_path(self, path) } - fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) { - walk_use_tree(self, use_tree, id) + fn visit_use_tree( + &mut self, + use_tree: &'ast UseTree, + id: NodeId, + _nested: bool, + item_span: Span, + ) { + walk_use_tree(self, use_tree, id, item_span) } fn visit_path_segment(&mut self, path_segment: &'ast PathSegment) { walk_path_segment(self, path_segment) @@ -307,7 +313,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { visitor.visit_ident(item.ident); match &item.kind { ItemKind::ExternCrate(_) => {} - ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false), + ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false, item.span), ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => { visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); @@ -450,7 +456,12 @@ pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) { } } -pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) { +pub fn walk_use_tree<'a, V: Visitor<'a>>( + visitor: &mut V, + use_tree: &'a UseTree, + id: NodeId, + item_span: Span, +) { visitor.visit_path(&use_tree.prefix, id); match &use_tree.kind { UseTreeKind::Simple(rename) => { @@ -462,7 +473,7 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, UseTreeKind::Glob => {} UseTreeKind::Nested(use_trees) => { for &(ref nested_tree, nested_id) in use_trees { - visitor.visit_use_tree(nested_tree, nested_id, true); + visitor.visit_use_tree(nested_tree, nested_id, true, item_span); } } } diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index a4ba1a5c9bf48..08e87d76bbaf4 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -902,8 +902,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { })), AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data), }; + let mut attr = Attribute { kind, id: attr.id, style: attr.style, span: attr.span }; - Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) } + use rustc_ast::mut_visit::MutVisitor; + struct SpanMarker<'ctx, 'res, 'hir> { + lctx: &'ctx LoweringContext<'res, 'hir>, + } + impl MutVisitor for SpanMarker<'_, '_, '_> { + fn visit_span(&mut self, span: &mut Span) { + *span = self.lctx.lower_span(*span); + } + } + SpanMarker { lctx: self }.visit_attribute(&mut attr); + + attr } fn alias_attrs(&mut self, id: hir::HirId, target_id: hir::HirId) { diff --git a/compiler/rustc_ast_passes/src/node_count.rs b/compiler/rustc_ast_passes/src/node_count.rs index fa42f87786de9..c735b0172a1c3 100644 --- a/compiler/rustc_ast_passes/src/node_count.rs +++ b/compiler/rustc_ast_passes/src/node_count.rs @@ -111,9 +111,9 @@ impl<'ast> Visitor<'ast> for NodeCounter { self.count += 1; walk_path(self, path) } - fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool) { + fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool, item_span: Span) { self.count += 1; - walk_use_tree(self, use_tree, id) + walk_use_tree(self, use_tree, id, item_span) } fn visit_generic_args(&mut self, generic_args: &GenericArgs) { self.count += 1; diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 7dbbd4c34ea7d..caba01c070f9e 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -165,7 +165,13 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> { visit::walk_item(self, item); } - fn visit_use_tree(&mut self, use_tree: &'a ast::UseTree, id: ast::NodeId, nested: bool) { + fn visit_use_tree( + &mut self, + use_tree: &'a ast::UseTree, + id: ast::NodeId, + nested: bool, + item_span: Span, + ) { // Use the base UseTree's NodeId as the item id // This allows the grouping of all the lints in the same item if !nested { @@ -186,7 +192,7 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> { self.check_import(id); } - visit::walk_use_tree(self, use_tree, id); + visit::walk_use_tree(self, use_tree, id, item_span); } } diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 356d7f365fe71..ab2eb0f0f96af 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -156,9 +156,15 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { visit::walk_fn(self, fn_kind); } - fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) { - self.create_def(id, DefPathData::Use, use_tree.span); - visit::walk_use_tree(self, use_tree, id); + fn visit_use_tree( + &mut self, + use_tree: &'a UseTree, + id: NodeId, + _nested: bool, + item_span: Span, + ) { + self.create_def(id, DefPathData::Use, item_span); + visit::walk_use_tree(self, use_tree, id, item_span); } fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {