Skip to content

Commit 6cc6054

Browse files
authored
Unrolled build for rust-lang#127349
Rollup merge of rust-lang#127349 - estebank:negative-unsigned-literal, r=petrochenkov Tweak `-1 as usize` suggestion When writing a negative unsigned integer literal, use a verbose suggestion and account for `as` casting.
2 parents 2ad6630 + 86a1946 commit 6cc6054

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

compiler/rustc_hir_typeck/src/op.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
838838
},
839839
) = ex.kind
840840
{
841-
err.span_suggestion(
842-
ex.span,
841+
let span = if let hir::Node::Expr(parent) =
842+
self.tcx.parent_hir_node(ex.hir_id)
843+
&& let hir::ExprKind::Cast(..) = parent.kind
844+
{
845+
// `-1 as usize` -> `usize::MAX`
846+
parent.span
847+
} else {
848+
ex.span
849+
};
850+
err.span_suggestion_verbose(
851+
span,
843852
format!(
844853
"you may have meant the maximum value of `{actual}`",
845854
),

tests/ui/feature-gates/feature-gate-negate-unsigned.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ error[E0600]: cannot apply unary operator `-` to type `usize`
22
--> $DIR/feature-gate-negate-unsigned.rs:10:23
33
|
44
LL | let _max: usize = -1;
5-
| ^^
6-
| |
7-
| cannot apply unary operator `-`
8-
| help: you may have meant the maximum value of `usize`: `usize::MAX`
5+
| ^^ cannot apply unary operator `-`
96
|
107
= note: unsigned values cannot be negated
8+
help: you may have meant the maximum value of `usize`
9+
|
10+
LL | let _max: usize = usize::MAX;
11+
| ~~~~~~~~~~
1112

1213
error[E0600]: cannot apply unary operator `-` to type `u8`
1314
--> $DIR/feature-gate-negate-unsigned.rs:14:14

tests/ui/unsigned-literal-negation.stderr

+15-12
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@ error[E0600]: cannot apply unary operator `-` to type `usize`
22
--> $DIR/unsigned-literal-negation.rs:2:13
33
|
44
LL | let x = -1 as usize;
5-
| ^^
6-
| |
7-
| cannot apply unary operator `-`
8-
| help: you may have meant the maximum value of `usize`: `usize::MAX`
5+
| ^^ cannot apply unary operator `-`
96
|
107
= note: unsigned values cannot be negated
8+
help: you may have meant the maximum value of `usize`
9+
|
10+
LL | let x = usize::MAX;
11+
| ~~~~~~~~~~
1112

1213
error[E0600]: cannot apply unary operator `-` to type `usize`
1314
--> $DIR/unsigned-literal-negation.rs:3:13
1415
|
1516
LL | let x = (-1) as usize;
16-
| ^^^^
17-
| |
18-
| cannot apply unary operator `-`
19-
| help: you may have meant the maximum value of `usize`: `usize::MAX`
17+
| ^^^^ cannot apply unary operator `-`
2018
|
2119
= note: unsigned values cannot be negated
20+
help: you may have meant the maximum value of `usize`
21+
|
22+
LL | let x = usize::MAX;
23+
| ~~~~~~~~~~
2224

2325
error[E0600]: cannot apply unary operator `-` to type `u32`
2426
--> $DIR/unsigned-literal-negation.rs:4:18
2527
|
2628
LL | let x: u32 = -1;
27-
| ^^
28-
| |
29-
| cannot apply unary operator `-`
30-
| help: you may have meant the maximum value of `u32`: `u32::MAX`
29+
| ^^ cannot apply unary operator `-`
3130
|
3231
= note: unsigned values cannot be negated
32+
help: you may have meant the maximum value of `u32`
33+
|
34+
LL | let x: u32 = u32::MAX;
35+
| ~~~~~~~~
3336

3437
error: aborting due to 3 previous errors
3538

0 commit comments

Comments
 (0)