Skip to main content

rustc_ast_passes/
show_spans.rs

1use std::mem;
2
3use rustc_ast::visit::{self, Visitor};
4use rustc_ast::{self as ast, ItemKind};
5use rustc_attr_ir::{Attribute, AttributeKind, RustcShowSpansKind};
6use rustc_attr_parsing::AttributeParser;
7use rustc_session::Session;
8use rustc_span::sym;
9
10use crate::diagnostics;
11
12struct ShowSpans<'a> {
13    sess: &'a Session,
14    kinds: ShowSpanKinds,
15}
16
17#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for ShowSpanKinds {
}
#[automatically_derived]impl ::core::clone::Clone for ShowSpanKinds {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::marker::Copy for ShowSpanKinds { }Copy, #[automatically_derived]impl ::core::default::Default for ShowSpanKinds {
    #[inline]
    fn default() -> Self {
        ShowSpanKinds {
            expr: ::core::default::Default::default(),
            ty: ::core::default::Default::default(),
            path: ::core::default::Default::default(),
            generic_args: ::core::default::Default::default(),
        }
    }
}Default)]
18struct ShowSpanKinds {
19    expr: bool,
20    ty: bool,
21    path: bool,
22    generic_args: bool,
23}
24
25impl<'a> Visitor<'a> for ShowSpans<'a> {
26    fn visit_item(&mut self, item: &'a ast::Item) {
27        let saved = mem::take(&mut self.kinds);
28
29        if let ItemKind::Fn(_) = &item.kind
30            && let Some(Attribute::Parsed(AttributeKind::RustcShowSpans(kinds))) =
31                AttributeParser::parse_limited_sym(self.sess, &item.attrs, &[sym::rustc_show_spans])
32        {
33            for kind in kinds {
34                match kind {
35                    RustcShowSpansKind::Expr => self.kinds.expr = true,
36                    RustcShowSpansKind::Ty => self.kinds.ty = true,
37                    RustcShowSpansKind::Path => self.kinds.path = true,
38                    RustcShowSpansKind::GenericArgs => self.kinds.generic_args = true,
39                }
40            }
41        }
42        visit::walk_item(self, item);
43
44        // Recover the state. Needed if we are looking into nested items.
45        self.kinds = saved;
46    }
47
48    // We override it as to not report attribute paths as paths.
49    fn visit_attribute(&mut self, _: &'a ast::Attribute) {}
50
51    fn visit_expr(&mut self, e: &'a ast::Expr) {
52        if self.kinds.expr {
53            self.sess.dcx().emit_note(diagnostics::ShowSpan { span: e.span, kind: "expr" });
54        }
55        visit::walk_expr(self, e);
56    }
57
58    fn visit_ty(&mut self, t: &'a ast::Ty) {
59        if self.kinds.ty {
60            self.sess.dcx().emit_note(diagnostics::ShowSpan { span: t.span, kind: "ty" });
61        }
62        visit::walk_ty(self, t);
63    }
64
65    fn visit_path(&mut self, p: &'a ast::Path) {
66        if self.kinds.path {
67            self.sess.dcx().emit_note(diagnostics::ShowSpan { span: p.span, kind: "path" });
68        }
69        visit::walk_path(self, p);
70    }
71
72    fn visit_generic_args(&mut self, g: &'a ast::GenericArgs) {
73        if self.kinds.generic_args {
74            self.sess
75                .dcx()
76                .emit_note(diagnostics::ShowSpan { span: g.span(), kind: "generic_args" });
77        }
78        visit::walk_generic_args(self, g);
79    }
80}
81
82pub fn check_crate(sess: &Session, krate: &ast::Crate) {
83    visit::walk_crate(&mut ShowSpans { sess, kinds: ShowSpanKinds::default() }, krate);
84}