rust/src
Ayaz Hafiz afbbd383cc
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-13 16:22:24 -07:00
..
bootstrap Rollup merge of #73267 - ehuss:cargotest-this-cargo, r=Mark-Simulacrum 2020-06-13 16:47:51 +02:00
build_helper
ci Fix emcc failure for wasm32. 2020-06-10 06:23:06 -07:00
doc Rollup merge of #73230 - Amanieu:asm-unused2, r=petrochenkov 2020-06-11 19:04:20 +02:00
etc
liballoc Rollup merge of #72906 - lzutao:migrate-numeric-assoc-consts, r=dtolnay 2020-06-12 12:28:23 +02:00
libcore Rollup merge of #73302 - JakobDegen:should-panic-documentation, r=Mark-Simulacrum 2020-06-13 16:47:54 +02:00
libpanic_abort
libpanic_unwind Bump to 1.46 2020-06-03 15:27:51 -04:00
libproc_macro Add test for proc_macro::LineColumn 2020-05-21 17:15:13 -07:00
libprofiler_builtins
librustc_apfloat Migrate to numeric associated consts 2020-06-10 01:35:47 +00:00
librustc_arena Rename some identifiers in RawVec and libarena. 2020-06-09 07:55:35 +10:00
librustc_ast Rollup merge of #73182 - Aaron1011:feature/call-fn-span, r=matthewjasper 2020-06-11 19:04:16 +02:00
librustc_ast_lowering Rollup merge of #73182 - Aaron1011:feature/call-fn-span, r=matthewjasper 2020-06-11 19:04:16 +02:00
librustc_ast_passes [AVR] Add AVR platform support 2020-06-09 17:34:07 +12:00
librustc_ast_pretty Rollup merge of #73182 - Aaron1011:feature/call-fn-span, r=matthewjasper 2020-06-11 19:04:16 +02:00
librustc_attr Make things build again 2020-06-02 20:38:24 +03:00
librustc_builtin_macros Rollup merge of #73178 - petrochenkov:explint, r=varkor 2020-06-12 12:28:25 +02:00
librustc_codegen_llvm Auto merge of #69478 - avr-rust:avr-support-upstream, r=jonas-schievink 2020-06-12 01:28:37 +00:00
librustc_codegen_ssa Rollup merge of #73033 - Amanieu:asm-tls, r=oli-obk 2020-06-12 00:05:19 +02:00
librustc_data_structures Migrate to numeric associated consts 2020-06-10 01:35:47 +00:00
librustc_driver Auto merge of #72927 - petrochenkov:rustc, r=Mark-Simulacrum 2020-06-06 09:00:51 +00:00
librustc_error_codes Rollup merge of #73236 - GuillaumeGomez:cleanup-e0666, r=Dylan-DPC 2020-06-12 12:28:29 +02:00
librustc_errors Rollup merge of #73169 - Amanieu:asm-warnings, r=petrochenkov 2020-06-11 19:04:14 +02:00
librustc_expand Rollup merge of #73178 - petrochenkov:explint, r=varkor 2020-06-12 12:28:25 +02:00
librustc_feature [AVR] Raise and link to a tracking issue for the avr-interrupt calling convention 2020-06-09 17:35:50 +12:00
librustc_fs_util
librustc_graphviz Rename the crates in source code 2020-06-02 20:42:54 +03:00
librustc_hir Rollup merge of #73178 - petrochenkov:explint, r=varkor 2020-06-12 12:28:25 +02:00
librustc_hir_pretty Rollup merge of #73182 - Aaron1011:feature/call-fn-span, r=matthewjasper 2020-06-11 19:04:16 +02:00
librustc_incremental Rename the crates in source code 2020-06-02 20:42:54 +03:00
librustc_index Make things build again 2020-06-02 20:38:24 +03:00
librustc_infer Rollup merge of #73182 - Aaron1011:feature/call-fn-span, r=matthewjasper 2020-06-11 19:04:16 +02:00
librustc_interface Rollup merge of #73178 - petrochenkov:explint, r=varkor 2020-06-12 12:28:25 +02:00
librustc_lexer Rollup merge of #72906 - lzutao:migrate-numeric-assoc-consts, r=dtolnay 2020-06-12 12:28:23 +02:00
librustc_lint Rollup merge of #72906 - lzutao:migrate-numeric-assoc-consts, r=dtolnay 2020-06-12 12:28:23 +02:00
librustc_llvm [AVR] Add AVR platform support 2020-06-09 17:34:07 +12:00
librustc_macros Add descriptions for all queries 2020-05-31 20:15:32 +01:00
librustc_metadata Migrate to numeric associated consts 2020-06-10 01:35:47 +00:00
librustc_middle Rollup merge of #73194 - lzutao:INT-patterns, r=dtolnay 2020-06-13 16:47:47 +02:00
librustc_mir Rollup merge of #73066 - ecstatic-morse:query-structural-eq2, r=pnkfelix 2020-06-13 16:47:45 +02:00
librustc_mir_build Rollup merge of #73066 - ecstatic-morse:query-structural-eq2, r=pnkfelix 2020-06-13 16:47:45 +02:00
librustc_parse Rollup merge of #73182 - Aaron1011:feature/call-fn-span, r=matthewjasper 2020-06-11 19:04:16 +02:00
librustc_parse_format Make things build again 2020-06-02 20:38:24 +03:00
librustc_passes Rollup merge of #73182 - Aaron1011:feature/call-fn-span, r=matthewjasper 2020-06-11 19:04:16 +02:00
librustc_plugin_impl Remove ast::{Ident, Name} reexports. 2020-05-08 13:13:15 +02:00
librustc_privacy Track span of function in method calls, and use this in #[track_caller] 2020-06-10 17:30:11 -04:00
librustc_query_system Rename the crates in source code 2020-06-02 20:42:54 +03:00
librustc_resolve Rollup merge of #73178 - petrochenkov:explint, r=varkor 2020-06-12 12:28:25 +02:00
librustc_save_analysis Rollup merge of #72906 - lzutao:migrate-numeric-assoc-consts, r=dtolnay 2020-06-12 12:28:23 +02:00
librustc_serialize Update fulldeps tests and clippy 2020-06-03 00:18:57 +03:00
librustc_session Rollup merge of #73178 - petrochenkov:explint, r=varkor 2020-06-12 12:28:25 +02:00
librustc_span Rollup merge of #72906 - lzutao:migrate-numeric-assoc-consts, r=dtolnay 2020-06-12 12:28:23 +02:00
librustc_symbol_mangling iterate List by value 2020-05-23 12:24:19 +02:00
librustc_target Rollup merge of #73241 - mati865:mingw-libs-order-comment, r=petrochenkov 2020-06-13 16:47:49 +02:00
librustc_trait_selection Make type_marked_structural private 2020-06-12 08:55:52 -07:00
librustc_traits change WellFormed predicate to GenericArg 2020-06-02 15:02:36 +02:00
librustc_ty Auto merge of #72114 - anyska:vtable-rename, r=nikomatsakis 2020-06-09 09:14:49 +00:00
librustc_typeck Note numeric literals that can never fit in an expected type 2020-06-13 16:22:24 -07:00
librustdoc Rollup merge of #72180 - euclio:rustdoc-test-extra-space, r=Dylan-DPC 2020-06-11 19:04:08 +02:00
libstd Migrate to numeric associated consts 2020-06-10 01:35:47 +00:00
libterm
libtest typo: awailable -> available 2020-06-08 13:37:58 -07:00
libunwind Rollup merge of #73187 - mati865:bootstrap-cleanup, r=Mark-Simulacrum 2020-06-12 00:05:31 +02:00
llvm-project@02e0d7fa55 Update LLVM submodule to include lld NOLOAD fix 2020-06-05 11:36:45 -07:00
rtstartup
rustc
rustllvm Auto merge of #69478 - avr-rust:avr-support-upstream, r=jonas-schievink 2020-06-12 01:28:37 +00:00
stdarch@45340c0e2f Update stdarch submodule to latest head 2020-05-31 00:12:59 +02:00
test Note numeric literals that can never fit in an expected type 2020-06-13 16:22:24 -07:00
tools Rollup merge of #72906 - lzutao:migrate-numeric-assoc-consts, r=dtolnay 2020-06-12 12:28:23 +02:00
README.md
stage0.txt Bump to 1.46 2020-06-03 15:27:51 -04:00

This directory contains the source code of the rust project, including:

  • rustc and its tests
  • libstd
  • Various submodules for tools, like rustdoc, rls, etc.

For more information on how various parts of the compiler work, see the rustc dev guide.