rustdoc-json: Include safety of statics

This commit is contained in:
Alona Enraght-Moony 2024-12-01 21:39:58 +00:00
parent 51ea7c12e7
commit f33dba0287
4 changed files with 74 additions and 14 deletions

View file

@ -321,8 +321,8 @@ fn from_clean_item(item: clean::Item, renderer: &JsonRenderer<'_>) -> ItemEnum {
MethodItem(m, _) => ItemEnum::Function(from_function(m, true, header.unwrap(), renderer)),
TyMethodItem(m) => ItemEnum::Function(from_function(m, false, header.unwrap(), renderer)),
ImplItem(i) => ItemEnum::Impl((*i).into_json(renderer)),
StaticItem(s) => ItemEnum::Static(s.into_json(renderer)),
ForeignStaticItem(s, _) => ItemEnum::Static(s.into_json(renderer)),
StaticItem(s) => ItemEnum::Static(convert_static(s, rustc_hir::Safety::Safe, renderer)),
ForeignStaticItem(s, safety) => ItemEnum::Static(convert_static(s, safety, renderer)),
ForeignTypeItem => ItemEnum::ExternType,
TypeAliasItem(t) => ItemEnum::TypeAlias(t.into_json(renderer)),
// FIXME(generic_const_items): Add support for generic free consts
@ -831,17 +831,20 @@ impl FromClean<Box<clean::TypeAlias>> for TypeAlias {
}
}
impl FromClean<clean::Static> for Static {
fn from_clean(stat: clean::Static, renderer: &JsonRenderer<'_>) -> Self {
let tcx = renderer.tcx;
Static {
type_: (*stat.type_).into_json(renderer),
is_mutable: stat.mutability == ast::Mutability::Mut,
expr: stat
.expr
.map(|e| rendered_const(tcx, tcx.hir().body(e), tcx.hir().body_owner_def_id(e)))
.unwrap_or_default(),
}
fn convert_static(
stat: clean::Static,
safety: rustc_hir::Safety,
renderer: &JsonRenderer<'_>,
) -> Static {
let tcx = renderer.tcx;
Static {
type_: (*stat.type_).into_json(renderer),
is_mutable: stat.mutability == ast::Mutability::Mut,
is_unsafe: safety == rustc_hir::Safety::Unsafe,
expr: stat
.expr
.map(|e| rendered_const(tcx, tcx.hir().body(e), tcx.hir().body_owner_def_id(e)))
.unwrap_or_default(),
}
}

View file

@ -30,7 +30,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
/// This integer is incremented with every breaking change to the API,
/// and is returned along with the JSON blob as [`Crate::format_version`].
/// Consuming code should assert that this value matches the format version(s) that it supports.
pub const FORMAT_VERSION: u32 = 36;
pub const FORMAT_VERSION: u32 = 37;
/// The root of the emitted JSON blob.
///
@ -1238,6 +1238,22 @@ pub struct Static {
///
/// It's not guaranteed that it'll match the actual source code for the initial value.
pub expr: String,
/// Is the static `unsafe`?
///
/// This is only true if it's in an `extern` block, and not explicity marked
/// as `safe`.
///
/// ```rust
/// unsafe extern {
/// static A: i32; // unsafe
/// safe static B: i32; // safe
/// }
///
/// static C: i32 = 0; // safe
/// static mut D: i32 = 0; // safe
/// ```
pub is_unsafe: bool,
}
/// A primitive type declaration. Declarations of this kind can only come from the core library.

View file

@ -0,0 +1,39 @@
// ignore-tidy-linelength
//@ edition: 2021
extern "C" {
//@ is '$.index[*][?(@.name=="A")].inner.static.is_unsafe' true
//@ is '$.index[*][?(@.name=="A")].inner.static.is_mutable' false
pub static A: i32;
//@ is '$.index[*][?(@.name=="B")].inner.static.is_unsafe' true
//@ is '$.index[*][?(@.name=="B")].inner.static.is_mutable' true
pub static mut B: i32;
// items in unadorned `extern` blocks cannot have safety qualifiers
}
unsafe extern "C" {
//@ is '$.index[*][?(@.name=="C")].inner.static.is_unsafe' true
//@ is '$.index[*][?(@.name=="C")].inner.static.is_mutable' false
pub static C: i32;
//@ is '$.index[*][?(@.name=="D")].inner.static.is_unsafe' true
//@ is '$.index[*][?(@.name=="D")].inner.static.is_mutable' true
pub static mut D: i32;
//@ is '$.index[*][?(@.name=="E")].inner.static.is_unsafe' false
//@ is '$.index[*][?(@.name=="E")].inner.static.is_mutable' false
pub safe static E: i32;
//@ is '$.index[*][?(@.name=="F")].inner.static.is_unsafe' false
//@ is '$.index[*][?(@.name=="F")].inner.static.is_mutable' true
pub safe static mut F: i32;
//@ is '$.index[*][?(@.name=="G")].inner.static.is_unsafe' true
//@ is '$.index[*][?(@.name=="G")].inner.static.is_mutable' false
pub unsafe static G: i32;
//@ is '$.index[*][?(@.name=="H")].inner.static.is_unsafe' true
//@ is '$.index[*][?(@.name=="H")].inner.static.is_mutable' true
pub unsafe static mut H: i32;
}
//@ ismany '$.index[*][?(@.inner.static)].inner.static.expr' '""' '""' '""' '""' '""' '""' '""' '""'
//@ ismany '$.index[*][?(@.inner.static)].inner.static.type.primitive' '"i32"' '"i32"' '"i32"' '"i32"' '"i32"' '"i32"' '"i32"' '"i32"'

View file

@ -1,10 +1,12 @@
//@ is '$.index[*][?(@.name=="A")].inner.static.type.primitive' '"i32"'
//@ is '$.index[*][?(@.name=="A")].inner.static.is_mutable' false
//@ is '$.index[*][?(@.name=="A")].inner.static.expr' '"5"'
//@ is '$.index[*][?(@.name=="A")].inner.static.is_unsafe' false
pub static A: i32 = 5;
//@ is '$.index[*][?(@.name=="B")].inner.static.type.primitive' '"u32"'
//@ is '$.index[*][?(@.name=="B")].inner.static.is_mutable' true
// Expr value isn't gaurenteed, it'd be fine to change it.
//@ is '$.index[*][?(@.name=="B")].inner.static.expr' '"_"'
//@ is '$.index[*][?(@.name=="B")].inner.static.is_unsafe' false
pub static mut B: u32 = 2 + 3;