Skip to content

Commit 1a9d20b

Browse files
authored
Rollup merge of #82004 - GuillaumeGomez:clean-static-struct, r=jyn514
clean up clean::Static struct Having a `String` for the expression didn't make much sense, and even less when it's actually not used (except in json so I kept it). r? ``@jyn514``
2 parents 55539cb + 583563d commit 1a9d20b

File tree

5 files changed

+63
-71
lines changed

5 files changed

+63
-71
lines changed

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ fn build_static(cx: &DocContext<'_>, did: DefId, mutable: bool) -> clean::Static
510510
clean::Static {
511511
type_: cx.tcx.type_of(did).clean(cx),
512512
mutability: if mutable { Mutability::Mut } else { Mutability::Not },
513-
expr: "\n\n\n".to_string(), // trigger the "[definition]" links
513+
expr: None,
514514
}
515515
}
516516

src/librustdoc/clean/mod.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl Clean<Constant> for hir::ConstArg {
408408
.tcx
409409
.type_of(cx.tcx.hir().body_owner_def_id(self.value.body).to_def_id())
410410
.clean(cx),
411-
expr: print_const_expr(cx, self.value.body),
411+
expr: print_const_expr(cx.tcx, self.value.body),
412412
value: None,
413413
is_literal: is_literal_expr(cx, self.value.body.hir_id),
414414
}
@@ -1052,7 +1052,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
10521052
cx.with_param_env(local_did, || {
10531053
let inner = match self.kind {
10541054
hir::TraitItemKind::Const(ref ty, default) => {
1055-
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
1055+
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx.tcx, e)))
10561056
}
10571057
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
10581058
let mut m = (sig, &self.generics, body).clean(cx);
@@ -1093,7 +1093,7 @@ impl Clean<Item> for hir::ImplItem<'_> {
10931093
cx.with_param_env(local_did, || {
10941094
let inner = match self.kind {
10951095
hir::ImplItemKind::Const(ref ty, expr) => {
1096-
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
1096+
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx.tcx, expr)))
10971097
}
10981098
hir::ImplItemKind::Fn(ref sig, body) => {
10991099
let mut m = (sig, &self.generics, body).clean(cx);
@@ -1954,14 +1954,12 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
19541954
let mut name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id));
19551955
cx.with_param_env(def_id, || {
19561956
let kind = match item.kind {
1957-
ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
1958-
type_: ty.clean(cx),
1959-
mutability,
1960-
expr: print_const_expr(cx, body_id),
1961-
}),
1957+
ItemKind::Static(ty, mutability, body_id) => {
1958+
StaticItem(Static { type_: ty.clean(cx), mutability, expr: Some(body_id) })
1959+
}
19621960
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
19631961
type_: ty.clean(cx),
1964-
expr: print_const_expr(cx, body_id),
1962+
expr: print_const_expr(cx.tcx, body_id),
19651963
value: print_evaluated_const(cx, def_id),
19661964
is_literal: is_literal_expr(cx, body_id.hir_id),
19671965
}),
@@ -2263,11 +2261,9 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
22632261
},
22642262
})
22652263
}
2266-
hir::ForeignItemKind::Static(ref ty, mutability) => ForeignStaticItem(Static {
2267-
type_: ty.clean(cx),
2268-
mutability,
2269-
expr: String::new(),
2270-
}),
2264+
hir::ForeignItemKind::Static(ref ty, mutability) => {
2265+
ForeignStaticItem(Static { type_: ty.clean(cx), mutability, expr: None })
2266+
}
22712267
hir::ForeignItemKind::Type => ForeignTypeItem,
22722268
};
22732269

src/librustdoc/clean/types.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hir as hir;
1919
use rustc_hir::def::{CtorKind, Res};
2020
use rustc_hir::def_id::{CrateNum, DefId, DefIndex};
2121
use rustc_hir::lang_items::LangItem;
22-
use rustc_hir::Mutability;
22+
use rustc_hir::{BodyId, Mutability};
2323
use rustc_index::vec::IndexVec;
2424
use rustc_middle::ty::{self, TyCtxt};
2525
use rustc_session::Session;
@@ -1955,10 +1955,7 @@ crate struct BareFunctionDecl {
19551955
crate struct Static {
19561956
crate type_: Type,
19571957
crate mutability: Mutability,
1958-
/// It's useful to have the value of a static documented, but I have no
1959-
/// desire to represent expressions (that'd basically be all of the AST,
1960-
/// which is huge!). So, have a string.
1961-
crate expr: String,
1958+
crate expr: Option<BodyId>,
19621959
}
19631960

19641961
#[derive(Clone, PartialEq, Eq, Hash, Debug)]

src/librustdoc/clean/utils.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ crate fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String {
304304
ty::ConstKind::Unevaluated(def, _, promoted) => {
305305
let mut s = if let Some(def) = def.as_local() {
306306
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def.did);
307-
print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id))
307+
print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(hir_id))
308308
} else {
309309
inline::print_inlined_const(cx, def.did)
310310
};
@@ -393,16 +393,17 @@ crate fn is_literal_expr(cx: &DocContext<'_>, hir_id: hir::HirId) -> bool {
393393
false
394394
}
395395

396-
crate fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
397-
let value = &cx.tcx.hir().body(body).value;
396+
crate fn print_const_expr(tcx: TyCtxt<'_>, body: hir::BodyId) -> String {
397+
let hir = tcx.hir();
398+
let value = &hir.body(body).value;
398399

399400
let snippet = if !value.span.from_expansion() {
400-
cx.sess().source_map().span_to_snippet(value.span).ok()
401+
tcx.sess.source_map().span_to_snippet(value.span).ok()
401402
} else {
402403
None
403404
};
404405

405-
snippet.unwrap_or_else(|| rustc_hir_pretty::id_to_string(&cx.tcx.hir(), body.hir_id))
406+
snippet.unwrap_or_else(|| rustc_hir_pretty::id_to_string(&hir, body.hir_id))
406407
}
407408

408409
/// Given a type Path, resolve it to a Type using the TyCtxt

src/librustdoc/json/conversions.rs

+44-46
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ use std::convert::From;
66

77
use rustc_ast::ast;
88
use rustc_hir::def::CtorKind;
9+
use rustc_middle::ty::TyCtxt;
910
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
1011
use rustc_span::Pos;
1112

1213
use rustdoc_json_types::*;
1314

1415
use crate::clean;
16+
use crate::clean::utils::print_const_expr;
1517
use crate::formats::item_type::ItemType;
1618
use crate::json::JsonRenderer;
1719

@@ -43,7 +45,7 @@ impl JsonRenderer<'_> {
4345
.collect(),
4446
deprecation: deprecation.map(from_deprecation),
4547
kind: item_type.into(),
46-
inner: kind.into(),
48+
inner: from_clean_item_kind(kind, self.tcx),
4749
}),
4850
}
4951
}
@@ -144,44 +146,42 @@ crate fn from_def_id(did: DefId) -> Id {
144146
Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
145147
}
146148

147-
impl From<clean::ItemKind> for ItemEnum {
148-
fn from(item: clean::ItemKind) -> Self {
149-
use clean::ItemKind::*;
150-
match item {
151-
ModuleItem(m) => ItemEnum::ModuleItem(m.into()),
152-
ExternCrateItem(c, a) => {
153-
ItemEnum::ExternCrateItem { name: c.to_string(), rename: a.map(|x| x.to_string()) }
154-
}
155-
ImportItem(i) => ItemEnum::ImportItem(i.into()),
156-
StructItem(s) => ItemEnum::StructItem(s.into()),
157-
UnionItem(u) => ItemEnum::UnionItem(u.into()),
158-
StructFieldItem(f) => ItemEnum::StructFieldItem(f.into()),
159-
EnumItem(e) => ItemEnum::EnumItem(e.into()),
160-
VariantItem(v) => ItemEnum::VariantItem(v.into()),
161-
FunctionItem(f) => ItemEnum::FunctionItem(f.into()),
162-
ForeignFunctionItem(f) => ItemEnum::FunctionItem(f.into()),
163-
TraitItem(t) => ItemEnum::TraitItem(t.into()),
164-
TraitAliasItem(t) => ItemEnum::TraitAliasItem(t.into()),
165-
MethodItem(m, _) => ItemEnum::MethodItem(from_function_method(m, true)),
166-
TyMethodItem(m) => ItemEnum::MethodItem(from_function_method(m, false)),
167-
ImplItem(i) => ItemEnum::ImplItem(i.into()),
168-
StaticItem(s) => ItemEnum::StaticItem(s.into()),
169-
ForeignStaticItem(s) => ItemEnum::StaticItem(s.into()),
170-
ForeignTypeItem => ItemEnum::ForeignTypeItem,
171-
TypedefItem(t, _) => ItemEnum::TypedefItem(t.into()),
172-
OpaqueTyItem(t) => ItemEnum::OpaqueTyItem(t.into()),
173-
ConstantItem(c) => ItemEnum::ConstantItem(c.into()),
174-
MacroItem(m) => ItemEnum::MacroItem(m.source),
175-
ProcMacroItem(m) => ItemEnum::ProcMacroItem(m.into()),
176-
AssocConstItem(t, s) => ItemEnum::AssocConstItem { type_: t.into(), default: s },
177-
AssocTypeItem(g, t) => ItemEnum::AssocTypeItem {
178-
bounds: g.into_iter().map(Into::into).collect(),
179-
default: t.map(Into::into),
180-
},
181-
StrippedItem(inner) => (*inner).into(),
182-
PrimitiveItem(_) | KeywordItem(_) => {
183-
panic!("{:?} is not supported for JSON output", item)
184-
}
149+
fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>) -> ItemEnum {
150+
use clean::ItemKind::*;
151+
match item {
152+
ModuleItem(m) => ItemEnum::ModuleItem(m.into()),
153+
ExternCrateItem(c, a) => {
154+
ItemEnum::ExternCrateItem { name: c.to_string(), rename: a.map(|x| x.to_string()) }
155+
}
156+
ImportItem(i) => ItemEnum::ImportItem(i.into()),
157+
StructItem(s) => ItemEnum::StructItem(s.into()),
158+
UnionItem(u) => ItemEnum::UnionItem(u.into()),
159+
StructFieldItem(f) => ItemEnum::StructFieldItem(f.into()),
160+
EnumItem(e) => ItemEnum::EnumItem(e.into()),
161+
VariantItem(v) => ItemEnum::VariantItem(v.into()),
162+
FunctionItem(f) => ItemEnum::FunctionItem(f.into()),
163+
ForeignFunctionItem(f) => ItemEnum::FunctionItem(f.into()),
164+
TraitItem(t) => ItemEnum::TraitItem(t.into()),
165+
TraitAliasItem(t) => ItemEnum::TraitAliasItem(t.into()),
166+
MethodItem(m, _) => ItemEnum::MethodItem(from_function_method(m, true)),
167+
TyMethodItem(m) => ItemEnum::MethodItem(from_function_method(m, false)),
168+
ImplItem(i) => ItemEnum::ImplItem(i.into()),
169+
StaticItem(s) => ItemEnum::StaticItem(from_clean_static(s, tcx)),
170+
ForeignStaticItem(s) => ItemEnum::StaticItem(from_clean_static(s, tcx)),
171+
ForeignTypeItem => ItemEnum::ForeignTypeItem,
172+
TypedefItem(t, _) => ItemEnum::TypedefItem(t.into()),
173+
OpaqueTyItem(t) => ItemEnum::OpaqueTyItem(t.into()),
174+
ConstantItem(c) => ItemEnum::ConstantItem(c.into()),
175+
MacroItem(m) => ItemEnum::MacroItem(m.source),
176+
ProcMacroItem(m) => ItemEnum::ProcMacroItem(m.into()),
177+
AssocConstItem(t, s) => ItemEnum::AssocConstItem { type_: t.into(), default: s },
178+
AssocTypeItem(g, t) => ItemEnum::AssocTypeItem {
179+
bounds: g.into_iter().map(Into::into).collect(),
180+
default: t.map(Into::into),
181+
},
182+
StrippedItem(inner) => from_clean_item_kind(*inner, tcx).into(),
183+
PrimitiveItem(_) | KeywordItem(_) => {
184+
panic!("{:?} is not supported for JSON output", item)
185185
}
186186
}
187187
}
@@ -535,13 +535,11 @@ impl From<clean::OpaqueTy> for OpaqueTy {
535535
}
536536
}
537537

538-
impl From<clean::Static> for Static {
539-
fn from(stat: clean::Static) -> Self {
540-
Static {
541-
type_: stat.type_.into(),
542-
mutable: stat.mutability == ast::Mutability::Mut,
543-
expr: stat.expr,
544-
}
538+
fn from_clean_static(stat: clean::Static, tcx: TyCtxt<'_>) -> Static {
539+
Static {
540+
type_: stat.type_.into(),
541+
mutable: stat.mutability == ast::Mutability::Mut,
542+
expr: stat.expr.map(|e| print_const_expr(tcx, e)).unwrap_or_default(),
545543
}
546544
}
547545

0 commit comments

Comments
 (0)