Skip to content

Detect negative literal inferred to unsigned integer#146042

Merged
bors merged 2 commits intorust-lang:masterfrom
estebank:issue-83413
Sep 1, 2025
Merged

Detect negative literal inferred to unsigned integer#146042
bors merged 2 commits intorust-lang:masterfrom
estebank:issue-83413

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Aug 30, 2025

error[E0277]: the trait bound `usize: Neg` is not satisfied
  --> $DIR/negative-literal-infered-to-unsigned.rs:2:14
   |
LL |     for x in -5..5 {
   |              ^^ the trait `Neg` is not implemented for `usize`
   |
help: consider specifying an integer type that can be negative
   |
LL |     for x in -5isize..5 {
   |                +++++

Applying this suggestion will always end up in another E0308 error at the point where the unsigned inference comes from, which should help with understanding what the actual problem is.

Fix #83413.

```
error[E0277]: the trait bound `usize: Neg` is not satisfied
  --> $DIR/negative-literal-infered-to-unsigned.rs:2:14
   |
LL |     for x in -5..5 {
   |              ^^ the trait `Neg` is not implemented for `usize`
   |
help: consider specifying an integer type that can be negative
   |
LL |     for x in -5isize..5 {
   |                +++++
```
@rustbot
Copy link
Collaborator

rustbot commented Aug 30, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 30, 2025
trait_pred: ty::PolyTraitPredicate<'tcx>,
err: &mut Diag<'_>,
) -> bool {
if let ObligationCauseCode::BinOp { lhs_hir_id, .. } = obligation.cause.code()
Copy link
Contributor

Choose a reason for hiding this comment

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

while you're here, can you change ObligationCauseCode to not just be BinOp 😅 that's horribly confusing.

Looking at the way it's used I think it'd be better to just split it into ObligationCauseCode::UnOp and ObligationCauseCode::BinOp

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

r=me after handling my comment

View changes since this review

@estebank
Copy link
Contributor Author

@bors r=lcnr

@bors
Copy link
Collaborator

bors commented Aug 31, 2025

📌 Commit ea2daa3 has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 31, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 1, 2025
Detect negative literal inferred to unsigned integer

```
error[E0277]: the trait bound `usize: Neg` is not satisfied
  --> $DIR/negative-literal-infered-to-unsigned.rs:2:14
   |
LL |     for x in -5..5 {
   |              ^^ the trait `Neg` is not implemented for `usize`
   |
help: consider specifying an integer type that can be negative
   |
LL |     for x in -5isize..5 {
   |                +++++
```

Applying this suggestion will always end up in another E0308 error at the point where the unsigned inference comes from, which should help with understanding what the actual problem is.

Fix rust-lang#83413.
bors added a commit that referenced this pull request Sep 1, 2025
Rollup of 6 pull requests

Successful merges:

 - #145421 (`dump_mir` cleanups)
 - #145968 (Add `Bound::copied`)
 - #146004 (resolve: Refactor `struct ExternPreludeEntry`)
 - #146042 (Detect negative literal inferred to unsigned integer)
 - #146046 (Suggest method name with maybe ty mismatch)
 - #146051 (Change std f32 test to pass under Miri)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 142ad69 into rust-lang:master Sep 1, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Sep 1, 2025
rust-timer added a commit that referenced this pull request Sep 1, 2025
Rollup merge of #146042 - estebank:issue-83413, r=lcnr

Detect negative literal inferred to unsigned integer

```
error[E0277]: the trait bound `usize: Neg` is not satisfied
  --> $DIR/negative-literal-infered-to-unsigned.rs:2:14
   |
LL |     for x in -5..5 {
   |              ^^ the trait `Neg` is not implemented for `usize`
   |
help: consider specifying an integer type that can be negative
   |
LL |     for x in -5isize..5 {
   |                +++++
```

Applying this suggestion will always end up in another E0308 error at the point where the unsigned inference comes from, which should help with understanding what the actual problem is.

Fix #83413.
Kobzol pushed a commit to Kobzol/rustc_codegen_cranelift that referenced this pull request Dec 29, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#145421 (`dump_mir` cleanups)
 - rust-lang/rust#145968 (Add `Bound::copied`)
 - rust-lang/rust#146004 (resolve: Refactor `struct ExternPreludeEntry`)
 - rust-lang/rust#146042 (Detect negative literal inferred to unsigned integer)
 - rust-lang/rust#146046 (Suggest method name with maybe ty mismatch)
 - rust-lang/rust#146051 (Change std f32 test to pass under Miri)

r? `@ghost`
`@rustbot` modify labels: rollup
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request Jan 27, 2026
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#145421 (`dump_mir` cleanups)
 - rust-lang/rust#145968 (Add `Bound::copied`)
 - rust-lang/rust#146004 (resolve: Refactor `struct ExternPreludeEntry`)
 - rust-lang/rust#146042 (Detect negative literal inferred to unsigned integer)
 - rust-lang/rust#146046 (Suggest method name with maybe ty mismatch)
 - rust-lang/rust#146051 (Change std f32 test to pass under Miri)

r? `@ghost`
`@rustbot` modify labels: rollup
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request Jan 27, 2026
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#145421 (`dump_mir` cleanups)
 - rust-lang/rust#145968 (Add `Bound::copied`)
 - rust-lang/rust#146004 (resolve: Refactor `struct ExternPreludeEntry`)
 - rust-lang/rust#146042 (Detect negative literal inferred to unsigned integer)
 - rust-lang/rust#146046 (Suggest method name with maybe ty mismatch)
 - rust-lang/rust#146051 (Change std f32 test to pass under Miri)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading error message with negative range

4 participants