use rustc_middle::ty::{self, TyCtxt};
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol};
-use rustc_span::Span;
use crate::clean::{
- self, Attributes, AttributesExt, FakeDefId, GetDefId, NestedAttributesExt, ToSource, Type,
+ self, utils, Attributes, AttributesExt, GetDefId, ItemId, NestedAttributesExt, Type,
};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;
crate fn try_inline(
cx: &mut DocContext<'_>,
parent_module: DefId,
+ import_def_id: Option<DefId>,
res: Res,
name: Symbol,
attrs: Option<Attrs<'_>>,
clean::ConstantItem(build_const(cx, did))
}
Res::Def(DefKind::Macro(kind), did) => {
- let mac = build_macro(cx, did, name);
+ let mac = build_macro(cx, did, name, import_def_id);
let type_kind = match kind {
MacroKind::Bang => ItemType::Macro,
let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs_clone);
cx.inlined.insert(did.into());
- ret.push(clean::Item::from_def_id_and_attrs_and_parts(
- did,
- Some(name),
- kind,
- box attrs,
- cx,
- cfg,
- ));
+ let mut item =
+ clean::Item::from_def_id_and_attrs_and_parts(did, Some(name), kind, box attrs, cx, cfg);
+ if let Some(import_def_id) = import_def_id {
+ // The visibility needs to reflect the one from the reexport and not from the "source" DefId.
+ item.visibility = cx.tcx.visibility(import_def_id).clean(cx);
+ }
+ ret.push(item);
Some(ret)
}
}
if let Res::PrimTy(p) = item.res {
// Primitive types can't be inlined so generate an import instead.
+ let prim_ty = clean::PrimitiveType::from(p);
items.push(clean::Item {
name: None,
attrs: box clean::Attributes::default(),
- def_id: FakeDefId::new_fake(did.krate),
+ def_id: ItemId::Primitive(prim_ty, did.krate),
visibility: clean::Public,
kind: box clean::ImportItem(clean::Import::new_simple(
item.ident.name,
global: false,
res: item.res,
segments: vec![clean::PathSegment {
- name: clean::PrimitiveType::from(p).as_sym(),
+ name: prim_ty.as_sym(),
args: clean::GenericArgs::AngleBracketed {
args: Vec::new(),
bindings: Vec::new(),
)),
cfg: None,
});
- } else if let Some(i) = try_inline(cx, did, item.res, item.ident.name, None, visited) {
+ } else if let Some(i) =
+ try_inline(cx, did, None, item.res, item.ident.name, None, visited)
+ {
items.extend(i)
}
}
}
}
-fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
- let imported_from = cx.tcx.crate_name(did.krate);
- match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
- LoadedMacro::MacroDef(def, _) => {
- let matchers: Vec<Span> = if let ast::ItemKind::MacroDef(ref def) = def.kind {
- let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
- tts.chunks(4).map(|arm| arm[0].span()).collect()
+fn build_macro(
+ cx: &mut DocContext<'_>,
+ def_id: DefId,
+ name: Symbol,
+ import_def_id: Option<DefId>,
+) -> clean::ItemKind {
+ let imported_from = cx.tcx.crate_name(def_id.krate);
+ match cx.enter_resolver(|r| r.cstore().load_macro_untracked(def_id, cx.sess())) {
+ LoadedMacro::MacroDef(item_def, _) => {
+ if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
+ clean::MacroItem(clean::Macro {
+ source: utils::display_macro_source(
+ cx,
+ name,
+ def,
+ def_id,
+ cx.tcx.visibility(import_def_id.unwrap_or(def_id)),
+ ),
+ imported_from: Some(imported_from),
+ })
} else {
unreachable!()
- };
-
- let source = format!(
- "macro_rules! {} {{\n{}}}",
- name.clean(cx),
- matchers
- .iter()
- .map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) })
- .collect::<String>()
- );
-
- clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) })
+ }
}
LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro {
kind: ext.macro_kind(),