Skip to content

Commit 04195bb

Browse files
committed
Mark more spans as relative.
1 parent 26089ba commit 04195bb

File tree

6 files changed

+52
-15
lines changed

6 files changed

+52
-15
lines changed

compiler/rustc_ast/src/mut_visit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,10 @@ pub fn visit_attr_args<T: MutVisitor>(args: &mut AttrArgs, vis: &mut T) {
396396
vis.visit_span(eq_span);
397397
vis.visit_expr(expr);
398398
}
399-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
400-
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
399+
AttrArgs::Eq(eq_span, AttrArgsEq::Hir(lit)) => {
400+
vis.visit_span(eq_span);
401+
let MetaItemLit { symbol: _, suffix: _, kind: _, span } = lit;
402+
vis.visit_span(span);
401403
}
402404
}
403405
}

compiler/rustc_ast/src/visit.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,14 @@ pub trait Visitor<'ast>: Sized {
206206
fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
207207
walk_path(self, path)
208208
}
209-
fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
210-
walk_use_tree(self, use_tree, id)
209+
fn visit_use_tree(
210+
&mut self,
211+
use_tree: &'ast UseTree,
212+
id: NodeId,
213+
_nested: bool,
214+
item_span: Span,
215+
) {
216+
walk_use_tree(self, use_tree, id, item_span)
211217
}
212218
fn visit_path_segment(&mut self, path_segment: &'ast PathSegment) {
213219
walk_path_segment(self, path_segment)
@@ -307,7 +313,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
307313
visitor.visit_ident(item.ident);
308314
match &item.kind {
309315
ItemKind::ExternCrate(_) => {}
310-
ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
316+
ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false, item.span),
311317
ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => {
312318
visitor.visit_ty(ty);
313319
walk_list!(visitor, visit_expr, expr);
@@ -450,7 +456,12 @@ pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
450456
}
451457
}
452458

453-
pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
459+
pub fn walk_use_tree<'a, V: Visitor<'a>>(
460+
visitor: &mut V,
461+
use_tree: &'a UseTree,
462+
id: NodeId,
463+
item_span: Span,
464+
) {
454465
visitor.visit_path(&use_tree.prefix, id);
455466
match &use_tree.kind {
456467
UseTreeKind::Simple(rename) => {
@@ -462,7 +473,7 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree,
462473
UseTreeKind::Glob => {}
463474
UseTreeKind::Nested(use_trees) => {
464475
for &(ref nested_tree, nested_id) in use_trees {
465-
visitor.visit_use_tree(nested_tree, nested_id, true);
476+
visitor.visit_use_tree(nested_tree, nested_id, true, item_span);
466477
}
467478
}
468479
}

compiler/rustc_ast_lowering/src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
902902
})),
903903
AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data),
904904
};
905+
let mut attr = Attribute { kind, id: attr.id, style: attr.style, span: attr.span };
905906

906-
Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) }
907+
use rustc_ast::mut_visit::MutVisitor;
908+
struct SpanMarker<'ctx, 'res, 'hir> {
909+
lctx: &'ctx LoweringContext<'res, 'hir>,
910+
}
911+
impl MutVisitor for SpanMarker<'_, '_, '_> {
912+
fn visit_span(&mut self, span: &mut Span) {
913+
*span = self.lctx.lower_span(*span);
914+
}
915+
}
916+
SpanMarker { lctx: self }.visit_attribute(&mut attr);
917+
918+
attr
907919
}
908920

909921
fn alias_attrs(&mut self, id: hir::HirId, target_id: hir::HirId) {

compiler/rustc_ast_passes/src/node_count.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ impl<'ast> Visitor<'ast> for NodeCounter {
111111
self.count += 1;
112112
walk_path(self, path)
113113
}
114-
fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool) {
114+
fn visit_use_tree(&mut self, use_tree: &UseTree, id: NodeId, _nested: bool, item_span: Span) {
115115
self.count += 1;
116-
walk_use_tree(self, use_tree, id)
116+
walk_use_tree(self, use_tree, id, item_span)
117117
}
118118
fn visit_generic_args(&mut self, generic_args: &GenericArgs) {
119119
self.count += 1;

compiler/rustc_resolve/src/check_unused.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
165165
visit::walk_item(self, item);
166166
}
167167

168-
fn visit_use_tree(&mut self, use_tree: &'a ast::UseTree, id: ast::NodeId, nested: bool) {
168+
fn visit_use_tree(
169+
&mut self,
170+
use_tree: &'a ast::UseTree,
171+
id: ast::NodeId,
172+
nested: bool,
173+
item_span: Span,
174+
) {
169175
// Use the base UseTree's NodeId as the item id
170176
// This allows the grouping of all the lints in the same item
171177
if !nested {
@@ -186,7 +192,7 @@ impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
186192
self.check_import(id);
187193
}
188194

189-
visit::walk_use_tree(self, use_tree, id);
195+
visit::walk_use_tree(self, use_tree, id, item_span);
190196
}
191197
}
192198

compiler/rustc_resolve/src/def_collector.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,15 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
156156
visit::walk_fn(self, fn_kind);
157157
}
158158

159-
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
160-
self.create_def(id, DefPathData::Use, use_tree.span);
161-
visit::walk_use_tree(self, use_tree, id);
159+
fn visit_use_tree(
160+
&mut self,
161+
use_tree: &'a UseTree,
162+
id: NodeId,
163+
_nested: bool,
164+
item_span: Span,
165+
) {
166+
self.create_def(id, DefPathData::Use, item_span);
167+
visit::walk_use_tree(self, use_tree, id, item_span);
162168
}
163169

164170
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {

0 commit comments

Comments
 (0)