rustdoc: Prototype implement RFC 3842 with safety::requires attribute#155201
Open
yilin0518 wants to merge 1 commit intorust-lang:mainfrom
Open
rustdoc: Prototype implement RFC 3842 with safety::requires attribute#155201yilin0518 wants to merge 1 commit intorust-lang:mainfrom
yilin0518 wants to merge 1 commit intorust-lang:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
e291eb3 to
02cded6
Compare
This comment has been minimized.
This comment has been minimized.
64eea17 to
f17a926
Compare
This comment has been minimized.
This comment has been minimized.
f17a926 to
9fcf976
Compare
This comment has been minimized.
This comment has been minimized.
9fcf976 to
a7aac10
Compare
* Support "#[safety::requires()]" attribute for documentation injection.
* Support "{Tag}={description}" format with customized tags.
* Prototype implementation of Safety contract insertion.
a7aac10 to
85c465d
Compare
Collaborator
|
These commits modify the If this was unintentional then you should revert the changes before this PR is merged. |
Collaborator
|
r? @notriddle rustbot has assigned @notriddle. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! I am a member of safer-rust, a group working toward safer patterns around
unsafeRust. In July 2025, we opened RFC 3842, which explores tagging public unsafe APIs with safety tags and#[safety]attribute.To help move that discussion forward with something concrete, this PR adds a prototype of one piece of that direction, applied in a way that already works with the standard library today. For now, we implement the inert
#[safety::requires(...)]attribute and a rustdoc pipeline that turns it into user-visible documentation before intra-doc links are resolved.Concretely, the PR adds:
--safety-spec PATH, pointing at a small TOML file (package.name + [tag.*] templates with {placeholder} substitution);inject-safety-docs, which reads#[safety::requires]on items (e.g. Tag(args…) expanded from TOML, or Tag = "…" for per-site text, including tags that appear in TOML and not in the TOML), and merges the generated Markdown into the item docs (typically under # Safety);#[safety::requires]attribute, we can use two forms:#[safety::requires(Tag(arg1, arg2, ...))]or#[safety::requires(Tag="description")]. The former requires the Tag must come from TOML. The latter can use costomized Tag name and use thedescriptionas its content. Also, the#[safety::requires(Tag="description")]transformssnake_casewhen the Tag doesn't exist in the TOML, replacing all underscores (_) with spaces and capitalizing the first letter.core, so we can add#[safety::requires]attributes incoreand can be processed by rustdoc.x.py doc library/...can pass a default spec path, while still allowing RUSTDOCFLAGS to override --safety-spec without duplicating the flag.As an emperimental tool, we performed tests in
core. We only modified one function(core::ptr::read_unaligned), adding corresponding#[safety::requires]attribute and removing the original safety contract. By using./x.py doc library/core --stage 1, we can generate the same safety description.Several days ago, we opened a zulip chat. Because this touches rustdoc (new unstable flag, default pass ordering, and doc merging) and core (tool registration for inert attributes), we would really appreciate any feedback from rust community. It would help us adjust the prototype and refocus the RFC discussion.