Skip to content

Compiler misinterprets cast and comparison as generic syntax #140235

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

Closed
reddevilmidzy opened this issue Apr 24, 2025 · 1 comment
Closed

Compiler misinterprets cast and comparison as generic syntax #140235

reddevilmidzy opened this issue Apr 24, 2025 · 1 comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@reddevilmidzy
Copy link
Contributor

I tried this code:

fn main() {
    let i = 1;
    if i as usize < 1 {}
}

I expected to compile successfully, just like the following code:

// this code compile successfully
fn main() {
    let i = 1;
    if i as usize > 1 {} // Change `<` to `>`
}

Instead, this happened: compile fail

error: `<` is interpreted as a start of generic arguments for `usize`, not a comparison
 --> src\main.rs:3:19
  |
3 |     if i as usize < 1 {}
  |                   ^ --- interpreted as generic arguments
  |                   |
  |                   not interpreted as comparison
  |
help: try comparing the cast value
  |
3 |     if (i as usize) < 1 {}
  |        +          +

I suspect that the compiler is interpreting the code as if I had written something like usize::<1>.

Meta

rustc --version --verbose:

<version>
rustc 1.88.0-nightly (0fe8f3454 2025-04-10)
binary: rustc
commit-hash: 0fe8f3454dbe9dda52a254991347e96bec579a6f
commit-date: 2025-04-10
host: x86_64-pc-windows-msvc
release: 1.88.0-nightly
LLVM version: 20.1.2
@reddevilmidzy reddevilmidzy added the C-bug Category: This is a bug. label Apr 24, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 24, 2025
@y21
Copy link
Member

y21 commented Apr 24, 2025

I don't think that's a bug in the compiler. The syntax for as casts is <Expression> as <Type>. Turbofish (::<) is only required for expression paths, not type paths, so as usize< in this context indeed means that it's the start of a generic arguments list rather than the less than operator as the error message points out, and the fix to wrap the left hand side in parentheses seems correct

@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants