diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2020-06-19 09:15:10 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-19 09:15:10 -0700 |
| commit | b285d68f36435b8ee75fc516be85b21eaa6c78c1 (patch) | |
| tree | a81e8012db6759bf10b43327c1c95066418799c3 /library/std/src/sys/unix/stack_overflow.rs | |
| parent | 058971cef3e7ce5925fa910239645f7c793d8dbc (diff) | |
| parent | 7a89a338239c0d551279b9183b9c2c1a1e7d7e74 (diff) | |
| download | rust-b285d68f36435b8ee75fc516be85b21eaa6c78c1.tar.gz rust-b285d68f36435b8ee75fc516be85b21eaa6c78c1.zip | |
Rollup merge of #73334 - ayazhafiz:err/num-type-cannot-fit, r=estebank
Note numeric literals that can never fit in an expected type
re https://github.com/rust-lang/rust/pull/72380#discussion_r438289385
Given the toy code
```rust
fn is_positive(n: usize) {
n > -1_isize;
}
```
We currently get a type mismatch error like the following:
```
error[E0308]: mismatched types
--> src/main.rs:2:9
|
2 | n > -1_isize;
| ^^^^^^^^ expected `usize`, found `isize`
|
help: you can convert an `isize` to `usize` and panic if the converted value wouldn't fit
|
2 | n > (-1_isize).try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
But clearly, `-1` can never fit into a `usize`, so the suggestion will
always panic. A more useful message would tell the user that the value
can never fit in the expected type:
```
error[E0308]: mismatched types
--> test.rs:2:9
|
2 | n > -1_isize;
| ^^^^^^^^ expected `usize`, found `isize`
|
note: `-1_isize` can never fit into `usize`
--> test.rs:2:9
|
2 | n > -1_isize;
| ^^^^^^^^
```
Which is what this commit implements.
I only added this check for negative literals because
- Currently we can only perform such a check for literals (constant
value propagation is outside the scope of the typechecker at this
point)
- A lint error for out-of-range numeric literals is already emitted
IMO it makes more sense to put this check in librustc_lint, but as far
as I can tell the typecheck pass happens before the lint pass, so I've
added it here.
r? @estebank
Diffstat (limited to 'library/std/src/sys/unix/stack_overflow.rs')
0 files changed, 0 insertions, 0 deletions
