Skip to content

Commit

Permalink
Rollup merge of rust-lang#110504 - compiler-errors:tweak-borrow-sugg,…
Browse files Browse the repository at this point in the history
… r=cjgillot

Tweak borrow suggestion span

Avoids a `span_to_snippet` call when we don't need to surround the expression in parentheses. The fact that the suggestion was using the whole span of the expression rather than just appending a `&` was prevented me from using `// run-rustfix` in another PR (rust-lang#110432 (comment)).

Also some drive-by renames of functions that have been annoying me for a bit.
  • Loading branch information
matthiaskrgr committed Apr 29, 2023
2 parents 4cfaead + cf24419 commit 07bf250
Show file tree
Hide file tree
Showing 35 changed files with 517 additions and 383 deletions.
198 changes: 102 additions & 96 deletions compiler/rustc_hir_typeck/src/demand.rs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
) -> bool {
let expr = expr.peel_blocks();
if let Some((sp, msg, suggestion, applicability, verbose, annotation)) =
self.check_ref(expr, found, expected)
if let Some((suggestion, msg, applicability, verbose, annotation)) =
self.suggest_deref_or_ref(expr, found, expected)
{
if verbose {
err.span_suggestion_verbose(sp, &msg, suggestion, applicability);
err.multipart_suggestion_verbose(&msg, suggestion, applicability);
} else {
err.span_suggestion(sp, &msg, suggestion, applicability);
err.multipart_suggestion(&msg, suggestion, applicability);
}
if annotation {
let suggest_annotation = match expr.peel_drop_temps().kind {
Expand Down Expand Up @@ -342,7 +342,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_label(sp, format!("{descr} `{name}` defined here"));
}
return true;
} else if self.check_for_cast(err, expr, found, expected, expected_ty_expr) {
} else if self.suggest_cast(err, expr, found, expected, expected_ty_expr) {
return true;
} else {
let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id);
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
}

self.check_for_inner_self(&mut err, source, rcvr_ty, item_name);
self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);

bound_spans.sort();
bound_spans.dedup();
Expand Down Expand Up @@ -1131,7 +1131,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

self.check_for_deref_method(&mut err, source, rcvr_ty, item_name, expected);
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
return Some(err);
}

Expand Down Expand Up @@ -1804,7 +1804,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

fn check_for_inner_self(
fn suggest_unwrapping_inner_self(
&self,
err: &mut Diagnostic,
source: SelfSource<'tcx>,
Expand Down Expand Up @@ -2174,7 +2174,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

fn check_for_deref_method(
fn note_derefed_ty_has_method(
&self,
err: &mut Diagnostic,
self_source: SelfSource<'tcx>,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-97484.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/async-await/issues/issue-102206.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions tests/ui/coercion/coercion-slice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 12 additions & 8 deletions tests/ui/inference/deref-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/issues/issue-11374.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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]`
Expand All @@ -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

Expand Down
11 changes: 7 additions & 4 deletions tests/ui/issues/issue-17033.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-18819.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LL | fn print_x(_: &dyn Foo<Item=bool>, extra: &str) {
help: consider borrowing here
|
LL | print_x(&X);
| ~~
| +
help: provide the argument
|
LL | print_x(/* &dyn Foo<Item = bool> */, /* &str */);
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/issues/issue-46302.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@ error[E0308]: mismatched types
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:12:42
|
LL | light_flows_our_war_of_mocking_words(behold as usize);
| ------------------------------------ ^^^^^^^^^^^^^^^
| | |
| | expected `&usize`, found `usize`
| | help: consider borrowing here: `&(behold as usize)`
| ------------------------------------ ^^^^^^^^^^^^^^^ expected `&usize`, found `usize`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:5:4
|
LL | fn light_flows_our_war_of_mocking_words(and_yet: &usize) -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---------------
help: consider borrowing here
|
LL | light_flows_our_war_of_mocking_words(&(behold as usize));
| ++ +

error[E0308]: mismatched types
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:14:42
|
LL | light_flows_our_war_of_mocking_words(with_tears + 4);
| ------------------------------------ ^^^^^^^^^^^^^^
| | |
| | expected `&usize`, found `usize`
| | help: consider borrowing here: `&(with_tears + 4)`
| ------------------------------------ ^^^^^^^^^^^^^^ expected `&usize`, found `usize`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:5:4
|
LL | fn light_flows_our_war_of_mocking_words(and_yet: &usize) -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---------------
help: consider borrowing here
|
LL | light_flows_our_war_of_mocking_words(&(with_tears + 4));
| ++ +

error: aborting due to 2 previous errors

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/issues/issue-61106.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ 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
--> $DIR/issue-61106.rs:6:4
|
LL | fn foo(_: &str) {}
| ^^^ -------
help: consider borrowing here
|
LL | foo(&x.clone());
| +

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/methods/method-self-arg-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ 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
--> $DIR/method-self-arg-1.rs:6:8
|
LL | fn bar(&self) {}
| ^^^ -----
help: consider borrowing here
|
LL | Foo::bar(&x);
| +

error[E0308]: mismatched types
--> $DIR/method-self-arg-1.rs:13:14
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/mismatched_types/dont-point-return-on-E0308.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ 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
--> $DIR/dont-point-return-on-E0308.rs:3:10
|
LL | async fn f(_: &()) {}
| ^ ------
help: consider borrowing here
|
LL | f(&());
| +

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/mut/mut-cross-borrowing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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

Expand Down
Loading

0 comments on commit 07bf250

Please sign in to comment.