Skip to content

Commit 5e53c05

Browse files
Add support for trait associated items
1 parent 53826a8 commit 5e53c05

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

src/librustdoc/html/render/span_map.rs

+31-30
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
44
use rustc_hir::def::{DefKind, Res};
55
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
66
use rustc_hir::intravisit::{self, Visitor};
7-
use rustc_hir::{
8-
ExprKind, HirId, Item, ItemKind, Mod, Node, Pat, PatExpr, PatExprKind, PatKind, QPath,
9-
};
7+
use rustc_hir::{ExprKind, HirId, Item, ItemKind, Mod, Node, QPath};
108
use rustc_middle::hir::nested_filter;
119
use rustc_middle::ty::TyCtxt;
1210
use rustc_span::hygiene::MacroKind;
@@ -191,31 +189,6 @@ impl SpanMapVisitor<'_> {
191189
self.matches.insert(span, link);
192190
}
193191
}
194-
195-
fn handle_pat(&mut self, p: &Pat<'_>) {
196-
let mut check_qpath = |qpath, hir_id| match qpath {
197-
QPath::TypeRelative(_, path) if matches!(path.res, Res::Err) => {
198-
self.infer_id(path.hir_id, Some(hir_id), qpath.span());
199-
}
200-
QPath::Resolved(_, path) => self.handle_path(path),
201-
_ => {}
202-
};
203-
match p.kind {
204-
PatKind::Binding(_, _, _, Some(p)) => self.handle_pat(p),
205-
PatKind::Struct(qpath, _, _) | PatKind::TupleStruct(qpath, _, _) => {
206-
check_qpath(qpath, p.hir_id)
207-
}
208-
PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, .. }) => {
209-
check_qpath(*qpath, *hir_id)
210-
}
211-
PatKind::Or(pats) => {
212-
for pat in pats {
213-
self.handle_pat(pat);
214-
}
215-
}
216-
_ => {}
217-
}
218-
}
219192
}
220193

221194
impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
@@ -233,8 +206,36 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
233206
intravisit::walk_path(self, path);
234207
}
235208

236-
fn visit_pat(&mut self, p: &Pat<'tcx>) {
237-
self.handle_pat(p);
209+
fn visit_qpath(&mut self, qpath: &QPath<'tcx>, id: HirId, span: Span) {
210+
match *qpath {
211+
QPath::TypeRelative(qself, path) => {
212+
if matches!(path.res, Res::Err) {
213+
let tcx = self.tcx;
214+
let hir = tcx.hir();
215+
let body_id = hir.enclosing_body_owner(id);
216+
let typeck_results = tcx.typeck_body(hir.body_owned_by(body_id).id());
217+
let path = rustc_hir::Path {
218+
// We change the span to not include parens.
219+
span: span.with_hi(path.ident.span.hi()),
220+
res: typeck_results.qpath_res(qpath, id),
221+
segments: &[],
222+
};
223+
self.handle_path(&path);
224+
} else {
225+
self.infer_id(path.hir_id, Some(id), span);
226+
}
227+
228+
rustc_ast::visit::try_visit!(self.visit_ty(qself));
229+
self.visit_path_segment(path);
230+
}
231+
QPath::Resolved(maybe_qself, path) => {
232+
self.handle_path(path);
233+
234+
rustc_ast::visit::visit_opt!(self, visit_ty, maybe_qself);
235+
self.visit_path(path, id)
236+
}
237+
_ => {}
238+
}
238239
}
239240

240241
fn visit_mod(&mut self, m: &'tcx Mod<'tcx>, span: Span, id: HirId) {
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ compile-flags: -Zunstable-options --generate-link-to-definition
2+
3+
#![crate_name = "foo"]
4+
5+
pub enum Ty {
6+
Var,
7+
}
8+
9+
//@ has 'src/foo/jump-to-def-assoc-items.rs.html'
10+
//@ has - '//a[@href="#6"]' 'Self::Var'
11+
impl Ty {
12+
fn f() {
13+
let _ = Self::Var;
14+
}
15+
}

0 commit comments

Comments
 (0)