Skip to content

Commit d0eeb29

Browse files
Add support for variant and types fields for intra links
1 parent 6960761 commit d0eeb29

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/librustdoc/clean/mod.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
10131013
let ty = cx.resolver.borrow_mut()
10141014
.with_scope(*id,
10151015
|resolver| {
1016-
resolver.resolve_str_path_error(DUMMY_SP,
1017-
&path, false)
1016+
resolver.resolve_str_path_error(DUMMY_SP, &path, false)
10181017
})?;
10191018
match ty.def {
10201019
Def::Struct(did) | Def::Union(did) | Def::Enum(did) | Def::TyAlias(did) => {
@@ -1029,7 +1028,27 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
10291028
};
10301029
Ok((ty.def, Some(format!("{}.{}", out, item_name))))
10311030
} else {
1032-
Err(())
1031+
let is_enum = match ty.def {
1032+
Def::Enum(_) => true,
1033+
_ => false,
1034+
};
1035+
let elem = if is_enum {
1036+
cx.tcx.adt_def(did).all_fields().find(|item| item.name == item_name)
1037+
} else {
1038+
cx.tcx.adt_def(did)
1039+
.non_enum_variant()
1040+
.fields
1041+
.iter()
1042+
.find(|item| item.name == item_name)
1043+
};
1044+
if let Some(item) = elem {
1045+
Ok((ty.def,
1046+
Some(format!("{}.{}",
1047+
if is_enum { "variant" } else { "structfield" },
1048+
item.name))))
1049+
} else {
1050+
Err(())
1051+
}
10331052
}
10341053
}
10351054
Def::Trait(did) => {

src/test/rustdoc/struct-field.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_name = "foo"]
12+
13+
// ignore-tidy-linelength
14+
15+
// @has foo/index.html '//*[@class="docblock"]/p/a[@href="../foo/struct.Foo.html#structfield.bar"]' 'Foo::bar'
16+
// @has foo/index.html '//*[@class="docblock"]/p/a[@href="../foo/union.Bar.html#structfield.foo"]' 'Bar::foo'
17+
// @has foo/index.html '//*[@class="docblock"]/p/a[@href="../foo/enum.Uniooon.html#X.v"]' 'Uniooon::X'
18+
19+
//! Test with [Foo::bar], [Bar::foo], [Uniooon::X]
20+
21+
pub struct Foo {
22+
pub bar: usize,
23+
}
24+
25+
pub union Bar {
26+
pub foo: u32,
27+
}
28+
29+
pub enum Uniooon {
30+
F,
31+
X,
32+
Y,
33+
}

0 commit comments

Comments
 (0)