From a9051d861ca13a1c16da3cd4248b26d86cf5e1d5 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 18 Apr 2023 19:44:27 +0000 Subject: [PATCH] Tweak borrow suggestion --- compiler/rustc_hir_typeck/src/demand.rs | 81 ++++++------ .../argument-suggestions/issue-97484.stderr | 2 +- .../ui/async-await/issues/issue-102206.stderr | 10 +- tests/ui/coercion/coercion-slice.stderr | 11 +- tests/ui/inference/deref-suggestion.stderr | 20 +-- tests/ui/issues/issue-11374.stderr | 10 +- tests/ui/issues/issue-17033.stderr | 11 +- tests/ui/issues/issue-18819.stderr | 2 +- tests/ui/issues/issue-46302.stderr | 10 +- tests/ui/issues/issue-61106.stderr | 10 +- tests/ui/methods/method-self-arg-1.stderr | 10 +- .../dont-point-return-on-E0308.stderr | 10 +- tests/ui/mut/mut-cross-borrowing.stderr | 10 +- tests/ui/range/issue-54505-no-literals.fixed | 24 ++-- tests/ui/range/issue-54505-no-literals.rs | 24 ++-- tests/ui/range/issue-54505-no-literals.stderr | 120 +++++++++++------- tests/ui/span/coerce-suggestions.stderr | 11 +- tests/ui/span/issue-39018.stderr | 10 +- tests/ui/str/str-array-assignment.stderr | 21 +-- tests/ui/suggestions/suggest-ref-macro.rs | 4 +- tests/ui/suggestions/suggest-ref-macro.stderr | 19 +-- tests/ui/type/type-mismatch.stderr | 20 +-- .../ui/typeck/bad-index-due-to-nested.stderr | 18 +-- .../ui/typeck/bad-type-in-vec-contains.stderr | 10 +- tests/ui/typeck/issue-13853.stderr | 10 +- tests/ui/unsized-locals/suggest-borrow.stderr | 11 +- 26 files changed, 288 insertions(+), 211 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index ee81ea345a6..b8de22272dd 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -1334,52 +1334,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { )); } - if let Ok(src) = sm.span_to_snippet(sugg_sp) { - let needs_parens = match expr.kind { - // parenthesize if needed (Issue #46756) - hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true, - // parenthesize borrows of range literals (Issue #54505) - _ if is_range_literal(expr) => true, - _ => false, - }; + let needs_parens = match expr.kind { + // parenthesize if needed (Issue #46756) + hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true, + // parenthesize borrows of range literals (Issue #54505) + _ if is_range_literal(expr) => true, + _ => false, + }; - if let Some(sugg) = self.can_use_as_ref(expr) { - return Some(( - sugg.0, - sugg.1.to_string(), - sugg.2, - Applicability::MachineApplicable, - false, - false, - )); - } - - let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) { - Some(ident) => format!("{ident}: "), - None => String::new(), - }; - - if let Some(hir::Node::Expr(hir::Expr { - kind: hir::ExprKind::Assign(..), - .. - })) = self.tcx.hir().find_parent(expr.hir_id) - { - if mutability.is_mut() { - // Suppressing this diagnostic, we'll properly print it in `check_expr_assign` - return None; - } - } - - let sugg_expr = if needs_parens { format!("({src})") } else { src }; + if let Some(sugg) = self.can_use_as_ref(expr) { return Some(( - sp, - format!("consider {}borrowing here", mutability.mutably_str()), - format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()), + sugg.0, + sugg.1.to_string(), + sugg.2, Applicability::MachineApplicable, false, false, )); } + + let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) { + Some(ident) => format!("{ident}: "), + None => String::new(), + }; + + if let Some(hir::Node::Expr(hir::Expr { + kind: hir::ExprKind::Assign(..), + .. + })) = self.tcx.hir().find_parent(expr.hir_id) + { + if mutability.is_mut() { + // Suppressing this diagnostic, we'll properly print it in `check_expr_assign` + return None; + } + } + + let (sp, sugg_expr, verbose) = if needs_parens { + let src = sm.span_to_snippet(sugg_sp).ok()?; + (sp, format!("({src})"), false) + } else { + (sp.shrink_to_lo(), "".to_string(), true) + }; + return Some(( + sp, + format!("consider {}borrowing here", mutability.mutably_str()), + format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()), + Applicability::MachineApplicable, + verbose, + false, + )); } } ( diff --git a/tests/ui/argument-suggestions/issue-97484.stderr b/tests/ui/argument-suggestions/issue-97484.stderr index a86cbbf1802..082564fbc7f 100644 --- a/tests/ui/argument-suggestions/issue-97484.stderr +++ b/tests/ui/argument-suggestions/issue-97484.stderr @@ -16,7 +16,7 @@ LL | fn foo(a: &A, d: D, e: &E, g: G) {} help: consider borrowing here | LL | foo(&&A, B, C, D, &E, F, G); - | ~~ + | + help: remove the extra arguments | LL - foo(&&A, B, C, D, E, F, G); diff --git a/tests/ui/async-await/issues/issue-102206.stderr b/tests/ui/async-await/issues/issue-102206.stderr index 750b7a886ef..cd845056805 100644 --- a/tests/ui/async-await/issues/issue-102206.stderr +++ b/tests/ui/async-await/issues/issue-102206.stderr @@ -2,14 +2,16 @@ error[E0308]: mismatched types --> $DIR/issue-102206.rs:6:27 | LL | std::mem::size_of_val(foo()); - | --------------------- ^^^^^ - | | | - | | expected `&_`, found future - | | help: consider borrowing here: `&foo()` + | --------------------- ^^^^^ expected `&_`, found future + | | | arguments to this function are incorrect | note: function defined here --> $SRC_DIR/core/src/mem/mod.rs:LL:COL +help: consider borrowing here + | +LL | std::mem::size_of_val(&foo()); + | + error: aborting due to previous error diff --git a/tests/ui/coercion/coercion-slice.stderr b/tests/ui/coercion/coercion-slice.stderr index c7b856a57eb..17bbca7a0bd 100644 --- a/tests/ui/coercion/coercion-slice.stderr +++ b/tests/ui/coercion/coercion-slice.stderr @@ -2,11 +2,14 @@ error[E0308]: mismatched types --> $DIR/coercion-slice.rs:4:21 | LL | let _: &[i32] = [0]; - | ------ ^^^ - | | | - | | expected `&[i32]`, found `[{integer}; 1]` - | | help: consider borrowing here: `&[0]` + | ------ ^^^ expected `&[i32]`, found `[{integer}; 1]` + | | | expected due to this + | +help: consider borrowing here + | +LL | let _: &[i32] = &[0]; + | + error: aborting due to previous error diff --git a/tests/ui/inference/deref-suggestion.stderr b/tests/ui/inference/deref-suggestion.stderr index 6f5aacacfc1..c58aab42269 100644 --- a/tests/ui/inference/deref-suggestion.stderr +++ b/tests/ui/inference/deref-suggestion.stderr @@ -98,19 +98,23 @@ error[E0308]: mismatched types --> $DIR/deref-suggestion.rs:40:17 | LL | let s = S { u }; - | ^ - | | - | expected `&u32`, found integer - | help: consider borrowing here: `u: &u` + | ^ expected `&u32`, found integer + | +help: consider borrowing here + | +LL | let s = S { u: &u }; + | ++++ error[E0308]: mismatched types --> $DIR/deref-suggestion.rs:42:20 | LL | let s = S { u: u }; - | ^ - | | - | expected `&u32`, found integer - | help: consider borrowing here: `&u` + | ^ expected `&u32`, found integer + | +help: consider borrowing here + | +LL | let s = S { u: &u }; + | + error[E0308]: mismatched types --> $DIR/deref-suggestion.rs:45:17 diff --git a/tests/ui/issues/issue-11374.stderr b/tests/ui/issues/issue-11374.stderr index 6e1fb1540bb..879dc5b76c5 100644 --- a/tests/ui/issues/issue-11374.stderr +++ b/tests/ui/issues/issue-11374.stderr @@ -2,10 +2,8 @@ error[E0308]: mismatched types --> $DIR/issue-11374.rs:26:15 | LL | c.read_to(v); - | ------- ^ - | | | - | | expected `&mut [u8]`, found `Vec<_>` - | | help: consider mutably borrowing here: `&mut v` + | ------- ^ expected `&mut [u8]`, found `Vec<_>` + | | | arguments to this method are incorrect | = note: expected mutable reference `&mut [u8]` @@ -15,6 +13,10 @@ note: method defined here | LL | pub fn read_to(&mut self, vec: &mut [u8]) { | ^^^^^^^ -------------- +help: consider mutably borrowing here + | +LL | c.read_to(&mut v); + | ++++ error: aborting due to previous error diff --git a/tests/ui/issues/issue-17033.stderr b/tests/ui/issues/issue-17033.stderr index f26bee5ff45..3419c079859 100644 --- a/tests/ui/issues/issue-17033.stderr +++ b/tests/ui/issues/issue-17033.stderr @@ -2,11 +2,14 @@ error[E0308]: mismatched types --> $DIR/issue-17033.rs:2:10 | LL | (*p)(()) - | ---- ^^ - | | | - | | expected `&mut ()`, found `()` - | | help: consider mutably borrowing here: `&mut ()` + | ---- ^^ expected `&mut ()`, found `()` + | | | arguments to this function are incorrect + | +help: consider mutably borrowing here + | +LL | (*p)(&mut ()) + | ++++ error: aborting due to previous error diff --git a/tests/ui/issues/issue-18819.stderr b/tests/ui/issues/issue-18819.stderr index 1fc974b609c..40098f9622f 100644 --- a/tests/ui/issues/issue-18819.stderr +++ b/tests/ui/issues/issue-18819.stderr @@ -19,7 +19,7 @@ LL | fn print_x(_: &dyn Foo, extra: &str) { help: consider borrowing here | LL | print_x(&X); - | ~~ + | + help: provide the argument | LL | print_x(/* &dyn Foo */, /* &str */); diff --git a/tests/ui/issues/issue-46302.stderr b/tests/ui/issues/issue-46302.stderr index a6f97c3c9af..6e126038cc9 100644 --- a/tests/ui/issues/issue-46302.stderr +++ b/tests/ui/issues/issue-46302.stderr @@ -2,10 +2,12 @@ error[E0308]: mismatched types --> $DIR/issue-46302.rs:3:27 | LL | let u: &str = if true { s[..2] } else { s }; - | ^^^^^^ - | | - | expected `&str`, found `str` - | help: consider borrowing here: `&s[..2]` + | ^^^^^^ expected `&str`, found `str` + | +help: consider borrowing here + | +LL | let u: &str = if true { &s[..2] } else { s }; + | + error: aborting due to previous error diff --git a/tests/ui/issues/issue-61106.stderr b/tests/ui/issues/issue-61106.stderr index eff3e6e7849..aa922e2682d 100644 --- a/tests/ui/issues/issue-61106.stderr +++ b/tests/ui/issues/issue-61106.stderr @@ -2,10 +2,8 @@ error[E0308]: mismatched types --> $DIR/issue-61106.rs:3:9 | LL | foo(x.clone()); - | --- ^^^^^^^^^ - | | | - | | expected `&str`, found `String` - | | help: consider borrowing here: `&x` + | --- ^^^^^^^^^ expected `&str`, found `String` + | | | arguments to this function are incorrect | note: function defined here @@ -13,6 +11,10 @@ note: function defined here | LL | fn foo(_: &str) {} | ^^^ ------- +help: consider borrowing here + | +LL | foo(&x.clone()); + | + error: aborting due to previous error diff --git a/tests/ui/methods/method-self-arg-1.stderr b/tests/ui/methods/method-self-arg-1.stderr index 9241a8be58f..dcc21acc5c0 100644 --- a/tests/ui/methods/method-self-arg-1.stderr +++ b/tests/ui/methods/method-self-arg-1.stderr @@ -2,10 +2,8 @@ error[E0308]: mismatched types --> $DIR/method-self-arg-1.rs:11:14 | LL | Foo::bar(x); - | -------- ^ - | | | - | | expected `&Foo`, found `Foo` - | | help: consider borrowing here: `&x` + | -------- ^ expected `&Foo`, found `Foo` + | | | arguments to this function are incorrect | note: method defined here @@ -13,6 +11,10 @@ note: method defined here | LL | fn bar(&self) {} | ^^^ ----- +help: consider borrowing here + | +LL | Foo::bar(&x); + | + error[E0308]: mismatched types --> $DIR/method-self-arg-1.rs:13:14 diff --git a/tests/ui/mismatched_types/dont-point-return-on-E0308.stderr b/tests/ui/mismatched_types/dont-point-return-on-E0308.stderr index 13942682d28..7be94ef4ad6 100644 --- a/tests/ui/mismatched_types/dont-point-return-on-E0308.stderr +++ b/tests/ui/mismatched_types/dont-point-return-on-E0308.stderr @@ -2,10 +2,8 @@ error[E0308]: mismatched types --> $DIR/dont-point-return-on-E0308.rs:11:11 | LL | f(()); - | - ^^ - | | | - | | expected `&()`, found `()` - | | help: consider borrowing here: `&()` + | - ^^ expected `&()`, found `()` + | | | arguments to this function are incorrect | note: function defined here @@ -13,6 +11,10 @@ note: function defined here | LL | async fn f(_: &()) {} | ^ ------ +help: consider borrowing here + | +LL | f(&()); + | + error: aborting due to previous error diff --git a/tests/ui/mut/mut-cross-borrowing.stderr b/tests/ui/mut/mut-cross-borrowing.stderr index 8401827e51f..8a3076db9b2 100644 --- a/tests/ui/mut/mut-cross-borrowing.stderr +++ b/tests/ui/mut/mut-cross-borrowing.stderr @@ -2,10 +2,8 @@ error[E0308]: mismatched types --> $DIR/mut-cross-borrowing.rs:7:7 | LL | f(x) - | - ^ - | | | - | | expected `&mut isize`, found `Box<{integer}>` - | | help: consider mutably borrowing here: `&mut x` + | - ^ expected `&mut isize`, found `Box<{integer}>` + | | | arguments to this function are incorrect | = note: expected mutable reference `&mut isize` @@ -15,6 +13,10 @@ note: function defined here | LL | fn f(_: &mut isize) {} | ^ ------------- +help: consider mutably borrowing here + | +LL | f(&mut x) + | ++++ error: aborting due to previous error diff --git a/tests/ui/range/issue-54505-no-literals.fixed b/tests/ui/range/issue-54505-no-literals.fixed index 4d8f67182b9..71c36c741cc 100644 --- a/tests/ui/range/issue-54505-no-literals.fixed +++ b/tests/ui/range/issue-54505-no-literals.fixed @@ -16,60 +16,60 @@ fn main() { take_range(&std::ops::Range { start: 0, end: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::Range { start: 0, end: 1 } + //~| SUGGESTION & take_range(&::std::ops::Range { start: 0, end: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::Range { start: 0, end: 1 } + //~| SUGGESTION & take_range(&std::ops::RangeFrom { start: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeFrom { start: 1 } + //~| SUGGESTION & take_range(&::std::ops::RangeFrom { start: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeFrom { start: 1 } + //~| SUGGESTION & take_range(&std::ops::RangeFull {}); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeFull {} + //~| SUGGESTION & take_range(&::std::ops::RangeFull {}); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeFull {} + //~| SUGGESTION & take_range(&std::ops::RangeInclusive::new(0, 1)); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeInclusive::new(0, 1) + //~| SUGGESTION & take_range(&::std::ops::RangeInclusive::new(0, 1)); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeInclusive::new(0, 1) + //~| SUGGESTION & take_range(&std::ops::RangeTo { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeTo { end: 5 } + //~| SUGGESTION & take_range(&::std::ops::RangeTo { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeTo { end: 5 } + //~| SUGGESTION & take_range(&std::ops::RangeToInclusive { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeToInclusive { end: 5 } + //~| SUGGESTION & take_range(&::std::ops::RangeToInclusive { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeToInclusive { end: 5 } + //~| SUGGESTION & } diff --git a/tests/ui/range/issue-54505-no-literals.rs b/tests/ui/range/issue-54505-no-literals.rs index dc21dcbc2db..db125d1a22b 100644 --- a/tests/ui/range/issue-54505-no-literals.rs +++ b/tests/ui/range/issue-54505-no-literals.rs @@ -16,60 +16,60 @@ fn main() { take_range(std::ops::Range { start: 0, end: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::Range { start: 0, end: 1 } + //~| SUGGESTION & take_range(::std::ops::Range { start: 0, end: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::Range { start: 0, end: 1 } + //~| SUGGESTION & take_range(std::ops::RangeFrom { start: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeFrom { start: 1 } + //~| SUGGESTION & take_range(::std::ops::RangeFrom { start: 1 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeFrom { start: 1 } + //~| SUGGESTION & take_range(std::ops::RangeFull {}); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeFull {} + //~| SUGGESTION & take_range(::std::ops::RangeFull {}); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeFull {} + //~| SUGGESTION & take_range(std::ops::RangeInclusive::new(0, 1)); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeInclusive::new(0, 1) + //~| SUGGESTION & take_range(::std::ops::RangeInclusive::new(0, 1)); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeInclusive::new(0, 1) + //~| SUGGESTION & take_range(std::ops::RangeTo { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeTo { end: 5 } + //~| SUGGESTION & take_range(::std::ops::RangeTo { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeTo { end: 5 } + //~| SUGGESTION & take_range(std::ops::RangeToInclusive { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &std::ops::RangeToInclusive { end: 5 } + //~| SUGGESTION & take_range(::std::ops::RangeToInclusive { end: 5 }); //~^ ERROR mismatched types [E0308] //~| HELP consider borrowing here - //~| SUGGESTION &::std::ops::RangeToInclusive { end: 5 } + //~| SUGGESTION & } diff --git a/tests/ui/range/issue-54505-no-literals.stderr b/tests/ui/range/issue-54505-no-literals.stderr index d112983848d..5894bb6ba55 100644 --- a/tests/ui/range/issue-54505-no-literals.stderr +++ b/tests/ui/range/issue-54505-no-literals.stderr @@ -2,10 +2,8 @@ error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:16:16 | LL | take_range(std::ops::Range { start: 0, end: 1 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `Range<{integer}>` - | | help: consider borrowing here: `&std::ops::Range { start: 0, end: 1 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -15,15 +13,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&std::ops::Range { start: 0, end: 1 }); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:21:16 | LL | take_range(::std::ops::Range { start: 0, end: 1 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `Range<{integer}>` - | | help: consider borrowing here: `&::std::ops::Range { start: 0, end: 1 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -33,15 +33,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&::std::ops::Range { start: 0, end: 1 }); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:26:16 | LL | take_range(std::ops::RangeFrom { start: 1 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeFrom<{integer}>` - | | help: consider borrowing here: `&std::ops::RangeFrom { start: 1 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFrom<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -51,15 +53,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&std::ops::RangeFrom { start: 1 }); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:31:16 | LL | take_range(::std::ops::RangeFrom { start: 1 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeFrom<{integer}>` - | | help: consider borrowing here: `&::std::ops::RangeFrom { start: 1 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFrom<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -69,15 +73,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&::std::ops::RangeFrom { start: 1 }); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:36:16 | LL | take_range(std::ops::RangeFull {}); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeFull` - | | help: consider borrowing here: `&std::ops::RangeFull {}` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFull` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -87,15 +93,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&std::ops::RangeFull {}); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:41:16 | LL | take_range(::std::ops::RangeFull {}); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeFull` - | | help: consider borrowing here: `&::std::ops::RangeFull {}` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFull` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -105,15 +113,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&::std::ops::RangeFull {}); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:46:16 | LL | take_range(std::ops::RangeInclusive::new(0, 1)); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeInclusive<{integer}>` - | | help: consider borrowing here: `&std::ops::RangeInclusive::new(0, 1)` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -123,15 +133,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&std::ops::RangeInclusive::new(0, 1)); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:51:16 | LL | take_range(::std::ops::RangeInclusive::new(0, 1)); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeInclusive<{integer}>` - | | help: consider borrowing here: `&::std::ops::RangeInclusive::new(0, 1)` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -141,15 +153,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&::std::ops::RangeInclusive::new(0, 1)); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:56:16 | LL | take_range(std::ops::RangeTo { end: 5 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeTo<{integer}>` - | | help: consider borrowing here: `&std::ops::RangeTo { end: 5 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeTo<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -159,15 +173,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&std::ops::RangeTo { end: 5 }); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:61:16 | LL | take_range(::std::ops::RangeTo { end: 5 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeTo<{integer}>` - | | help: consider borrowing here: `&::std::ops::RangeTo { end: 5 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeTo<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -177,15 +193,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&::std::ops::RangeTo { end: 5 }); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:66:16 | LL | take_range(std::ops::RangeToInclusive { end: 5 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeToInclusive<{integer}>` - | | help: consider borrowing here: `&std::ops::RangeToInclusive { end: 5 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeToInclusive<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -195,15 +213,17 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&std::ops::RangeToInclusive { end: 5 }); + | + error[E0308]: mismatched types --> $DIR/issue-54505-no-literals.rs:71:16 | LL | take_range(::std::ops::RangeToInclusive { end: 5 }); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | | - | | expected `&_`, found `RangeToInclusive<{integer}>` - | | help: consider borrowing here: `&::std::ops::RangeToInclusive { end: 5 }` + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeToInclusive<{integer}>` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -213,6 +233,10 @@ note: function defined here | LL | fn take_range(_r: &impl RangeBounds) {} | ^^^^^^^^^^ ------------------------- +help: consider borrowing here + | +LL | take_range(&::std::ops::RangeToInclusive { end: 5 }); + | + error: aborting due to 12 previous errors diff --git a/tests/ui/span/coerce-suggestions.stderr b/tests/ui/span/coerce-suggestions.stderr index bb30f000ea7..ff840b781f0 100644 --- a/tests/ui/span/coerce-suggestions.stderr +++ b/tests/ui/span/coerce-suggestions.stderr @@ -10,11 +10,14 @@ error[E0308]: mismatched types --> $DIR/coerce-suggestions.rs:9:19 | LL | let x: &str = String::new(); - | ---- ^^^^^^^^^^^^^ - | | | - | | expected `&str`, found `String` - | | help: consider borrowing here: `&String::new()` + | ---- ^^^^^^^^^^^^^ expected `&str`, found `String` + | | | expected due to this + | +help: consider borrowing here + | +LL | let x: &str = &String::new(); + | + error[E0308]: mismatched types --> $DIR/coerce-suggestions.rs:12:10 diff --git a/tests/ui/span/issue-39018.stderr b/tests/ui/span/issue-39018.stderr index bae93639271..c8c4a513988 100644 --- a/tests/ui/span/issue-39018.stderr +++ b/tests/ui/span/issue-39018.stderr @@ -78,10 +78,12 @@ error[E0308]: mismatched types --> $DIR/issue-39018.rs:29:17 | LL | let _ = a + b; - | ^ - | | - | expected `&str`, found `String` - | help: consider borrowing here: `&b` + | ^ expected `&str`, found `String` + | +help: consider borrowing here + | +LL | let _ = a + &b; + | + error[E0369]: cannot add `String` to `&String` --> $DIR/issue-39018.rs:30:15 diff --git a/tests/ui/str/str-array-assignment.stderr b/tests/ui/str/str-array-assignment.stderr index c23400a1d14..515cb9e12f8 100644 --- a/tests/ui/str/str-array-assignment.stderr +++ b/tests/ui/str/str-array-assignment.stderr @@ -10,10 +10,12 @@ error[E0308]: mismatched types --> $DIR/str-array-assignment.rs:5:27 | LL | let u: &str = if true { s[..2] } else { s }; - | ^^^^^^ - | | - | expected `&str`, found `str` - | help: consider borrowing here: `&s[..2]` + | ^^^^^^ expected `&str`, found `str` + | +help: consider borrowing here + | +LL | let u: &str = if true { &s[..2] } else { s }; + | + error[E0277]: the size for values of type `str` cannot be known at compilation time --> $DIR/str-array-assignment.rs:7:7 @@ -33,11 +35,14 @@ error[E0308]: mismatched types --> $DIR/str-array-assignment.rs:9:17 | LL | let w: &str = s[..2]; - | ---- ^^^^^^ - | | | - | | expected `&str`, found `str` - | | help: consider borrowing here: `&s[..2]` + | ---- ^^^^^^ expected `&str`, found `str` + | | | expected due to this + | +help: consider borrowing here + | +LL | let w: &str = &s[..2]; + | + error: aborting due to 4 previous errors diff --git a/tests/ui/suggestions/suggest-ref-macro.rs b/tests/ui/suggestions/suggest-ref-macro.rs index 6f780f32a14..730f5fa1b5e 100644 --- a/tests/ui/suggestions/suggest-ref-macro.rs +++ b/tests/ui/suggestions/suggest-ref-macro.rs @@ -14,7 +14,7 @@ macro_rules! bla { () => { x(123); //~^ ERROR mismatched types - //~| SUGGESTION &mut 123 + //~| SUGGESTION &mut }; ($v:expr) => { x($v) @@ -25,5 +25,5 @@ fn main() { bla!(); bla!(456); //~^ ERROR mismatched types - //~| SUGGESTION &mut 456 + //~| SUGGESTION &mut } diff --git a/tests/ui/suggestions/suggest-ref-macro.stderr b/tests/ui/suggestions/suggest-ref-macro.stderr index 17de49fbd84..08bc9e86a50 100644 --- a/tests/ui/suggestions/suggest-ref-macro.stderr +++ b/tests/ui/suggestions/suggest-ref-macro.stderr @@ -18,10 +18,8 @@ error[E0308]: mismatched types --> $DIR/suggest-ref-macro.rs:15:11 | LL | x(123); - | - ^^^ - | | | - | | expected `&mut i32`, found integer - | | help: consider mutably borrowing here: `&mut 123` + | - ^^^ expected `&mut i32`, found integer + | | | arguments to this function are incorrect ... LL | bla!(); @@ -33,6 +31,10 @@ note: function defined here LL | fn x(_: &mut i32) {} | ^ ----------- = note: this error originates in the macro `bla` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider mutably borrowing here + | +LL | x(&mut 123); + | ++++ error[E0308]: mismatched types --> $DIR/suggest-ref-macro.rs:26:10 @@ -41,16 +43,17 @@ LL | x($v) | - arguments to this function are incorrect ... LL | bla!(456); - | ^^^ - | | - | expected `&mut i32`, found integer - | help: consider mutably borrowing here: `&mut 456` + | ^^^ expected `&mut i32`, found integer | note: function defined here --> $DIR/suggest-ref-macro.rs:11:4 | LL | fn x(_: &mut i32) {} | ^ ----------- +help: consider mutably borrowing here + | +LL | bla!(&mut 456); + | ++++ error: aborting due to 3 previous errors diff --git a/tests/ui/type/type-mismatch.stderr b/tests/ui/type/type-mismatch.stderr index 67a1f893050..ce6f29d354f 100644 --- a/tests/ui/type/type-mismatch.stderr +++ b/tests/ui/type/type-mismatch.stderr @@ -378,10 +378,8 @@ error[E0308]: mismatched types --> $DIR/type-mismatch.rs:47:23 | LL | want::<&Foo>(f); - | ----------------- ^ - | | | - | | expected `&Foo`, found `Foo` - | | help: consider borrowing here: `&f` + | ----------------- ^ expected `&Foo`, found `Foo` + | | | arguments to this function are incorrect | = note: expected reference `&Foo` @@ -391,6 +389,10 @@ note: function defined here | LL | fn want(t: T) {} | ^^^^ ---- +help: consider borrowing here + | +LL | want::<&Foo>(&f); + | + error[E0308]: mismatched types --> $DIR/type-mismatch.rs:48:26 @@ -556,10 +558,8 @@ error[E0308]: mismatched types --> $DIR/type-mismatch.rs:61:26 | LL | want::<&Foo>(f); - | -------------------- ^ - | | | - | | expected `&Foo`, found `Foo` - | | help: consider borrowing here: `&f` + | -------------------- ^ expected `&Foo`, found `Foo` + | | | arguments to this function are incorrect | = note: expected reference `&Foo` @@ -569,6 +569,10 @@ note: function defined here | LL | fn want(t: T) {} | ^^^^ ---- +help: consider borrowing here + | +LL | want::<&Foo>(&f); + | + error[E0308]: mismatched types --> $DIR/type-mismatch.rs:65:19 diff --git a/tests/ui/typeck/bad-index-due-to-nested.stderr b/tests/ui/typeck/bad-index-due-to-nested.stderr index cdb23372c4b..f9cdb280e27 100644 --- a/tests/ui/typeck/bad-index-due-to-nested.stderr +++ b/tests/ui/typeck/bad-index-due-to-nested.stderr @@ -42,13 +42,14 @@ error[E0308]: mismatched types LL | fn index<'a, K, V>(map: &'a HashMap, k: K) -> &'a V { | - this type parameter LL | map[k] - | ^ - | | - | expected `&K`, found type parameter `K` - | help: consider borrowing here: `&k` + | ^ expected `&K`, found type parameter `K` | = note: expected reference `&K` found type parameter `K` +help: consider borrowing here + | +LL | map[&k] + | + error[E0308]: mismatched types --> $DIR/bad-index-due-to-nested.rs:20:5 @@ -56,13 +57,14 @@ error[E0308]: mismatched types LL | fn index<'a, K, V>(map: &'a HashMap, k: K) -> &'a V { | - this type parameter ----- expected `&'a V` because of return type LL | map[k] - | ^^^^^^ - | | - | expected `&V`, found type parameter `V` - | help: consider borrowing here: `&map[k]` + | ^^^^^^ expected `&V`, found type parameter `V` | = note: expected reference `&'a V` found type parameter `V` +help: consider borrowing here + | +LL | &map[k] + | + error: aborting due to 4 previous errors diff --git a/tests/ui/typeck/bad-type-in-vec-contains.stderr b/tests/ui/typeck/bad-type-in-vec-contains.stderr index 72533ab1fa3..b9b3a5fe5ec 100644 --- a/tests/ui/typeck/bad-type-in-vec-contains.stderr +++ b/tests/ui/typeck/bad-type-in-vec-contains.stderr @@ -2,16 +2,18 @@ error[E0308]: mismatched types --> $DIR/bad-type-in-vec-contains.rs:5:21 | LL | primes.contains(3); - | -------- ^ - | | | - | | expected `&_`, found integer - | | help: consider borrowing here: `&3` + | -------- ^ expected `&_`, found integer + | | | arguments to this method are incorrect | = note: expected reference `&_` found type `{integer}` note: method defined here --> $SRC_DIR/core/src/slice/mod.rs:LL:COL +help: consider borrowing here + | +LL | primes.contains(&3); + | + error: aborting due to previous error diff --git a/tests/ui/typeck/issue-13853.stderr b/tests/ui/typeck/issue-13853.stderr index 11d34f5b93b..8ecb8b68016 100644 --- a/tests/ui/typeck/issue-13853.stderr +++ b/tests/ui/typeck/issue-13853.stderr @@ -20,10 +20,8 @@ error[E0308]: mismatched types --> $DIR/issue-13853.rs:37:13 | LL | iterate(graph); - | ------- ^^^^^ - | | | - | | expected `&_`, found `Vec` - | | help: consider borrowing here: `&graph` + | ------- ^^^^^ expected `&_`, found `Vec` + | | | arguments to this function are incorrect | = note: expected reference `&_` @@ -33,6 +31,10 @@ note: function defined here | LL | fn iterate>(graph: &G) { | ^^^^^^^ --------- +help: consider borrowing here + | +LL | iterate(&graph); + | + error: aborting due to 3 previous errors diff --git a/tests/ui/unsized-locals/suggest-borrow.stderr b/tests/ui/unsized-locals/suggest-borrow.stderr index d456c16de0d..8741b35cdcf 100644 --- a/tests/ui/unsized-locals/suggest-borrow.stderr +++ b/tests/ui/unsized-locals/suggest-borrow.stderr @@ -16,11 +16,14 @@ error[E0308]: mismatched types --> $DIR/suggest-borrow.rs:3:20 | LL | let x: &[u8] = vec!(1, 2, 3)[..]; - | ----- ^^^^^^^^^^^^^^^^^ - | | | - | | expected `&[u8]`, found `[{integer}]` - | | help: consider borrowing here: `&vec!(1, 2, 3)[..]` + | ----- ^^^^^^^^^^^^^^^^^ expected `&[u8]`, found `[{integer}]` + | | | expected due to this + | +help: consider borrowing here + | +LL | let x: &[u8] = &vec!(1, 2, 3)[..]; + | + error[E0308]: mismatched types --> $DIR/suggest-borrow.rs:4:19