Skip to content

Commit 130eaae

Browse files
committed
Auto merge of #46000 - kennytm:fix-45640-extern-type-ice-in-rustdoc, r=QuietMisdreavus
Support `extern type` in rustdoc. Fixes #45640. (cc #43467)
2 parents 859c716 + 2792b56 commit 130eaae

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

src/librustdoc/clean/inline.rs

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ pub fn try_inline(cx: &DocContext, def: Def, name: ast::Name)
7777
ret.extend(build_impls(cx, did));
7878
clean::EnumItem(build_enum(cx, did))
7979
}
80+
Def::TyForeign(did) => {
81+
record_extern_fqn(cx, did, clean::TypeKind::Foreign);
82+
ret.extend(build_impls(cx, did));
83+
clean::ForeignTypeItem
84+
}
8085
// Never inline enum variants but leave them shown as reexports.
8186
Def::Variant(..) => return None,
8287
// Assume that enum variants and struct types are reexported next to

src/librustdoc/html/render.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ impl DocFolder for Cache {
12571257
clean::FunctionItem(..) | clean::ModuleItem(..) |
12581258
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) |
12591259
clean::ConstantItem(..) | clean::StaticItem(..) |
1260-
clean::UnionItem(..)
1260+
clean::UnionItem(..) | clean::ForeignTypeItem
12611261
if !self.stripped_mod => {
12621262
// Reexported items mean that the same id can show up twice
12631263
// in the rustdoc ast that we're looking at. We know,
@@ -1292,7 +1292,7 @@ impl DocFolder for Cache {
12921292
// Maintain the parent stack
12931293
let orig_parent_is_trait_impl = self.parent_is_trait_impl;
12941294
let parent_pushed = match item.inner {
1295-
clean::TraitItem(..) | clean::EnumItem(..) |
1295+
clean::TraitItem(..) | clean::EnumItem(..) | clean::ForeignTypeItem |
12961296
clean::StructItem(..) | clean::UnionItem(..) => {
12971297
self.parent_stack.push(item.def_id);
12981298
self.parent_is_trait_impl = false;
@@ -1711,6 +1711,7 @@ impl<'a> fmt::Display for Item<'a> {
17111711
clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?,
17121712
clean::StaticItem(..) | clean::ForeignStaticItem(..) => write!(fmt, "Static ")?,
17131713
clean::ConstantItem(..) => write!(fmt, "Constant ")?,
1714+
clean::ForeignTypeItem => write!(fmt, "Foreign Type ")?,
17141715
_ => {
17151716
// We don't generate pages for any other type.
17161717
unreachable!();
@@ -1775,6 +1776,7 @@ impl<'a> fmt::Display for Item<'a> {
17751776
clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) =>
17761777
item_static(fmt, self.cx, self.item, i),
17771778
clean::ConstantItem(ref c) => item_constant(fmt, self.cx, self.item, c),
1779+
clean::ForeignTypeItem => item_foreign_type(fmt, self.cx, self.item),
17781780
_ => {
17791781
// We don't generate pages for any other type.
17801782
unreachable!();
@@ -3429,6 +3431,21 @@ fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
34293431
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
34303432
}
34313433

3434+
fn item_foreign_type(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item) -> fmt::Result {
3435+
writeln!(w, "<pre class='rust foreigntype'>extern {{")?;
3436+
render_attributes(w, it)?;
3437+
write!(
3438+
w,
3439+
" {}type {};\n}}</pre>",
3440+
VisSpace(&it.visibility),
3441+
it.name.as_ref().unwrap(),
3442+
)?;
3443+
3444+
document(w, cx, it)?;
3445+
3446+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
3447+
}
3448+
34323449
impl<'a> fmt::Display for Sidebar<'a> {
34333450
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
34343451
let cx = self.cx;
@@ -3446,6 +3463,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
34463463
clean::UnionItem(..) => write!(fmt, "Union ")?,
34473464
clean::EnumItem(..) => write!(fmt, "Enum ")?,
34483465
clean::TypedefItem(..) => write!(fmt, "Type Definition ")?,
3466+
clean::ForeignTypeItem => write!(fmt, "Foreign Type ")?,
34493467
clean::ModuleItem(..) => if it.is_crate() {
34503468
write!(fmt, "Crate ")?;
34513469
} else {
@@ -3474,6 +3492,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
34743492
clean::EnumItem(ref e) => sidebar_enum(fmt, it, e)?,
34753493
clean::TypedefItem(ref t, _) => sidebar_typedef(fmt, it, t)?,
34763494
clean::ModuleItem(ref m) => sidebar_module(fmt, it, &m.items)?,
3495+
clean::ForeignTypeItem => sidebar_foreign_type(fmt, it)?,
34773496
_ => (),
34783497
}
34793498
}
@@ -3897,6 +3916,14 @@ fn sidebar_module(fmt: &mut fmt::Formatter, _it: &clean::Item,
38973916
Ok(())
38983917
}
38993918

3919+
fn sidebar_foreign_type(fmt: &mut fmt::Formatter, it: &clean::Item) -> fmt::Result {
3920+
let sidebar = sidebar_assoc_items(it);
3921+
if !sidebar.is_empty() {
3922+
write!(fmt, "<div class=\"block items\">{}</div>", sidebar)?;
3923+
}
3924+
Ok(())
3925+
}
3926+
39003927
impl<'a> fmt::Display for Source<'a> {
39013928
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
39023929
let Source(s) = *self;

src/librustdoc/html/static/main.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"associatedtype",
3838
"constant",
3939
"associatedconstant",
40-
"union"];
40+
"union",
41+
"foreigntype"];
4142

4243
// On the search screen, so you remain on the last tab you opened.
4344
//
@@ -1445,6 +1446,7 @@
14451446
block("trait", "Traits");
14461447
block("fn", "Functions");
14471448
block("type", "Type Definitions");
1449+
block("foreigntype", "Foreign Types");
14481450
}
14491451

14501452
window.initSidebarItems = initSidebarItems;

src/librustdoc/html/static/styles/main.css

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pre {
104104
.content .highlighted.method,
105105
.content .highlighted.tymethod { background-color: #c6afb3; }
106106
.content .highlighted.type { background-color: #ffc891; }
107+
.content .highlighted.foreigntype { background-color: #f5c4ff; }
107108
.content .highlighted.macro { background-color: #8ce488; }
108109
.content .highlighted.constant,
109110
.content .highlighted.static { background-color: #c3e0ff; }
@@ -112,6 +113,7 @@ pre {
112113
.content span.enum, .content a.enum, .block a.current.enum { color: #508157; }
113114
.content span.struct, .content a.struct, .block a.current.struct { color: #df3600; }
114115
.content span.type, .content a.type, .block a.current.type { color: #ba5d00; }
116+
.content span.foreigntype, .content a.foreigntype, .block a.current.foreigntype { color: #cd00e2; }
115117
.content span.macro, .content a.macro, .block a.current.macro { color: #068000; }
116118
.content span.union, .content a.union, .block a.current.union { color: #767b27; }
117119
.content span.constant, .content a.constant, .block a.current.constant,

src/test/rustdoc/foreigntype.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 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+
#![feature(extern_types)]
12+
13+
extern {
14+
// @has foreigntype/foreigntype.ExtType.html
15+
pub type ExtType;
16+
}
17+
18+
impl ExtType {
19+
// @has - '//a[@class="fnname"]' 'do_something'
20+
pub fn do_something(&self) {}
21+
}
22+
23+
pub trait Trait {}
24+
25+
// @has foreigntype/trait.Trait.html '//a[@class="foreigntype"]' 'ExtType'
26+
impl Trait for ExtType {}
27+
28+
// @has foreigntype/index.html '//a[@class="foreigntype"]' 'ExtType'

0 commit comments

Comments
 (0)