Skip to content

[wgsl-in,ir] Add support for parsing rust-style doc comments #6364

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

Merged
merged 7 commits into from
Jun 5, 2025

Conversation

Vrixyz
Copy link
Contributor

@Vrixyz Vrixyz commented Oct 3, 2024

Connections
None as far as I know.

Description
Generating automated documentation à la https://docs.rs is very useful, but currently difficult.

naga lacks information about comments to be too helpful, so this pull requests adds support to parsing those.

Testing

Status

  • Add a token DocComment
  • Add a token DocCommentModule
  • Rust syntax was chosen: [wgsl-in,ir] Add support for parsing rust-style doc comments #6364 (comment)
    • Parse /// comments
    • Parse /** */ comments
    • Parse //! module comments
    • Parse /*! */ module comments
  • Add comments to ast::Struct
  • Support comments for multiple items
    • struct
      • struct members
    • var (global variables)
    • consts
    • functions
    • module level comment
      • For maximum compatibility with naga_oil, this is implemented with its own syntax: they should start with //!.
        • because naga_oil injects the imported modules at the beginning of the file, and not necessarily where the import line was written exactly. That prompted that commit: 02caf3a
    • alias ; 🚫 I consider this out of scope for this PR.
  • Add comments list in naga::Module
  • Added more test and adapted existing tests

Checklist

  • Run cargo fmt.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.

@Vrixyz Vrixyz requested a review from a team October 3, 2024 15:56
@Vrixyz Vrixyz marked this pull request as draft October 3, 2024 15:56
@jimblandy
Copy link
Member

This doesn't get the comments out of the front end, though, does it?

It seems to me it could be fine to just add a field like:

    doc_comments: Option<Box<DocComments>>,

to naga::Module, and then have something like

struct DocComments {
    types: FastIndexMap<Handle<Type>, Vec<Span>>,
    global_variables: FastIndexMap<Handle<GlobalVariable>, Vec<Span>>,
    ...
}

I think it would make sense for Naga to adopt Rust's /// syntax as a WGSL extension.

@Vrixyz
Copy link
Contributor Author

Vrixyz commented Oct 4, 2024

This doesn't get the comments out of the front end, though, does it?

Correct, I'm looking into propagating them to the module now ; my first intuition was to add comments to naga::TypeInner::Struct and the likes ; but I fear for performance.

Your idea of a "flat" DocComments with an indexmap to Spans is interesting and probably a better idea! Thanks for suggesting. I'll go in that direction.

Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more comments here.

You'll also need to change compact::compact to adjust the type handles, even though only named types can have comments and compaction never removes named types, because compaction may remove anonymous types, and that renumbers the handles.

naga/src/lib.rs Outdated
@@ -2263,4 +2263,27 @@ pub struct Module {
pub functions: Arena<Function>,
/// Entry points.
pub entry_points: Vec<EntryPoint>,
/// Comments, usually serving as documentation
pub comments: Comments,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an Option<Box<Comments>>, so that users who aren't interested in collecting these comments don't need to spend memory on them.

Copy link
Contributor Author

@Vrixyz Vrixyz Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the change ; we could also add a feature for that ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I didn't really say what I meant:

I think users should be able to choose to collect or ignore comments at run time. That is, naga::front::wgsl::FrontEnd::new should take an Options type, or FrontEnd should have a new_with_comments constructor - or whatever, something boring but tasteful - and then parsing should return a Module whose comments field is either populated or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! I addressed this in 57865be ; I'm open to suggestions about the implementation.

I updated a few tests to test both options.

@jimblandy
Copy link
Member

Note: I branched off 0.22 because I'm not confident with jumping on the maybe unstable trunk.

It should be fine to work on trunk. This PR should be rebased on that.

@jimblandy
Copy link
Member

@Vrixyz Since this isn't passing CI, to make it easier to track stuff I need to review, I'm going to convert this to a draft PR until those issues are sorted out. Feel free to flip it back into non-draft when you're ready.

@jimblandy jimblandy marked this pull request as draft January 8, 2025 03:34
@Vrixyz Vrixyz force-pushed the token-comment branch 3 times, most recently from 0025864 to 243445c Compare January 9, 2025 10:23
@Vrixyz Vrixyz changed the base branch from v22 to trunk January 9, 2025 10:24
@Vrixyz
Copy link
Contributor Author

Vrixyz commented Jan 9, 2025

I rebased on trunk (github comment reviews are degraded, but I adressed those 🤞), tests are encouraging, there's a major bug I'm investigating still (validation_error_messages): I think spans are incorrect, which I hope to fix by working on Jim's feedback.

❓ I'm curious about "pushing rules": I imagine parsing should contain a rule for parsing comments ? so we push that rule when checking for comments.

@Vrixyz Vrixyz marked this pull request as ready for review January 10, 2025 10:20
@Vrixyz Vrixyz requested a review from jimblandy January 10, 2025 10:21
@jimblandy
Copy link
Member

❓ I'm curious about "pushing rules": I imagine parsing should contain a rule for parsing comments ? so we push that rule when checking for comments.

You mean Parser::rules? You shouldn't need to worry about that at all. You should just call start_byte_offset and span_from directly.

The rule stack is currently being used as a rough approximation to WGSL's template list discovery, which you definitely do not want to get tangled up with, and which shouldn't affect your work at all. And it's used for some sanity checks.

@jimblandy jimblandy added naga Shader Translator area: naga front-end lang: WGSL WebGPU Shading Language labels Feb 6, 2025
@jimblandy
Copy link
Member

@Vrixyz In Rust, only comments starting with /// or /** are considered to be doc comments, and others are just ordinary comments. The syntax you've chosen doesn't make any distinction between the two. What are your thoughts about Rust's design? Would it be appropriate for WGSL?

@Vrixyz
Copy link
Contributor Author

Vrixyz commented Feb 24, 2025

What are your thoughts about Rust's design? Would it be appropriate for WGSL?

@jimblandy personally I'd advocate for mimicking Rust's syntax rules, and I'd be ok with implementing it 👍 (i.e ignore comments not starting by /// or /**).

But I chose to parse every comment because wgsl spec doesn't acknowledge "comment docs", as all comments are considered documentation.

That being said, I didn't quite follow my objective, because I didn't succeed in implementing module comments without adding a specific syntax: //!, which is similar to rust's syntax.

I made this decision because it made things simpler when working with naga_oil when multiple files are stitched together.

@jimblandy
Copy link
Member

@Vrixyz Let's go with the Rust syntax. Can we take care of it in this PR?

@jimblandy
Copy link
Member

* Can we record comments as Spans instead of Strings?

I don't think we can or should.

Okay, this is fine. We can revisit this question in the future if necessary.

@Vrixyz Vrixyz force-pushed the token-comment branch 2 times, most recently from 386ed0d to 9cb6d53 Compare February 25, 2025 09:51
@Vrixyz
Copy link
Contributor Author

Vrixyz commented Feb 25, 2025

Let's go with the Rust syntax. Can we take care of it in this PR?

I did that in 9cb6d53 👍

@Vrixyz Vrixyz changed the title [naga] Comments parsing [naga] Comments parsing (naga + wgsl) Feb 27, 2025
@cwfitzgerald cwfitzgerald assigned teoxoy and unassigned jimblandy Apr 9, 2025
@teoxoy
Copy link
Member

teoxoy commented Apr 11, 2025

The overall approach looks good, I mainly left some comments that I think would improve the impl and a one last question about usage.

@teoxoy
Copy link
Member

teoxoy commented Jun 5, 2025

I squashed the commits (since there were too many commits to rebase individually), rebased the PR and made some more modifications.

@Vrixyz let me know if the PR looks good, if so I think we can land it

@teoxoy teoxoy changed the title [naga] Comments parsing (naga + wgsl) [wgsl-in,ir] Add support for parsing rust-style doc comments Jun 5, 2025
@@ -2750,6 +2771,8 @@ impl Parser {
lexer: &mut Lexer<'a>,
out: &mut ast::TranslationUnit<'a>,
) -> Result<'a, ()> {
let doc_comments = lexer.accumulate_doc_comments();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is interesting, as we could use it to emit warning when doc comments are encountered when it shouldn't have been: check that doc_comments is empty for items that don't support it (separator, word...).

I'd consider this for another PR though, as it will lead to questions about error reporting, etc...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it would be nice to warn/error in these cases, through we need to make sure to only do so based on a setting or else this would make naga not spec compliant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could just do it based on options.parse_doc_comments.

@Vrixyz
Copy link
Contributor Author

Vrixyz commented Jun 5, 2025

Thanks @teoxoy for championing it! Looks great to me.

@teoxoy teoxoy self-requested a review June 5, 2025 13:11
@teoxoy teoxoy dismissed jimblandy’s stale review June 5, 2025 13:12

Comments have been addressed.

@teoxoy teoxoy merged commit 28af245 into gfx-rs:trunk Jun 5, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga front-end area: naga middle-end Intermediate representation lang: WGSL WebGPU Shading Language naga Shader Translator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants