summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorTom Jakubowski <tom@crystae.net>2014-12-12 04:13:36 -0800
committerTom Jakubowski <tom@crystae.net>2014-12-12 05:37:08 -0800
commit25223c8ef8cf386880cdbdbdeeab643c2f8517e5 (patch)
treeef443652c0fb13b281f787576eeed65494cebd5d /src
parentd2e2bd1b442948d4754bb1eb09ff1914a83604dd (diff)
downloadrust-25223c8ef8cf386880cdbdbdeeab643c2f8517e5.tar.gz
rust-25223c8ef8cf386880cdbdbdeeab643c2f8517e5.zip
rustdoc: Properly inline const items
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 #19773).

Partially address #18156, #19722, #19185

Fix #15821
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/clean/inline.rs22
-rw-r--r--src/librustdoc/clean/mod.rs3
-rw-r--r--src/librustdoc/html/item_type.rs1
3 files changed, 25 insertions, 1 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs
index 3ee07df6ed4..5926c62dd02 100644
--- a/src/librustdoc/clean/inline.rs
+++ b/src/librustdoc/clean/inline.rs
@@ -104,6 +104,10 @@ fn try_inline_def(cx: &DocContext, tcx: &ty::ctxt,
             record_extern_fqn(cx, did, clean::TypeStatic);
             clean::StaticItem(build_static(cx, tcx, did, mtbl))
         }
+        def::DefConst(did) => {
+            record_extern_fqn(cx, did, clean::TypeConst);
+            clean::ConstantItem(build_const(cx, tcx, did))
+        }
         _ => return None,
     };
     let fqn = csearch::get_item_path(tcx, did);
@@ -387,6 +391,24 @@ fn build_module(cx: &DocContext, tcx: &ty::ctxt,
     }
 }
 
+fn build_const(cx: &DocContext, tcx: &ty::ctxt,
+               did: ast::DefId) -> clean::Constant {
+    use rustc::middle::const_eval;
+    use syntax::print::pprust;
+
+    let expr = const_eval::lookup_const_by_id(tcx, did).unwrap_or_else(|| {
+        panic!("expected lookup_const_by_id to succeed for {}", did);
+    });
+    debug!("converting constant expr {} to snippet", expr);
+    let sn = pprust::expr_to_string(expr);
+    debug!("got snippet {}", sn);
+
+    clean::Constant {
+        type_: ty::lookup_item_type(tcx, did).ty.clean(cx),
+        expr: sn
+    }
+}
+
 fn build_static(cx: &DocContext, tcx: &ty::ctxt,
                 did: ast::DefId,
                 mutable: bool) -> clean::Static {
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index df7b922bd1a..7b358b0ce9a 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -1185,6 +1185,7 @@ pub enum TypeKind {
     TypeEnum,
     TypeFunction,
     TypeModule,
+    TypeConst,
     TypeStatic,
     TypeStruct,
     TypeTrait,
@@ -1818,7 +1819,7 @@ impl Clean<Item> for doctree::Static {
     }
 }
 
-#[deriving(Clone, Encodable, Decodable)]
+#[deriving(Clone, Encodable, Decodable, Show)]
 pub struct Constant {
     pub type_: Type,
     pub expr: String,
diff --git a/src/librustdoc/html/item_type.rs b/src/librustdoc/html/item_type.rs
index 86787e5c805..580b7fbe1a3 100644
--- a/src/librustdoc/html/item_type.rs
+++ b/src/librustdoc/html/item_type.rs
@@ -76,6 +76,7 @@ impl ItemType {
             clean::TypeTrait    => ItemType::Trait,
             clean::TypeModule   => ItemType::Module,
             clean::TypeStatic   => ItemType::Static,
+            clean::TypeConst    => ItemType::Constant,
             clean::TypeVariant  => ItemType::Variant,
             clean::TypeTypedef  => ItemType::Typedef,
         }