Skip to content
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

rustdoc: Add item_template macro #112202

Merged
merged 1 commit into from
Jun 11, 2023

Conversation

nicklimmm
Copy link
Contributor

@nicklimmm nicklimmm commented Jun 2, 2023

Closes #112021

This change removes the use of self.borrows() in Askama templates, removes code duplication from item_and_mut_cx(), and improved readability by eliminating the prefix item_template_ when calling from the template.

References:

r? @GuillaumeGomez

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jun 2, 2023
@GuillaumeGomez
Copy link
Member

So overall it looks quite nice minus the few improvements I suggested. But one thing I'm considering here would be to directly use a macro.

Technical wondering: do we really need to use syn and quote here? The code is simple enough here that I'm not sure whether it's worth it or not. For quote it's almost for sure since all the generated code could be put in one format!(...).into().

Anyway, what do you think of this @rust-lang/rustdoc ?

@Manishearth
Copy link
Member

Manishearth commented Jun 2, 2023

Even extracting the struct name from the AST can be nontrivial so I do think we need syn and quote if we're using a proc macro.

However in this case I think a regular macro should be fine?

@nicklimmm
Copy link
Contributor Author

I'm not really sure how to achieve similar results here using regular macros (macro_rules).

One problem using regular macros here is when we need to add extra fields to the struct other than it and cx

For example ItemStruct in a different PR:

#[derive(Template)]
#[template(path = "item_struct.html")]
struct ItemStruct<'a, 'cx> {
    cx: std::cell::RefCell<&'a mut Context<'cx>>,
    it: &'a clean::Item,
    s: &'a clean::Struct, // <-- extra here
    should_render_fields: bool, // <-- extra here
}

@GuillaumeGomez
Copy link
Member

I suppose something like this would work:

macro_rules! item_template {
    (struct $name:ident {
cx: std::cell::RefCell<&'a mut Context<'cx>>,
    it: &'a clean::Item
$rest:tt*) => {{
       // implementation
    }}
}

@nicklimmm
Copy link
Contributor Author

nicklimmm commented Jun 3, 2023

Managed to pull it off (along with method selection), and the usage looks something like this:

    item_template!(
        #[template(path = "item_union.html")]
        struct ItemUnion<'a, 'cx> {
            cx: RefCell<&'a mut Context<'cx>>,
            it: &'a clean::Item,
            s: &'a clean::Union,
        },
        methods = [document, document_type_layout, render_attributes_in_pre, render_assoc_items]
    );

I think I prefer using regular macros instead of derives to prevent adding dependencies and create a new crate just for this issue.

Note: will remove the derive macro once this is finalized

@GuillaumeGomez
Copy link
Member

Looks great like this. 👍

@nicklimmm
Copy link
Contributor Author

Looks great like this. 👍

Lemme clean things up and remove the draft status once it's done :)

@nicklimmm nicklimmm changed the title rustdoc: Add ItemTemplate derive macro rustdoc: Add ItemTemplate macro Jun 3, 2023
@nicklimmm nicklimmm marked this pull request as ready for review June 3, 2023 16:58
@nicklimmm nicklimmm changed the title rustdoc: Add ItemTemplate macro rustdoc: Add item_template macro Jun 3, 2023
@nicklimmm
Copy link
Contributor Author

Bump @GuillaumeGomez whenever you are free :)

})
}
};
($method:tt $($rest:tt)*) => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
($method:tt $($rest:tt)*) => {
($method:ident $($rest:tt)*) => {

And very likely needs to add another case with:

    ($token:tt $($rest:tt)*) => {
        compile_error!(concat!("unexpected token: ", stringify($token));
    }

Copy link
Contributor Author

@nicklimmm nicklimmm Jun 10, 2023

Choose a reason for hiding this comment

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

Since we only expect idents for methods, seems like we can restrict them further by only allowing idents as input.

In this case, we need to modify the match rules in item_template_methods!()

Wdyt?

Ignore above: sounds better to catch everything with tt first, then match on ident on unknown method case

it: &'a clean::Item,
$($field_name:ident: $field_ty:ty),*,
},
methods = [$($methods:tt),* $(,)?]
Copy link
Member

@GuillaumeGomez GuillaumeGomez Jun 10, 2023

Choose a reason for hiding this comment

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

I really like the methods = part. It makes the macro easier to understand when using it/reading code. Well done! 👍

@GuillaumeGomez
Copy link
Member

One other thing: can you put the macros at the top of the file (right after the imports) please?

@GuillaumeGomez
Copy link
Member

Please just add an explanation on top of the macro on what it does and how to call it. Then squash your commit and it'll be time for r+. Nice work!

@GuillaumeGomez
Copy link
Member

Thanks!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 11, 2023

📌 Commit e240dab has been approved by GuillaumeGomez

It is now in the queue for this repository.

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 11, 2023
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jun 11, 2023
@bors
Copy link
Contributor

bors commented Jun 11, 2023

⌛ Testing commit e240dab with merge 7b6093e...

@bors
Copy link
Contributor

bors commented Jun 11, 2023

☀️ Test successful - checks-actions
Approved by: GuillaumeGomez
Pushing 7b6093e to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 11, 2023
@bors bors merged commit 7b6093e into rust-lang:master Jun 11, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 11, 2023
@nicklimmm nicklimmm deleted the item-template-derive-macro branch June 11, 2023 14:48
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7b6093e): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.4% [1.4%, 1.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.9% [-1.9%, -1.9%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 647.255s -> 647.688s (0.07%)

@coffinmatician
Copy link

coffinmatician commented Jul 21, 2023

This PR introduces an ICE regression in geo v0.22.1 if you build with cargo build --release. You first need to update the Cargo.lock with cargo update (or see the version that I attached below).

Cargo.lock.txt

error[E0275]: overflow evaluating the requirement `[closure@src/algorithm/map_coords.rs:888:69: 888:72]: Fn<(geo_types::Coord<T>,)>`
  |
  = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`geo`)
  = note: required for `&[closure@src/algorithm/map_coords.rs:888:69: 888:72]` to implement `Fn<(geo_types::Coord<T>,)>`
  = note: 128 redundant requirements hidden
  = note: required for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...` to implement `Fn<(geo_types::Coord<T>,)>`
  = note: the full type name has been written to '/home/alex/Downloads/geo-0.22.1/target/release/deps/geo-31a6d43c26e8a8cc.long-type-15151749810108145118.txt'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustdoc: remove .borrow() calls in Askama templates for ItemTemplate implementors
7 participants