carbon-lang
carbon-lang copied to clipboard
Inconsistencies regarding '_' as an identifier.
Description of the bug:
Per explorer/syntax/lexer.lpp:135 - an identifier token is defined to be a token that matches the regular expression [A-Za-z_][A-Za-z0-9_]*. When trying some simple examples, I get some inconsistent results.
What did you do, or what's a simple way to reproduce the bug?
The simplest example is package _ api; which produces "SYNTAX ERROR: <source>:1: syntax error, unexpected UNDERSCORE, expecting identifier". At the very least, this error is confusing because _ is an identifier.
What did you expect to happen?
I expected _ to be a valid identifier in all contexts.
What actually happened?
Sometimes _ is allowed to be an identifier, sometimes not.
Any other information, logs, or outputs that you want to share?
1. package foo api;
2.
3. fn Main() -> i32 {
4. let _: i32 = 0;
5. return _;
6. }
Interestingly enough, the syntax error occurs on line 5. If I change return _; to return 0;, the explorer exits with "result: 0" despite line 4 having an identifier _.
The lexer has a dedicated UNDERSCORE token, which superseeds identifier. _ can be used in some places in place of an identifier, but it does not mean that all identifiers can be _. The main use for _ AFAIK is when you need to provide a binding, but don't want to use it, like for an unused parameter or return value.
From https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/README.md#binding-patterns
A underscore (_) may be used instead of the name to match a value but without binding any name to it.
As such, return _ is not valid, because _ is not an actual named value.
Regarding the error, I think most users would not interpret it as "_ is an identifier, why can't I use it?", because most wouldn't be looking at the parser source. But agree that most errors would still benefit from being clarified.
We have an approved proposal that very strongly implies that _ is not an identifier, and is instead a keyword (or similar special token). Specifically, the pattern matching proposal uses _ as a grammar terminal and says "with _ in place of an identifier". Because design updates for #2188 have not been done yet, the keywords list does not yet include _, but I would expect that to change.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive label. The long term label can also be added for issues which are expected to take time. \n\n\n This issue is labeled inactive because the last activity was over 90 days ago.
Closing explorer-specific issues as not-planned for now due to our decision to prioritize working on the toolchain over other implementation work in the near term: https://github.com/carbon-language/carbon-lang/blob/trunk/proposals/p3532.md