os-rust/compiler
Guillaume Gomez fad04ff60e
Rollup merge of #85369 - FabianWolff:issue-84973, r=jackh726
Suggest borrowing if a trait implementation is found for &/&mut <type>

This pull request fixes #84973 by suggesting to borrow if a trait is not implemented for some type `T`, but it is for `&T` or `&mut T`. For instance:
```rust
trait Ti {}
impl<T> Ti for &T {}
fn foo<T: Ti>(_: T) {}

trait Tm {}
impl<T> Tm for &mut T {}
fn bar<T: Tm>(_: T) {}

fn main() {
    let a: i32 = 5;
    foo(a);

    let b: Box<i32> = Box::new(42);
    bar(b);
}
```
gives, on current nightly:
```
error[E0277]: the trait bound `i32: Ti` is not satisfied
  --> t2.rs:11:9
   |
3  | fn foo<T: Ti>(_: T) {}
   |           -- required by this bound in `foo`
...
11 |     foo(a);
   |         ^ the trait `Ti` is not implemented for `i32`

error[E0277]: the trait bound `Box<i32>: Tm` is not satisfied
  --> t2.rs:14:9
   |
7  | fn bar<T: Tm>(_: T) {}
   |           -- required by this bound in `bar`
...
14 |     bar(b);
   |         ^ the trait `Tm` is not implemented for `Box<i32>`

error: aborting due to 2 previous errors
```
whereas with my changes, I get:
```
error[E0277]: the trait bound `i32: Ti` is not satisfied
  --> t2.rs:11:9
   |
3  | fn foo<T: Ti>(_: T) {}
   |           -- required by this bound in `foo`
...
11 |     foo(a);
   |         ^
   |         |
   |         expected an implementor of trait `Ti`
   |         help: consider borrowing here: `&a`

error[E0277]: the trait bound `Box<i32>: Tm` is not satisfied
  --> t2.rs:14:9
   |
7  | fn bar<T: Tm>(_: T) {}
   |           -- required by this bound in `bar`
...
14 |     bar(b);
   |         ^
   |         |
   |         expected an implementor of trait `Tm`
   |         help: consider borrowing mutably here: `&mut b`

error: aborting due to 2 previous errors
```
In my implementation, I have added a "blacklist" to make these suggestions flexible. In particular, suggesting to borrow can interfere with other suggestions, such as to add another trait bound to a generic argument. I have tried to configure this blacklist to cause the least amount of test case failures, i.e. to model the current behavior as closely as possible (I only had to change one existing test case, and this change was quite clearly an improvement).
2021-05-18 14:08:55 +02:00
..
rustc use jemallocator in rustc/rustdoc 2021-04-04 14:10:26 -07:00
rustc_apfloat Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_arena Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_ast Implement Anonymous{Struct, Union} in the AST 2021-05-16 09:49:16 -05:00
rustc_ast_lowering Implement Anonymous{Struct, Union} in the AST 2021-05-16 09:49:16 -05:00
rustc_ast_passes Check and deny anonymous fields on ast_validation 2021-05-16 09:51:00 -05:00
rustc_ast_pretty Implement Anonymous{Struct, Union} in the AST 2021-05-16 09:49:16 -05:00
rustc_attr Allow specifying alignment for functions 2021-04-05 17:36:51 -04:00
rustc_builtin_macros Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
rustc_codegen_cranelift Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_codegen_llvm Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_codegen_ssa Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_data_structures Only compute Obligation cache_key once in register_obligation_at 2021-05-04 11:57:53 -07:00
rustc_driver Use () for analysis. 2021-05-12 14:01:16 +02:00
rustc_error_codes swap function order for better read flow 2021-05-13 13:22:24 +02:00
rustc_errors Show macro name in 'this error originates in macro' message 2021-05-12 19:03:06 -04:00
rustc_expand Auto merge of #83813 - cbeuw:remap-std, r=michaelwoerister 2021-05-12 11:05:56 +00:00
rustc_feature Auto merge of #84571 - jedel1043:issue-49804-impl, r=petrochenkov 2021-05-17 12:15:26 +00:00
rustc_fs_util Optimize away a fs::metadata call. 2021-01-06 08:33:15 -08:00
rustc_graphviz Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_hir Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
rustc_hir_pretty Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
rustc_incremental Use () for codegen queries. 2021-05-12 13:58:46 +02:00
rustc_index Change bitwise operator to more easily keep data in vector registers 2021-05-04 11:55:18 -04:00
rustc_infer Don't suggest adding 'static lifetime to arguments 2021-05-12 21:43:09 -04:00
rustc_interface Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_lexer Fix outdated crate names in compiler docs 2021-04-08 11:12:14 -05:00
rustc_lint Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_lint_defs Add additional migrations to handle auto-traits and clone traits 2021-05-06 14:17:59 -04:00
rustc_llvm Auto merge of #85190 - mati865:update-cc, r=Mark-Simulacrum 2021-05-14 04:12:40 +00:00
rustc_macros Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_metadata Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_middle Rollup merge of #85412 - RalfJung:c_str, r=oli-obk 2021-05-17 18:52:17 +02:00
rustc_mir Address review comments 2021-05-17 21:31:05 -04:00
rustc_mir_build Check for inline assembly in THIR unsafeck 2021-05-14 09:03:30 -04:00
rustc_parse Implement Anonymous{Struct, Union} in the AST 2021-05-16 09:49:16 -05:00
rustc_parse_format Rollup merge of #83348 - osa1:issue83344, r=jackh726 2021-03-28 01:33:13 +09:00
rustc_passes Rollup merge of #85339 - FabianWolff:issue-83893, r=varkor 2021-05-18 14:08:51 +02:00
rustc_plugin_impl Use () for plugin_registrar_fn. 2021-05-12 13:58:43 +02:00
rustc_privacy Use () for privacy. 2021-05-12 13:58:45 +02:00
rustc_query_impl Use () in dependency_formats. 2021-05-12 13:58:41 +02:00
rustc_query_system Show nicer error when an 'unstable fingerprints' error occurs 2021-05-10 17:43:51 -04:00
rustc_resolve Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
rustc_save_analysis Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_serialize Auto merge of #83465 - michaelwoerister:safe-read_raw_bytes, r=cjgillot 2021-03-26 01:28:59 +00:00
rustc_session Auto merge of #83129 - LeSeulArtichaut:thir-unsafeck, r=nikomatsakis 2021-05-13 10:49:29 +00:00
rustc_span Auto merge of #84571 - jedel1043:issue-49804-impl, r=petrochenkov 2021-05-17 12:15:26 +00:00
rustc_symbol_mangling Use () for proc_macro_decls_static. 2021-05-12 13:58:43 +02:00
rustc_target Auto merge of #85312 - ehuss:macro_use-unused-attr, r=petrochenkov 2021-05-16 20:19:45 +00:00
rustc_trait_selection Rollup merge of #85369 - FabianWolff:issue-84973, r=jackh726 2021-05-18 14:08:55 +02:00
rustc_traits Auto merge of #83207 - oli-obk:valtree2, r=lcnr 2021-04-02 10:28:12 +00:00
rustc_ty_utils add new attribute rustc_insignificant_dtor and a query to check if a type has a significant drop 2021-05-14 22:57:33 -04:00
rustc_type_ir Make TypeFoldable::is_global() false when fresh tys/consts are present 2021-05-01 16:58:33 -04:00
rustc_typeck Rollup merge of #85339 - FabianWolff:issue-83893, r=varkor 2021-05-18 14:08:51 +02:00