Skip to content

Commit c091d07

Browse files
authored
Rollup merge of rust-lang#49512 - GuillaumeGomez:intra-links-fields, r=QuietMisdreavus
Add support for variant and types fields for intra links Part of rust-lang#43466. r? @QuietMisdreavus
2 parents 6c9b016 + d0eeb29 commit c091d07

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
@@ -1074,8 +1074,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
10741074
let ty = cx.resolver.borrow_mut()
10751075
.with_scope(*id,
10761076
|resolver| {
1077-
resolver.resolve_str_path_error(DUMMY_SP,
1078-
&path, false)
1077+
resolver.resolve_str_path_error(DUMMY_SP, &path, false)
10791078
})?;
10801079
match ty.def {
10811080
Def::Struct(did) | Def::Union(did) | Def::Enum(did) | Def::TyAlias(did) => {
@@ -1090,7 +1089,27 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
10901089
};
10911090
Ok((ty.def, Some(format!("{}.{}", out, item_name))))
10921091
} else {
1093-
Err(())
1092+
let is_enum = match ty.def {
1093+
Def::Enum(_) => true,
1094+
_ => false,
1095+
};
1096+
let elem = if is_enum {
1097+
cx.tcx.adt_def(did).all_fields().find(|item| item.name == item_name)
1098+
} else {
1099+
cx.tcx.adt_def(did)
1100+
.non_enum_variant()
1101+
.fields
1102+
.iter()
1103+
.find(|item| item.name == item_name)
1104+
};
1105+
if let Some(item) = elem {
1106+
Ok((ty.def,
1107+
Some(format!("{}.{}",
1108+
if is_enum { "variant" } else { "structfield" },
1109+
item.name))))
1110+
} else {
1111+
Err(())
1112+
}
10941113
}
10951114
}
10961115
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)