-
Notifications
You must be signed in to change notification settings - Fork 13.5k
rustdoc-json: Structured attributes #142936
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,8 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc | |
// will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line | ||
// are deliberately not in a doc comment, because they need not be in public docs.) | ||
// | ||
// Latest feature: Pretty printing of no_mangle attributes changed | ||
pub const FORMAT_VERSION: u32 = 53; | ||
// Latest feature: Structured Attributes | ||
pub const FORMAT_VERSION: u32 = 54; | ||
|
||
/// The root of the emitted JSON blob. | ||
/// | ||
|
@@ -195,13 +195,87 @@ pub struct Item { | |
/// - `#[repr(C)]` and other reprs also appear as themselves, | ||
/// though potentially with a different order: e.g. `repr(i8, C)` may become `repr(C, i8)`. | ||
/// Multiple repr attributes on the same item may be combined into an equivalent single attr. | ||
pub attrs: Vec<String>, | ||
pub attrs: Vec<Attribute>, | ||
/// Information about the item’s deprecation, if present. | ||
pub deprecation: Option<Deprecation>, | ||
/// The type-specific fields describing this item. | ||
pub inner: ItemEnum, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
/// An attribute, eg `#[repr(C)]` | ||
/// | ||
/// This doesn't include: | ||
/// - `#[doc = "Doc Comment"]` or `/// Doc comment`. These are in [`Item::docs`] instead. | ||
/// - `#[deprecated]`. These are in [`Item::deprecation`] instead. | ||
pub enum Attribute { | ||
aDotInTheVoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// `#[non_exhaustive]` | ||
NonExhaustive, | ||
|
||
/// `#[must_use]` | ||
MustUse { | ||
reason: Option<String>, | ||
}, | ||
|
||
/// `#[automatically_derived]` | ||
AutomaticallyDerived, | ||
|
||
/// `#[repr]` | ||
Repr(AttributeRepr), | ||
aDotInTheVoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// `#[no_mangle]` | ||
NoMangle, | ||
|
||
/// Something else. | ||
/// | ||
/// Things here are explicitly *not* covered by the [`FORMAT_VERSION`] | ||
/// constant, and may change without bumping the format version. | ||
/// | ||
/// As an implementation detail, this is currently either: | ||
/// 1. A HIR debug printing, like `"#[attr = Optimize(Speed)]"` | ||
/// 2. The attribute as it appears in source form, like | ||
/// `"#[optimize(speed)]"`. | ||
Other(String), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would love to break out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally doable after #142876 lands, which is probably before this PR. |
||
g | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
/// The contents of a `#[repr(...)]` attribute. | ||
/// | ||
/// Used in [`Attribute::Repr`]. | ||
pub struct AttributeRepr { | ||
/// The representation, e.g. `#[repr(C)]`, `#[repr(transparent)]` | ||
pub kind: ReprKind, | ||
|
||
/// Alignment in bytes, if explicitly specified by `#[repr(align(...)]`. | ||
pub align: Option<u64>, | ||
/// Alignment in bytes, if explicitly specified by `#[repr(packed(...)]]`. | ||
pub packed: Option<u64>, | ||
|
||
/// The integer type for an enum descriminant, if explicitly specified. | ||
/// | ||
/// e.g. `"i32"`, for `#[repr(C, i32)]` | ||
pub int: Option<String>, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
/// The kind of `#[repr]`. | ||
/// | ||
/// See [AttributeRepr::kind]`. | ||
pub enum ReprKind { | ||
/// `#[repr(Rust)]` | ||
/// | ||
/// Also the default. | ||
Rust, | ||
/// `#[repr(C)]` | ||
C, | ||
/// `#[repr(transparent)] | ||
Transparent, | ||
/// `#[repr(simd)]` | ||
Simd, | ||
} | ||
|
||
/// A range of source code. | ||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub struct Span { | ||
|
@@ -1343,7 +1417,7 @@ pub struct Static { | |
|
||
/// Is the static `unsafe`? | ||
/// | ||
/// This is only true if it's in an `extern` block, and not explicity marked | ||
/// This is only true if it's in an `extern` block, and not explicitly marked | ||
/// as `safe`. | ||
/// | ||
/// ```rust | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
//@ is "$.index[?(@.name=='cold_fn')].attrs" '["#[attr = Cold]"]' | ||
//@ is "$.index[?(@.name=='cold_fn')].attrs" '[{"other": "#[attr = Cold]"}]' | ||
#[cold] | ||
pub fn cold_fn() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
//@ edition: 2021 | ||
#![no_std] | ||
|
||
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]' | ||
//@ is "$.index[?(@.name=='example')].attrs" '[{"export_name": "altered"}]' | ||
#[export_name = "altered"] | ||
pub extern "C" fn example() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a separate concern unrelated to this PR, perhaps HTML output should list both
#[no_mangle]
and#[export_name]
in their edition 2024 form i.e. wrapped inunsafe
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please open an issue then so it's not forgotten.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already track this in #142835 (comment) (jana is assigned)