Manish Goregaokar
b285d68f36
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
2020-06-19 09:15:10 -07:00
..
2020-05-10 01:25:30 +02:00
2020-04-23 20:21:38 -07:00
2020-05-16 19:46:29 +02:00
2020-06-12 00:05:19 +02:00
2020-05-02 18:24:07 -07:00
2020-05-31 20:15:32 +01:00
2020-04-18 16:37:08 -07:00
2020-06-11 16:24:01 +01:00
2020-06-16 17:36:55 -07:00
2020-05-25 23:44:40 -04:00
2020-06-15 09:06:58 -07:00
2020-04-23 20:21:38 -07:00
2020-05-03 19:24:41 +03:00
2020-05-09 14:40:17 +02:00
2020-06-11 17:40:40 -04:00
2020-04-24 00:22:50 +02:00
2020-06-15 09:57:28 +02:00
2020-05-30 10:22:26 -07:00
2020-06-04 15:59:59 +01:00
2020-06-15 08:57:20 -07:00
2020-06-04 15:59:59 +01:00
2020-05-07 17:35:58 -04:00
2020-06-11 17:40:40 -04:00
2020-04-18 17:19:53 -07:00
2020-06-15 08:57:20 -07:00
2020-06-15 09:57:22 +02:00
2020-06-18 15:20:57 -07:00
2020-04-23 15:46:05 +08:00
2020-05-12 09:34:23 -07:00
2020-05-27 16:28:20 -07:00
2020-06-08 18:04:41 -07:00
2020-06-19 08:56:02 +02:00
2020-05-31 20:15:32 +01:00
2020-04-23 20:21:38 -07:00
2020-06-09 08:49:05 +05:30
2020-05-01 23:26:52 -04:00
2020-05-28 10:33:07 +01:00
2020-05-20 16:08:09 +02:00
2020-05-28 10:33:07 +01:00
2020-06-15 19:46:27 -07:00
2020-05-05 19:35:20 -07:00
2020-05-07 02:22:08 +09:00
2020-06-10 01:35:47 +00:00
2020-06-15 08:57:20 -07:00
2020-05-26 23:06:46 +03:00
2020-05-26 21:49:55 -04:00
2020-06-15 19:46:27 -07:00
2020-06-15 04:10:24 +00:00
2020-05-08 00:39:02 +09:00
2020-06-13 01:18:18 +00:00
2020-04-30 10:23:45 -04:00
2020-06-08 14:58:37 -07:00
2020-05-30 10:22:26 -07:00
2020-04-22 12:42:02 -07:00
2020-06-13 01:18:18 +00:00
2020-05-08 00:39:02 +09:00
2020-05-27 16:28:20 -07:00
2020-06-11 17:40:40 -04:00
2020-05-08 00:39:02 +09:00
2020-05-09 14:40:17 +02:00
2020-05-27 16:27:15 -07:00
2020-06-18 15:20:43 -07:00
2020-05-08 00:39:02 +09:00
2020-05-27 20:58:05 -07:00
2020-05-09 14:40:17 +02:00
2020-06-15 12:01:01 +02:00
2020-04-17 06:16:14 +09:00
2020-06-19 09:15:08 -07:00
2020-06-10 01:35:47 +00:00
2020-05-26 01:18:07 +05:30
2020-05-30 13:55:25 +02:00
2020-05-30 10:22:26 -07:00
2020-04-30 17:27:33 +02:00
2020-06-19 09:15:08 -07:00
2020-05-29 17:46:38 +02:00
2020-06-04 15:59:59 +01:00
2020-05-08 00:39:02 +09:00
2020-05-26 21:49:55 -04:00
2020-04-18 16:37:08 -07:00
2020-06-02 00:23:47 +02:00
2020-04-14 07:12:07 +08:00
2020-05-28 16:50:10 +09:00
2020-05-03 11:36:11 -07:00
2020-06-15 08:57:20 -07:00
2020-04-16 14:05:57 +02:00
2020-06-08 12:00:12 +01:00
2020-06-11 18:10:13 -04:00
2020-05-07 00:33:25 -04:00
2020-05-08 00:39:02 +09:00
2020-06-15 09:57:28 +02:00
2020-06-10 01:35:47 +00:00
2020-06-16 20:05:55 -07:00
2020-06-15 09:06:58 -07:00
2020-06-13 01:18:18 +00:00
2020-05-08 00:39:02 +09:00
2020-05-08 00:39:02 +09:00
2020-06-19 14:29:29 +02:00
2020-06-13 01:18:18 +00:00
2020-06-11 16:24:01 +01:00
2020-06-11 19:04:09 +02:00
2020-05-08 00:39:02 +09:00
2020-05-06 23:41:38 -07:00
2020-06-15 12:01:01 +02:00
2020-06-15 09:06:58 -07:00
2020-06-09 08:49:05 +05:30
2020-05-08 00:39:02 +09:00
2020-06-10 17:30:11 -04:00
2020-05-30 10:22:26 -07:00
2020-06-16 21:34:36 +08:00
2020-05-09 14:40:17 +02:00
2020-05-09 14:40:17 +02:00
2020-05-01 17:32:06 +02:00
2020-05-07 00:33:25 -04:00
2020-06-13 14:24:30 +02:00
2020-06-11 16:24:01 +01:00
2020-06-15 09:06:58 -07:00
2020-06-10 01:35:47 +00:00
2020-06-10 14:09:51 -07:00
2020-05-02 18:24:07 -07:00
2020-05-02 21:14:08 +03:00
2020-05-04 23:47:00 +02:00
2020-05-08 00:39:02 +09:00
2020-05-08 00:39:02 +09:00
2020-06-19 09:15:06 -07:00
2020-06-06 22:36:21 -04:00
2020-06-04 15:59:59 +01:00
2020-05-06 14:02:55 +09:00
2020-05-06 14:02:55 +09:00
2020-06-15 09:57:24 +02:00
2020-04-14 18:39:20 +02:00
2020-06-10 14:09:51 -07:00
2020-05-02 18:23:46 -07:00
2020-06-18 15:20:57 -07:00
2020-06-15 01:09:26 +01:00
2020-06-11 17:40:40 -04:00
2020-04-23 20:21:38 -07:00
2020-06-15 09:06:58 -07:00
2020-06-15 08:57:20 -07:00
2020-06-10 14:09:51 -07:00
2020-05-27 16:28:20 -07:00
2020-06-16 17:24:16 -07:00
2020-06-11 18:10:13 -04:00
2020-06-01 16:29:25 -07:00
2020-06-11 18:10:13 -04:00
2020-06-09 13:21:27 -03:00
2020-05-08 00:39:02 +09:00
2020-06-16 17:24:16 -07:00
2020-04-18 17:19:53 -07:00
2020-04-16 19:00:30 +03:00
2020-04-16 19:00:30 +03:00
2020-04-23 20:21:38 -07:00
2020-05-28 16:31:48 -07:00
2020-04-26 15:42:43 +03:00
2020-04-26 15:42:43 +03:00
2020-04-26 15:42:43 +03:00
2020-06-15 21:28:50 -07:00
2020-06-15 21:28:50 -07:00
2020-04-23 14:36:30 +02:00
2020-05-04 23:47:00 +02:00
2020-05-07 00:33:25 -04:00
2020-05-05 10:08:02 +02:00
2020-05-04 23:47:00 +02:00
2020-05-04 23:47:00 +02:00
2020-05-07 00:33:25 -04:00
2020-05-03 10:31:33 +03:00
2020-05-31 20:15:32 +01:00
2020-04-23 20:21:38 -07:00
2020-04-23 20:21:38 -07:00
2020-05-05 19:19:39 -07:00
2020-06-15 08:57:20 -07:00
2020-04-23 20:21:38 -07:00
2020-06-15 08:57:20 -07:00
2020-05-20 01:16:11 +02:00
2020-05-20 01:16:11 +02:00
2020-05-20 01:16:11 +02:00
2020-05-20 01:16:11 +02:00
2020-05-20 01:16:11 +02:00
2020-05-20 01:16:11 +02:00
2020-06-11 23:16:42 +05:30
2020-04-23 20:21:38 -07:00
2020-04-22 12:12:33 -07:00
2020-05-07 00:33:25 -04:00
2020-05-23 00:16:17 +03:00
2020-04-18 16:37:08 -07:00
2020-06-14 19:20:56 +01:00
2020-06-14 19:20:56 +01:00
2020-05-09 14:40:17 +02:00
2020-05-09 14:40:17 +02:00
2020-04-12 12:14:42 +05:30
2020-04-12 12:15:07 +05:30
2020-05-02 12:50:57 -07:00
2020-05-02 12:50:57 -07:00
2020-04-21 15:06:24 -04:00
2020-04-21 15:06:24 -04:00
2020-05-26 11:16:02 +02:00
2020-05-30 20:42:20 +03:00
2020-05-01 09:05:13 -07:00
2020-05-01 09:05:13 -07:00
2020-04-29 11:57:26 -07:00
2020-04-29 11:57:26 -07:00
2020-04-29 11:57:26 -07:00
2020-05-03 19:24:41 +03:00
2020-05-03 19:24:41 +03:00
2020-05-06 14:02:55 +09:00
2020-05-08 00:39:02 +09:00
2020-06-09 08:49:05 +05:30
2020-06-09 08:49:05 +05:30
2020-05-01 16:50:10 -07:00
2020-05-01 16:50:10 -07:00
2020-06-15 08:57:20 -07:00
2020-06-15 08:57:20 -07:00
2020-06-15 08:57:20 -07:00
2020-06-11 17:40:40 -04:00
2020-05-10 15:40:17 +02:00
2020-05-10 15:40:17 +02:00
2020-05-10 15:40:17 +02:00
2020-05-10 15:40:17 +02:00
2020-04-14 20:50:26 +02:00
2020-04-26 11:50:58 -07:00
2020-06-15 08:57:20 -07:00
2020-06-13 01:18:18 +00:00
2020-05-03 19:24:41 +03:00
2020-05-27 16:28:20 -07:00
2020-05-27 16:27:15 -07:00
2020-05-20 16:08:09 +02:00
2020-06-16 23:10:41 -07:00
2020-06-10 14:09:51 -07:00
2020-04-26 11:43:43 -07:00
2020-06-15 08:57:20 -07:00
2020-06-15 08:57:20 -07:00
2020-06-15 08:57:20 -07:00
2020-05-05 19:19:39 -07:00
2020-05-05 19:19:39 -07:00
2020-05-05 19:19:39 -07:00
2020-05-05 19:19:39 -07:00
2020-06-13 13:24:19 +01:00
2020-04-17 14:08:08 +02:00
2020-04-17 14:08:08 +02:00
2020-04-26 11:50:58 -07:00
2020-05-19 14:34:30 -04:00
2020-05-19 14:34:30 -04:00
2020-06-11 17:40:40 -04:00
2020-06-15 19:46:27 -07:00
2020-06-15 19:46:27 -07:00
2020-04-22 12:12:33 -07:00
2020-06-07 14:57:57 +02:00
2020-06-07 14:57:57 +02:00
2020-06-07 14:57:57 +02:00
2020-06-07 14:57:57 +02:00
2020-04-21 21:16:50 -07:00
2020-06-01 17:47:26 -07:00
2020-06-11 18:10:13 -04:00
2020-06-12 09:43:55 +02:00
2020-05-27 16:28:20 -07:00