rust/compiler
Dylan DPC 12ea0f6112
Rollup merge of #82364 - osa1:issue82361, r=estebank
Improve error msgs when found type is deref of expected

This improves help messages in two cases:

- When expected type is `T` and found type is `&T`, we now look through blocks
  and suggest dereferencing the expression of the block, rather than the whole
  block.

- In the above case, if the expression is an `&`, we not suggest removing the
  `&` instead of adding `*`.

Both of these are demonstrated in the regression test. Before this patch the
first error in the test would be:

    error[E0308]: `if` and `else` have incompatible types
     --> test.rs:8:9
      |
    5 | /     if true {
    6 | |         a
      | |         - expected because of this
    7 | |     } else {
    8 | |         b
      | |         ^ expected `usize`, found `&usize`
    9 | |     };
      | |_____- `if` and `else` have incompatible types
      |
    help: consider dereferencing the borrow
      |
    7 |     } else *{
    8 |         b
    9 |     };
      |

Now:

    error[E0308]: `if` and `else` have incompatible types
     --> test.rs:8:9
      |
    5 | /     if true {
    6 | |         a
      | |         - expected because of this
    7 | |     } else {
    8 | |         b
      | |         ^
      | |         |
      | |         expected `usize`, found `&usize`
      | |         help: consider dereferencing the borrow: `*b`
    9 | |     };
      | |_____- `if` and `else` have incompatible types

The second error:

    error[E0308]: `if` and `else` have incompatible types
      --> test.rs:14:9
       |
    11 | /     if true {
    12 | |         1
       | |         - expected because of this
    13 | |     } else {
    14 | |         &1
       | |         ^^ expected integer, found `&{integer}`
    15 | |     };
       | |_____- `if` and `else` have incompatible types
       |
    help: consider dereferencing the borrow
       |
    13 |     } else *{
    14 |         &1
    15 |     };
       |

now:

    error[E0308]: `if` and `else` have incompatible types
      --> test.rs:14:9
       |
    11 | /     if true {
    12 | |         1
       | |         - expected because of this
    13 | |     } else {
    14 | |         &1
       | |         ^-
       | |         ||
       | |         |help: consider removing the `&`: `1`
       | |         expected integer, found `&{integer}`
    15 | |     };
       | |_____- `if` and `else` have incompatible types

Fixes #82361

---

r? ````@estebank````
2021-02-25 14:34:04 +01:00
..
rustc just max_level_info 2020-09-11 09:37:51 -07:00
rustc_apfloat bumped smallvec deps 2021-02-14 18:03:11 +03:00
rustc_arena Update the bootstrap compiler 2021-02-20 17:19:30 -05:00
rustc_ast Rollup merge of #82321 - bugadani:ast3, r=varkor 2021-02-25 14:34:03 +01:00
rustc_ast_lowering Rollup merge of #82308 - estebank:issue-82290, r=lcnr 2021-02-23 16:10:27 +01:00
rustc_ast_passes Rollup merge of #82296 - spastorino:pubrules, r=nikomatsakis 2021-02-23 16:10:23 +01:00
rustc_ast_pretty Rollup merge of #82238 - petrochenkov:nocratemod, r=Aaron1011 2021-02-19 02:49:08 +01:00
rustc_attr remove redundant wrapping of return types of allow_internal_unstable() and rustc_allow_const_fn_unstable() 2021-02-21 18:11:27 +01:00
rustc_builtin_macros Remove some P-s 2021-02-20 10:51:26 +01:00
rustc_codegen_cranelift Use a QueryContext for try_mark_green. 2021-02-19 17:51:56 +01:00
rustc_codegen_llvm Rollup merge of #82214 - est31:no_to_string, r=oli-obk 2021-02-25 14:33:59 +01:00
rustc_codegen_ssa Rollup merge of #82214 - est31:no_to_string, r=oli-obk 2021-02-25 14:33:59 +01:00
rustc_data_structures Update the bootstrap compiler 2021-02-20 17:19:30 -05:00
rustc_driver Auto merge of #82341 - GuillaumeGomez:rollup-t7y7tyg, r=GuillaumeGomez 2021-02-20 21:38:53 +00:00
rustc_error_codes Rollup merge of #82246 - jesusprubio:add-long-explanation-e0549, r=GuillaumeGomez 2021-02-18 16:57:41 +01:00
rustc_errors Rollup merge of #82087 - estebank:abolish-ice, r=oli-obk 2021-02-25 14:33:56 +01:00
rustc_expand remove redundant wrapping of return types of allow_internal_unstable() and rustc_allow_const_fn_unstable() 2021-02-21 18:11:27 +01:00
rustc_feature Update pub_macro_rules since version 2021-02-19 13:53:02 -03:00
rustc_fs_util Optimize away a `fs::metadata` call. 2021-01-06 08:33:15 -08:00
rustc_graphviz remove redundant closures (clippy::redundant_closure) 2021-01-03 13:34:24 +01:00
rustc_hir Improve error msgs when found type is deref of expected 2021-02-23 10:50:06 +03:00
rustc_hir_pretty Auto merge of #81611 - cjgillot:meowner, r=estebank 2021-02-16 22:14:32 +00:00
rustc_incremental Print -Ztime-passes (and misc stats/logs) on stderr, not stdout. 2021-02-18 14:13:38 +02:00
rustc_index Auto merge of #81498 - thomaseizinger:ice-workaround-56935-rustc-index, r=matthewjasper 2021-02-07 08:09:58 +00:00
rustc_infer Rollup merge of #81496 - guswynn:expected_async_block, r=oli-obk 2021-02-19 02:49:00 +01:00
rustc_interface Rollup merge of #82255 - nhwn:nonzero-err-as-bug, r=davidtwco 2021-02-23 02:51:55 +01:00
rustc_lexer Return EOF_CHAR constant instead of magic char. 2021-01-07 13:20:04 +01:00
rustc_lint Rollup merge of #82113 - m-ou-se:panic-format-lint, r=estebank 2021-02-23 16:10:21 +01:00
rustc_lint_defs Add explanations and suggestions to `irrefutable_let_patterns` lint 2021-02-18 16:21:16 -08:00
rustc_llvm HWASan support 2021-02-07 23:48:58 -08:00
rustc_macros Move the query system to rustc_query_impl. 2021-02-19 17:51:58 +01:00
rustc_metadata Print -Ztime-passes (and misc stats/logs) on stderr, not stdout. 2021-02-18 14:13:38 +02:00
rustc_middle Rollup merge of #81713 - estebank:unstable-assoc-item-lint, r=oli-obk 2021-02-25 14:33:53 +01:00
rustc_mir Auto merge of #82338 - RalfJung:interp-error-allocs, r=oli-obk 2021-02-25 08:27:09 +00:00
rustc_mir_build Auto merge of #81978 - tmiasko:head-ctor, r=Mark-Simulacrum 2021-02-22 21:45:50 +00:00
rustc_parse Rollup merge of #82321 - bugadani:ast3, r=varkor 2021-02-25 14:34:03 +01:00
rustc_parse_format parse_format: treat r" as a literal 2021-02-06 15:01:07 +00:00
rustc_passes Rollup merge of #82297 - tmiasko:write-only, r=oli-obk 2021-02-23 16:10:25 +01:00
rustc_plugin_impl Only store a LocalDefId in hir::Item. 2021-02-15 19:32:10 +01:00
rustc_privacy Rollup merge of #82066 - matthewjasper:trait-ref-fix, r=jackh726 2021-02-18 16:57:34 +01:00
rustc_query_impl Simplify hashing. 2021-02-21 12:22:22 +01:00
rustc_query_system Simplify hashing. 2021-02-21 12:22:22 +01:00
rustc_resolve Rollup merge of #82087 - estebank:abolish-ice, r=oli-obk 2021-02-25 14:33:56 +01:00
rustc_save_analysis Only store a LocalDefId in hir::ForeignItem. 2021-02-15 19:32:29 +01:00
rustc_serialize Update the bootstrap compiler 2021-02-20 17:19:30 -05:00
rustc_session Auto merge of #82102 - nagisa:nagisa/fix-dwo-name, r=davidtwco 2021-02-23 10:02:16 +00:00
rustc_span Rollup merge of #82296 - spastorino:pubrules, r=nikomatsakis 2021-02-23 16:10:23 +01:00
rustc_symbol_mangling Use less HirId when referring to items. 2021-02-15 19:36:12 +01:00
rustc_target Rollup merge of #82166 - kaniini:s390x-musl-target, r=nagisa 2021-02-23 02:51:53 +01:00
rustc_trait_selection Auto merge of #82020 - jyn514:mut-passes, r=camelid,GuillaumeGomez 2021-02-19 16:39:03 +00:00
rustc_traits Rollup merge of #82066 - matthewjasper:trait-ref-fix, r=jackh726 2021-02-18 16:57:34 +01:00
rustc_ty_utils Auto merge of #82159 - BoxyUwU:uwu, r=varkor 2021-02-24 21:54:52 +00:00
rustc_type_ir New pass to deduplicate blocks 2021-02-21 21:51:54 +01:00
rustc_typeck Rollup merge of #82364 - osa1:issue82361, r=estebank 2021-02-25 14:34:04 +01:00