Skip to content

Commit eb2ce7f

Browse files
committed
rollup merge of rust-lang#19774: tomjakubowski/rustdoc-consts-statics
Build `clean::ConstantItem` values in the `inline` module and pretty-print the AST for inlined const items. Doc strings are still missing from inlined constants (see rust-lang#19773). Partially address rust-lang#18156, rust-lang#19722, rust-lang#19185 Fix rust-lang#15821 r? @alexcrichton
2 parents e42b36f + 25223c8 commit eb2ce7f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/librustdoc/clean/inline.rs

+22
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ fn try_inline_def(cx: &DocContext, tcx: &ty::ctxt,
104104
record_extern_fqn(cx, did, clean::TypeStatic);
105105
clean::StaticItem(build_static(cx, tcx, did, mtbl))
106106
}
107+
def::DefConst(did) => {
108+
record_extern_fqn(cx, did, clean::TypeConst);
109+
clean::ConstantItem(build_const(cx, tcx, did))
110+
}
107111
_ => return None,
108112
};
109113
let fqn = csearch::get_item_path(tcx, did);
@@ -387,6 +391,24 @@ fn build_module(cx: &DocContext, tcx: &ty::ctxt,
387391
}
388392
}
389393

394+
fn build_const(cx: &DocContext, tcx: &ty::ctxt,
395+
did: ast::DefId) -> clean::Constant {
396+
use rustc::middle::const_eval;
397+
use syntax::print::pprust;
398+
399+
let expr = const_eval::lookup_const_by_id(tcx, did).unwrap_or_else(|| {
400+
panic!("expected lookup_const_by_id to succeed for {}", did);
401+
});
402+
debug!("converting constant expr {} to snippet", expr);
403+
let sn = pprust::expr_to_string(expr);
404+
debug!("got snippet {}", sn);
405+
406+
clean::Constant {
407+
type_: ty::lookup_item_type(tcx, did).ty.clean(cx),
408+
expr: sn
409+
}
410+
}
411+
390412
fn build_static(cx: &DocContext, tcx: &ty::ctxt,
391413
did: ast::DefId,
392414
mutable: bool) -> clean::Static {

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ pub enum TypeKind {
11851185
TypeEnum,
11861186
TypeFunction,
11871187
TypeModule,
1188+
TypeConst,
11881189
TypeStatic,
11891190
TypeStruct,
11901191
TypeTrait,
@@ -1818,7 +1819,7 @@ impl Clean<Item> for doctree::Static {
18181819
}
18191820
}
18201821

1821-
#[deriving(Clone, Encodable, Decodable)]
1822+
#[deriving(Clone, Encodable, Decodable, Show)]
18221823
pub struct Constant {
18231824
pub type_: Type,
18241825
pub expr: String,

src/librustdoc/html/item_type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl ItemType {
7676
clean::TypeTrait => ItemType::Trait,
7777
clean::TypeModule => ItemType::Module,
7878
clean::TypeStatic => ItemType::Static,
79+
clean::TypeConst => ItemType::Constant,
7980
clean::TypeVariant => ItemType::Variant,
8081
clean::TypeTypedef => ItemType::Typedef,
8182
}

0 commit comments

Comments
 (0)