Skip to content

Commit 836a136

Browse files
committed
Merge pull request #19774 from tomjakubowski/rustdoc-consts-statics
rustdoc: Properly inline const items Reviewed-by: alexcrichton
2 parents a06508a + 25223c8 commit 836a136

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);
@@ -388,6 +392,24 @@ fn build_module(cx: &DocContext, tcx: &ty::ctxt,
388392
}
389393
}
390394

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

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,7 @@ pub enum TypeKind {
12011201
TypeEnum,
12021202
TypeFunction,
12031203
TypeModule,
1204+
TypeConst,
12041205
TypeStatic,
12051206
TypeStruct,
12061207
TypeTrait,
@@ -1841,7 +1842,7 @@ impl Clean<Item> for doctree::Static {
18411842
}
18421843
}
18431844

1844-
#[deriving(Clone, Encodable, Decodable)]
1845+
#[deriving(Clone, Encodable, Decodable, Show)]
18451846
pub struct Constant {
18461847
pub type_: Type,
18471848
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)