Skip to content

metadata: Encode attributes for const items #19793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_bounds_and_type(rbml_w, ecx, &lookup_item_type(tcx, def_id));
encode_name(rbml_w, item.ident.name);
encode_path(rbml_w, path);
encode_attributes(rbml_w, item.attrs.as_slice());
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
encode_visibility(rbml_w, vis);
encode_stability(rbml_w, stab);
Expand Down
22 changes: 22 additions & 0 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ pub enum TypeKind {
TypeEnum,
TypeFunction,
TypeModule,
TypeConst,
TypeStatic,
TypeStruct,
TypeTrait,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down