close
Skip to content

rustdoc: Add [arg@name] intra-doc syntax for referencing function arguments#153734

Open
nik-rev wants to merge 1 commit intorust-lang:mainfrom
nik-contrib:intradoc-args
Open

rustdoc: Add [arg@name] intra-doc syntax for referencing function arguments#153734
nik-rev wants to merge 1 commit intorust-lang:mainfrom
nik-contrib:intradoc-args

Conversation

@nik-rev
Copy link
Copy Markdown
Contributor

@nik-rev nik-rev commented Mar 11, 2026

This PR adds a feature that allows referencing function arguments using the intra-doc syntax [arg@name].

Example

#![feature(intra_doc_arg)]

/// Apply a blur to an image of known [`arg@width`] and [`arg@height`],
/// where the raw image data resides in [`arg@img_data`]
/// in a row-major fashion.
fn blur(image_data: &mut [f32], width: u32, height: u32) {}

You may not be able to tell at a first glance, but the above example actually contains a typo. The img_data argument does not exist; instead, the function author meant to refer to the image_data argument.

Thankfully, because they explicitly referred to the argument with the intra-doc syntax [`arg@img_data`], rustdoc will emit an error for the above example:

error: argument `img_data` does not exist
  --> src/main.rs:27:16
   |
27 |     /// where the raw image data resides in [`arg@img_data`]
   |                                              ^^^^^^^^^^^^^^

Impact on generated HTML

In terms of the generated documentation, arg@ intra-doc "links" don't impact the HTML output at all. The links are stripped completely, and they are rendered equivalently to the following documentation:

/// Apply a blur to an image of known `width` and `height`,
/// where the raw image data resides in `image_data`
/// in a row-major fashion.

Exciting possibilities

if rustdoc gains an official syntax for referring to function arguments, rust-analyzer could also implement that - so you could rename your function's arguments and they would automatically be renamed in the documentation, for example.

Notes

Using the arg@ prefix is required. But if we are happy with it being inferred, I can make that happen.

Relevant issue: #57525

Implementation

  1. PageId (the field that says "where is this this item's documentation") is now Option<DefId> instead of DefId, because intra-doc argument references do not refer to any particular item (function arguments are not real items)
  2. Because of that, RenderedLink's href field is now Option<String> instead of String. When it is None, no link is generated in the HTML at all.
  3. When it comes to modifying the markdown Events, the Event::Start(Link) is replaced with an empty text, and Event::End(Link) is also. So there is no link in the output.

Zulip discussion: https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Allow.20referencing.20function.20arguments.20in.20function.20docs.3F/with/577839079

@rustbot rustbot added A-rustdoc-json Area: Rustdoc JSON backend S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Mar 11, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 11, 2026

r? @notriddle

rustbot has assigned @notriddle.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: rustdoc
  • rustdoc expanded to 9 candidates
  • Random selection from GuillaumeGomez, camelid, fmease, lolbinarycat, notriddle

@rust-log-analyzer

This comment has been minimized.

@nik-rev nik-rev force-pushed the intradoc-args branch 2 times, most recently from cbe7b7b to c709d3f Compare March 11, 2026 18:21
@GuillaumeGomez
Copy link
Copy Markdown
Member

Mostly for me: the feature as of now checks if an argument exists and if so, replace the intra-doc link with the argument name, allowing to check that the docs are up to date. Not sure if it's worth generating IDs for all function arguments as it would increase quite a lot the HTML size. Maybe we could only generate IDs for arguments which are pointed by arg@? Anyway, can be discussed later on. Checking code now. :)

@nik-rev
Copy link
Copy Markdown
Contributor Author

nik-rev commented Mar 11, 2026

Considering what @kpreid said on zulip in (#t-rustdoc > Allow referencing function arguments in function docs? @ 💬):

I worry that always having arg@ significantly reduces readability of the doc as plain comments, and people might refrain from using the feature at all as a result. (But that could be changed compatibly later, since arg@ wouldn't mean anything else.

I agree with this quite a lot, so I have pushed a new commit (edit: reverted, but can bring back if needed) that adds implicit function argument support. I will squash that if this PR gets merged, but also keeping it as a separate commit so it can be easily reverted if needed.

@GuillaumeGomez
Copy link
Copy Markdown
Member

I commented here why I don't think we should put the function arguments at the same level.

Comment thread src/librustdoc/clean/types.rs
Comment thread src/librustdoc/html/markdown.rs Outdated
Comment thread src/librustdoc/html/markdown.rs Outdated
Comment thread src/librustdoc/html/markdown.rs Outdated
Comment thread src/librustdoc/html/markdown.rs Outdated
@nik-rev nik-rev marked this pull request as draft March 11, 2026 20:31
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 11, 2026
@rust-log-analyzer

This comment has been minimized.

@nik-rev nik-rev force-pushed the intradoc-args branch 2 times, most recently from 483d7bf to 28f9bdf Compare March 11, 2026 20:58
@rust-log-analyzer

This comment has been minimized.

@nik-rev
Copy link
Copy Markdown
Contributor Author

nik-rev commented Mar 11, 2026

All unrelated code changes have been removed

@nik-rev nik-rev marked this pull request as ready for review March 11, 2026 21:17
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 11, 2026
@nik-rev nik-rev requested a review from GuillaumeGomez March 11, 2026 21:17
@notriddle notriddle added needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. and removed needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. labels Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-rustdoc-json Area: Rustdoc JSON backend S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants