Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Use raw identifiers for identifiers which collide with Rust keywords - #476

Merged
djc merged 8 commits into
askama-rs:mainfrom
SciStarter:main
Apr 21, 2021
Merged

Use raw identifiers for identifiers which collide with Rust keywords#476
djc merged 8 commits into
askama-rs:mainfrom
SciStarter:main

Conversation

@SciStarter

Copy link
Copy Markdown
Contributor

Rust keywords are valid identifier names in templates, so allow the template expansions to use those identifiers safely, by turning them into raw identifiers during the parse stage.

@vallentin

vallentin commented Apr 19, 2021

Copy link
Copy Markdown
Collaborator

Unrelated, but why are you posting as @SciStarter, but committing as @djarb?

@SciStarter

SciStarter commented Apr 19, 2021 via email

Copy link
Copy Markdown
Contributor Author

@vallentin
vallentin requested a review from djc April 19, 2021 18:10
@djc

djc commented Apr 19, 2021

Copy link
Copy Markdown
Collaborator

Why do this in the parser? It would make much more sense to me in the code generator.

@SciStarter

SciStarter commented Apr 19, 2021 via email

Copy link
Copy Markdown
Contributor Author

@djc

djc commented Apr 19, 2021

Copy link
Copy Markdown
Collaborator

I'm sorry, but that doesn't make much sense to me. The parser is supposed to parse text into an AST that represents the template as parsed in a way that is easier to handle. The exact name of the variables only becomes a problem when they're used in the generated Rust code -- so that's where they should be renamed. I'm pretty sure there's only a few places where the code generator generates code for variable references; you could start by looking here.

(Since we're generating code into a buffer here, I don't think we need parallel arrays with r#-prefixed keywords. Just one array of keywords is enough, and then the code generator should take care of pushing the prefix into the buffer where needed.)

@SciStarter

SciStarter commented Apr 19, 2021

Copy link
Copy Markdown
Contributor Author

Okay, I've moved the identifier normalization into the code generator. Is this more to your liking?

@djc

djc commented Apr 20, 2021

Copy link
Copy Markdown
Collaborator

This is much better, thanks! As I understand it, though, we don't actually need to store the raw form in the generator's data structures -- we only need to put the raw form in the generated code, right? That should simplify this some more and also make it easier to follow through on my suggestion from the last comment that we don't need the two separate arrays.

Also, please put the constants and helpers near the bottom of the module as per the convention in this project.

@SciStarter

Copy link
Copy Markdown
Contributor Author

Unfortunately, that doesn't work due to the way expressions are handled (specifically, because they're rendered into a returned string instead of directly into the output buffer). I'd have to make extensive changes to the expression processing.

@SciStarter

Copy link
Copy Markdown
Contributor Author

However, due to the return value actually being a String, I can actually do something reasonable along those lines after all.

@vallentin

Copy link
Copy Markdown
Collaborator

Ah, I see what the issue is. It's because the key in locals: MapChain<...> is a &str and not a String. A workaround could then be to change the key to a String or Cow<'a, str>. Then it would be possible for normalize_identifier() to return Cow<'a, str> instead.

The question now ofc is, whether the price of dynamically allocated keys is better or worse, than the increased binary size, by having the "duplicate" arrays.

@SciStarter

Copy link
Copy Markdown
Contributor Author

As a rule of thumb, I prefer increased size over slower execution, but that's just a general opinion. Do you have a preference in this instance?

@djc

djc commented Apr 20, 2021

Copy link
Copy Markdown
Collaborator

I think I prefer the constants, but let's make it one array of tuples rather than parallel arrays (and let rustfmt do its thing on it).

Comment thread askama_shared/src/generator.rs
@djc
djc merged commit 451ef35 into askama-rs:main Apr 21, 2021
@djc

djc commented Apr 21, 2021

Copy link
Copy Markdown
Collaborator

Thanks for doing the work on this!

@SciStarter

Copy link
Copy Markdown
Contributor Author

Thanks for being open to the idea :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants