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

Allow idents to start with _ #244

Merged
merged 6 commits into from
Nov 20, 2024

Conversation

GuillaumeGomez
Copy link
Contributor

Until now, we didn't allow _x as ident. Fixed it. :)

Comment on lines -629 to +640
(None, name, []) if name.chars().next().map_or(true, char::is_lowercase) => {
(None, name, [])
if name
.chars()
.next()
.map_or(true, |c| c == '_' || c.is_lowercase()) =>
Copy link
Collaborator

@Kijewski Kijewski Nov 15, 2024

Choose a reason for hiding this comment

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

I'm kinda confused if it is okay that this function returns PathOrIdentifier::Identifier("_") for the placeholder with this change. Before, PathOrIdentifier::Path(vec!["_"]) would be returned. Actually, both look wrong to me.

Can you have a look what happens of you add a case ↓ before the changed one?

(None, "_", _) => fail.parse_next(initial_i),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it didn't change anything as far as I could see.

@Kijewski
Copy link
Collaborator

Good catch! Interesting that this case did not occur earlier.

@GuillaumeGomez
Copy link
Contributor Author

The original patch was not enough so I greatly extended it. It is now much more useful. :)

@Kijewski
Copy link
Collaborator

Sorry, I'm not quite happy with the second commit. IMHO on the left-hand side of an assignment, the target, placeholders should always be allowed without restrictions. Maybe you are are only interested in the side-effect of an assignment {% let _ = it.next() %}, or it must have a specific form {% let [_] = slice.try_into().unwrap() %}.

Maybe you could remove the 2nd commit from this PR and move it into a new one, and I'll have a look at it, then?

@GuillaumeGomez
Copy link
Contributor Author

Good point. Wouldn't you prefer that I update the second commit to allow it instead?

@Kijewski
Copy link
Collaborator

I have no strong opinion. :) We can keep it in one PR.

@GuillaumeGomez
Copy link
Contributor Author

Then leave me some time so I can check what actual cases are allowed on the lhs.

@Kijewski
Copy link
Collaborator

Take as much time as you need. :)

@cipriancraciun
Copy link
Contributor

cipriancraciun commented Nov 18, 2024

I think I've opened this can of worms with my initial issue on Askama: https://github.com/djc/askama/issues/1105

(I've tested all of the following examples on the current pull request commit GuillaumeGomez@05ae1ba)

In essence, for variable names I think the following cases should be covered as they are allowed by Rust (and most other programming languages):

  • {% let _ = 42 %} -- fails with error: reserved keyword _ cannot be used here; why would someone use this?
    • perhaps for the side-effect;
    • perhaps when refactoring some code where we one doesn't want to change too much;
  • {% let _x = 42 %} or {% let _X = 42 %} -- my initial issue, which fails with latest Rinja release, works with this pull request; why would someone use this?
    • for example I always prefix variable names with _ to highlight that they are locals, and not some crate or something imported; (I know that in Rust an identifier starting with _ is treated specially by the compiler, but it works;)
    • any other reason, like for example coming from Python, and creating some templates for Rinja;
    • for example one could use __, i.e. two (or more) underscores, as to mean "reserved for base and partials to communicate between themselves", etc., just as the Python convention;
  • {% let X = 42 %} (i.e. uppercase variables) -- fails with error: literals are not allowed on the left-hand side of an assignment; why would someone use this?
    • for example in a base template, or perhaps in an include, someone would like to define a variable that is all upper-case to denote a constant;

Thus, it would be nice before merging this pull-request, to cover a few more corner-cases.

It would be nice (unless it conflicts with the Jinja syntax) that all Rust valid identifiers (as specified in https://doc.rust-lang.org/reference/identifiers.html) to be considered valid identifiers for let, for, match and other places where identifiers are to be used.

@GuillaumeGomez
Copy link
Contributor Author

{% let X = 42 %}

For this one, please open another issue. For the rest, I'll add a regression test for them.

@GuillaumeGomez
Copy link
Contributor Author

I re-allowed the few cases that should not be forbidden. :)

@@ -1118,6 +1118,7 @@ impl<'a> Generator<'a> {
_ => Ok(false),
}
}
Target::Placeholder("_") => Ok(false),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't wildcard .. handling needed, too?

Target::Rest(var) => match &**var {
  Some(var) => "what's done in Target::Name(_)",
  None => Ok(false),
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point!

@GuillaumeGomez
Copy link
Contributor Author

Added handling for ...

@@ -997,6 +997,7 @@ impl<'a> Generator<'a> {
_ => Ok(false),
}
}
Target::Placeholder("_" | "..") => Ok(false),
Copy link
Collaborator

Choose a reason for hiding this comment

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

The second case will never be true, though. Target::Placeholder(_) only ever captures "_", and Target::Rest(_) captures .. or .. @ var.

Both variant should probably contain a WithSpan<'a, ()> instead of the captured token, to make the meaning more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, gonna clean up this code because there is no point in keeping content if it's not used.

Comment on lines 12 to 17
Target::Name("_") | Target::Placeholder(_) => {
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(
"reserved keyword `_` cannot be used here",
i,
)));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMHO, this use of placeholders is fine. Maybe I want to {% let _ = iter.next() %} or {% let [_] = slice.try_into().unwrap() %}, because I want to test something in my code, generate an effect, whatever. Sorry, but I think whole function can the removed, because I don't want to restrict what can be skipped on the lhs of an assignment. :-/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, let's go then. :)

@GuillaumeGomez
Copy link
Contributor Author

Ah, we have a new license in our dependency tree apparently.

allow = ["Apache-2.0", "MIT", "MIT-0", "Unicode-DFS-2016"]
allow = ["Apache-2.0", "MIT", "MIT-0", "Unicode-3.0"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, the license was changed "about 5 hours ago" :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lucky me. :')

Copy link
Collaborator

@Kijewski Kijewski left a comment

Choose a reason for hiding this comment

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

Thank you very much for the change! :)

@GuillaumeGomez GuillaumeGomez merged commit b856eb8 into rinja-rs:master Nov 20, 2024
36 checks passed
@GuillaumeGomez GuillaumeGomez deleted the allow-underscore branch November 20, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants