rustdoc/html/render/
span_map.rs

1use std::path::{Path, PathBuf};
2
3use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
4use rustc_hir::def::{DefKind, Res};
5use rustc_hir::def_id::{DefId, LOCAL_CRATE};
6use rustc_hir::intravisit::{self, Visitor};
7use rustc_hir::{
8    ExprKind, HirId, Item, ItemKind, Mod, Node, Pat, PatExpr, PatExprKind, PatKind, QPath,
9};
10use rustc_middle::hir::nested_filter;
11use rustc_middle::ty::TyCtxt;
12use rustc_span::hygiene::MacroKind;
13use rustc_span::{BytePos, ExpnKind, Span};
14
15use crate::clean::{self, PrimitiveType, rustc_span};
16use crate::html::sources;
17
18/// This enum allows us to store two different kinds of information:
19///
20/// In case the `span` definition comes from the same crate, we can simply get the `span` and use
21/// it as is.
22///
23/// Otherwise, we store the definition `DefId` and will generate a link to the documentation page
24/// instead of the source code directly.
25#[derive(Debug)]
26pub(crate) enum LinkFromSrc {
27    Local(clean::Span),
28    External(DefId),
29    Primitive(PrimitiveType),
30    Doc(DefId),
31}
32
33/// This function will do at most two things:
34///
35/// 1. Generate a `span` correspondence map which links an item `span` to its definition `span`.
36/// 2. Collect the source code files.
37///
38/// It returns the `krate`, the source code files and the `span` correspondence map.
39///
40/// Note about the `span` correspondence map: the keys are actually `(lo, hi)` of `span`s. We don't
41/// need the `span` context later on, only their position, so instead of keeping a whole `Span`, we
42/// only keep the `lo` and `hi`.
43pub(crate) fn collect_spans_and_sources(
44    tcx: TyCtxt<'_>,
45    krate: &clean::Crate,
46    src_root: &Path,
47    include_sources: bool,
48    generate_link_to_definition: bool,
49) -> (FxIndexMap<PathBuf, String>, FxHashMap<Span, LinkFromSrc>) {
50    if include_sources {
51        let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
52
53        if generate_link_to_definition {
54            tcx.hir_walk_toplevel_module(&mut visitor);
55        }
56        let sources = sources::collect_local_sources(tcx, src_root, krate);
57        (sources, visitor.matches)
58    } else {
59        (Default::default(), Default::default())
60    }
61}
62
63struct SpanMapVisitor<'tcx> {
64    pub(crate) tcx: TyCtxt<'tcx>,
65    pub(crate) matches: FxHashMap<Span, LinkFromSrc>,
66}
67
68impl SpanMapVisitor<'_> {
69    /// This function is where we handle `hir::Path` elements and add them into the "span map".
70    fn handle_path(&mut self, path: &rustc_hir::Path<'_>) {
71        match path.res {
72            // FIXME: For now, we handle `DefKind` if it's not a `DefKind::TyParam`.
73            // Would be nice to support them too alongside the other `DefKind`
74            // (such as primitive types!).
75            Res::Def(kind, def_id) if kind != DefKind::TyParam => {
76                let link = if def_id.as_local().is_some() {
77                    LinkFromSrc::Local(rustc_span(def_id, self.tcx))
78                } else {
79                    LinkFromSrc::External(def_id)
80                };
81                // In case the path ends with generics, we remove them from the span.
82                let span = path
83                    .segments
84                    .last()
85                    .map(|last| {
86                        // In `use` statements, the included item is not in the path segments.
87                        // However, it doesn't matter because you can't have generics on `use`
88                        // statements.
89                        if path.span.contains(last.ident.span) {
90                            path.span.with_hi(last.ident.span.hi())
91                        } else {
92                            path.span
93                        }
94                    })
95                    .unwrap_or(path.span);
96                self.matches.insert(span, link);
97            }
98            Res::Local(_) if let Some(span) = self.tcx.hir().res_span(path.res) => {
99                self.matches.insert(path.span, LinkFromSrc::Local(clean::Span::new(span)));
100            }
101            Res::PrimTy(p) => {
102                // FIXME: Doesn't handle "path-like" primitives like arrays or tuples.
103                self.matches.insert(path.span, LinkFromSrc::Primitive(PrimitiveType::from(p)));
104            }
105            Res::Err => {}
106            _ => {}
107        }
108    }
109
110    /// Used to generate links on items' definition to go to their documentation page.
111    pub(crate) fn extract_info_from_hir_id(&mut self, hir_id: HirId) {
112        if let Node::Item(item) = self.tcx.hir_node(hir_id)
113            && let Some(span) = self.tcx.def_ident_span(item.owner_id)
114        {
115            let cspan = clean::Span::new(span);
116            // If the span isn't from the current crate, we ignore it.
117            if cspan.inner().is_dummy() || cspan.cnum(self.tcx.sess) != LOCAL_CRATE {
118                return;
119            }
120            self.matches.insert(span, LinkFromSrc::Doc(item.owner_id.to_def_id()));
121        }
122    }
123
124    /// Adds the macro call into the span map. Returns `true` if the `span` was inside a macro
125    /// expansion, whether or not it was added to the span map.
126    ///
127    /// The idea for the macro support is to check if the current `Span` comes from expansion. If
128    /// so, we loop until we find the macro definition by using `outer_expn_data` in a loop.
129    /// Finally, we get the information about the macro itself (`span` if "local", `DefId`
130    /// otherwise) and store it inside the span map.
131    fn handle_macro(&mut self, span: Span) -> bool {
132        if !span.from_expansion() {
133            return false;
134        }
135        // So if the `span` comes from a macro expansion, we need to get the original
136        // macro's `DefId`.
137        let mut data = span.ctxt().outer_expn_data();
138        let mut call_site = data.call_site;
139        // Macros can expand to code containing macros, which will in turn be expanded, etc.
140        // So the idea here is to "go up" until we're back to code that was generated from
141        // macro expansion so that we can get the `DefId` of the original macro that was at the
142        // origin of this expansion.
143        while call_site.from_expansion() {
144            data = call_site.ctxt().outer_expn_data();
145            call_site = data.call_site;
146        }
147
148        let macro_name = match data.kind {
149            ExpnKind::Macro(MacroKind::Bang, macro_name) => macro_name,
150            // Even though we don't handle this kind of macro, this `data` still comes from
151            // expansion so we return `true` so we don't go any deeper in this code.
152            _ => return true,
153        };
154        let link_from_src = match data.macro_def_id {
155            Some(macro_def_id) => {
156                if macro_def_id.is_local() {
157                    LinkFromSrc::Local(clean::Span::new(data.def_site))
158                } else {
159                    LinkFromSrc::External(macro_def_id)
160                }
161            }
162            None => return true,
163        };
164        let new_span = data.call_site;
165        let macro_name = macro_name.as_str();
166        // The "call_site" includes the whole macro with its "arguments". We only want
167        // the macro name.
168        let new_span = new_span.with_hi(new_span.lo() + BytePos(macro_name.len() as u32));
169        self.matches.insert(new_span, link_from_src);
170        true
171    }
172
173    fn infer_id(&mut self, hir_id: HirId, expr_hir_id: Option<HirId>, span: Span) {
174        let tcx = self.tcx;
175        let body_id = tcx.hir_enclosing_body_owner(hir_id);
176        // FIXME: this is showing error messages for parts of the code that are not
177        // compiled (because of cfg)!
178        //
179        // See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
180        let typeck_results = tcx.typeck_body(tcx.hir_body_owned_by(body_id).id());
181        // Interestingly enough, for method calls, we need the whole expression whereas for static
182        // method/function calls, we need the call expression specifically.
183        if let Some(def_id) = typeck_results.type_dependent_def_id(expr_hir_id.unwrap_or(hir_id)) {
184            let link = if def_id.as_local().is_some() {
185                LinkFromSrc::Local(rustc_span(def_id, tcx))
186            } else {
187                LinkFromSrc::External(def_id)
188            };
189            self.matches.insert(span, link);
190        }
191    }
192
193    fn handle_pat(&mut self, p: &Pat<'_>) {
194        let mut check_qpath = |qpath, hir_id| match qpath {
195            QPath::TypeRelative(_, path) if matches!(path.res, Res::Err) => {
196                self.infer_id(path.hir_id, Some(hir_id), qpath.span());
197            }
198            QPath::Resolved(_, path) => self.handle_path(path),
199            _ => {}
200        };
201        match p.kind {
202            PatKind::Binding(_, _, _, Some(p)) => self.handle_pat(p),
203            PatKind::Struct(qpath, _, _) | PatKind::TupleStruct(qpath, _, _) => {
204                check_qpath(qpath, p.hir_id)
205            }
206            PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, .. }) => {
207                check_qpath(*qpath, *hir_id)
208            }
209            PatKind::Or(pats) => {
210                for pat in pats {
211                    self.handle_pat(pat);
212                }
213            }
214            _ => {}
215        }
216    }
217}
218
219impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
220    type NestedFilter = nested_filter::All;
221
222    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
223        self.tcx
224    }
225
226    fn visit_path(&mut self, path: &rustc_hir::Path<'tcx>, _id: HirId) {
227        if self.handle_macro(path.span) {
228            return;
229        }
230        self.handle_path(path);
231        intravisit::walk_path(self, path);
232    }
233
234    fn visit_pat(&mut self, p: &Pat<'tcx>) {
235        self.handle_pat(p);
236    }
237
238    fn visit_mod(&mut self, m: &'tcx Mod<'tcx>, span: Span, id: HirId) {
239        // To make the difference between "mod foo {}" and "mod foo;". In case we "import" another
240        // file, we want to link to it. Otherwise no need to create a link.
241        if !span.overlaps(m.spans.inner_span) {
242            // Now that we confirmed it's a file import, we want to get the span for the module
243            // name only and not all the "mod foo;".
244            if let Node::Item(item) = self.tcx.hir_node(id) {
245                let (ident, _) = item.expect_mod();
246                self.matches
247                    .insert(ident.span, LinkFromSrc::Local(clean::Span::new(m.spans.inner_span)));
248            }
249        } else {
250            // If it's a "mod foo {}", we want to look to its documentation page.
251            self.extract_info_from_hir_id(id);
252        }
253        intravisit::walk_mod(self, m);
254    }
255
256    fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
257        match expr.kind {
258            ExprKind::MethodCall(segment, ..) => {
259                self.infer_id(segment.hir_id, Some(expr.hir_id), segment.ident.span)
260            }
261            ExprKind::Call(call, ..) => self.infer_id(call.hir_id, None, call.span),
262            _ => {
263                if self.handle_macro(expr.span) {
264                    // We don't want to go deeper into the macro.
265                    return;
266                }
267            }
268        }
269        intravisit::walk_expr(self, expr);
270    }
271
272    fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
273        match item.kind {
274            ItemKind::Static(..)
275            | ItemKind::Const(..)
276            | ItemKind::Fn { .. }
277            | ItemKind::Macro(..)
278            | ItemKind::TyAlias(..)
279            | ItemKind::Enum(..)
280            | ItemKind::Struct(..)
281            | ItemKind::Union(..)
282            | ItemKind::Trait(..)
283            | ItemKind::TraitAlias(..) => self.extract_info_from_hir_id(item.hir_id()),
284            ItemKind::Impl(_)
285            | ItemKind::Use(..)
286            | ItemKind::ExternCrate(..)
287            | ItemKind::ForeignMod { .. }
288            | ItemKind::GlobalAsm { .. }
289            // We already have "visit_mod" above so no need to check it here.
290            | ItemKind::Mod(..) => {}
291        }
292        intravisit::walk_item(self, item);
293    }
294}