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

Undocumented: Identifier r#move cannot be used as the name of a constant #137128

Closed
wuwbobo2021 opened this issue Feb 16, 2025 · 2 comments · Fixed by #137140
Closed

Undocumented: Identifier r#move cannot be used as the name of a constant #137128

wuwbobo2021 opened this issue Feb 16, 2025 · 2 comments · Fixed by #137140
Assignees
Labels
A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. S-has-bisection Status: a bisection has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@wuwbobo2021
Copy link

Location

https://doc.rust-lang.org/reference/items/constant-items.html

Summary

//const r#move : i32 = 17760257; // It does not compile
fn main() {
    fn r#move() {} // It does compile
}
   Compiling playground v0.0.1 (/playground)
error: expected item, found keyword `const`
 --> src/main.rs:1:1
  |
1 | const r#move : i32 = 17760257;
  | ^^^^^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: could not compile `playground` (bin "playground") due to 1 previous error

This may cause problem in generated bindings for APIs in other languages.

https://developer.android.com/reference/android/R.transition.html#move

@wuwbobo2021 wuwbobo2021 added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Feb 16, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 16, 2025
@cyrgani
Copy link
Contributor

cyrgani commented Feb 16, 2025

This code used to compile fine in 1.68: https://godbolt.org/z/Mzx6PdTeM, but became a parse error in 1.69. I can't find anything in the release notes about this either.
@rustbot label: regression-from-stable-to-stable S-has-bisection

bisection gives:


Regression in nightly-2023-02-06


fetching https://static.rust-lang.org/dist/2023-02-05/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2023-02-05: 40 B / 40 B [===================================================================================================] 100.00 % 422.10 KB/s converted 2023-02-05 to 50d3ba5
fetching https://static.rust-lang.org/dist/2023-02-06/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2023-02-06: 40 B / 40 B [===================================================================================================] 100.00 % 424.34 KB/s converted 2023-02-06 to 75a0be9
looking for regression commit between 2023-02-05 and 2023-02-06
fetching (via remote github) commits from max(50d3ba5, 2023-02-03) to 75a0be9
ending github query because we found starting sha: 50d3ba5
get_commits_between returning commits, len: 6
commit[0] 2023-02-04: Auto merge of #107672 - matthiaskrgr:rollup-7e6dbuk, r=matthiaskrgr
commit[1] 2023-02-05: Auto merge of #107434 - BoxyUwU:nll_const_equate, r=compiler-errors
commit[2] 2023-02-05: Auto merge of #107679 - est31:less_import_overhead, r=compiler-errors
commit[3] 2023-02-05: Auto merge of #102842 - rol1510:issue-85566-fix, r=notriddle
commit[4] 2023-02-05: Auto merge of #107663 - matthiaskrgr:107423-point-at-EOF-code, r=compiler-errors
commit[5] 2023-02-05: Auto merge of #107526 - obeis:for-missing-iterator, r=estebank,compiler-errors

@rustbot rustbot added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. I-prioritize Issue: Indicates that prioritization has been requested for this issue. S-has-bisection Status: a bisection has been found for this issue labels Feb 16, 2025
@Noratrieb
Copy link
Member

Noratrieb commented Feb 16, 2025

The bisection above does not seem to be correct 🤔

This happens because when parsing items, for some reason it checks that it's not a parsing a const closure, and const closures may be const move, and therefore it bails and treats it as invalid.

token::Ident(kw::Move | kw::Static, _) | token::OrOr | token::BinOp(token::Or) => {

@Noratrieb Noratrieb self-assigned this Feb 16, 2025
@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 16, 2025
@Noratrieb Noratrieb removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Feb 16, 2025
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. labels Feb 16, 2025
@fmease fmease removed the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Feb 16, 2025
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Feb 16, 2025
…ompiler-errors

Fix const items not being allowed to be called `r#move` or `r#static`

Because of an ambiguity with const closures, the parser needs to ensure that for a const item, the `const` keyword isn't followed by a `move` or `static` keyword, as that would indicate a const closure:

```rust
fn main() {
  const move // ...
}
```

This check did not take raw identifiers into account, therefore being unable to distinguish between `const move` and `const r#move`. The latter is obviously not a const closure, so it should be allowed as a const item.

This fixes the check in the parser to only treat `const ...` as a const closure if it's followed by the *proper keyword*, and not a raw identifier.

Additionally, this adds a large test that tests for all raw identifiers in all kinds of positions, including `const`, to prevent issues like this one from occurring again.

fixes rust-lang#137128
@bors bors closed this as completed in f071099 Feb 17, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 17, 2025
Rollup merge of rust-lang#137140 - Noratrieb:const-move, r=jieyouxu,compiler-errors

Fix const items not being allowed to be called `r#move` or `r#static`

Because of an ambiguity with const closures, the parser needs to ensure that for a const item, the `const` keyword isn't followed by a `move` or `static` keyword, as that would indicate a const closure:

```rust
fn main() {
  const move // ...
}
```

This check did not take raw identifiers into account, therefore being unable to distinguish between `const move` and `const r#move`. The latter is obviously not a const closure, so it should be allowed as a const item.

This fixes the check in the parser to only treat `const ...` as a const closure if it's followed by the *proper keyword*, and not a raw identifier.

Additionally, this adds a large test that tests for all raw identifiers in all kinds of positions, including `const`, to prevent issues like this one from occurring again.

fixes rust-lang#137128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. S-has-bisection Status: a bisection has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants