Skip to content

Commit 2ecc722

Browse files
authored
Rollup merge of #110504 - compiler-errors:tweak-borrow-sugg, 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 (#110432 (comment)). Also some drive-by renames of functions that have been annoying me for a bit.
2 parents 02a85bd + 4731a25 commit 2ecc722

36 files changed

+527
-391
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+102-96
Large diffs are not rendered by default.

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
275275
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
276276
) -> bool {
277277
let expr = expr.peel_blocks();
278-
if let Some((sp, msg, suggestion, applicability, verbose, annotation)) =
279-
self.check_ref(expr, found, expected)
278+
if let Some((suggestion, msg, applicability, verbose, annotation)) =
279+
self.suggest_deref_or_ref(expr, found, expected)
280280
{
281281
if verbose {
282-
err.span_suggestion_verbose(sp, msg, suggestion, applicability);
282+
err.multipart_suggestion_verbose(msg, suggestion, applicability);
283283
} else {
284-
err.span_suggestion(sp, msg, suggestion, applicability);
284+
err.multipart_suggestion(msg, suggestion, applicability);
285285
}
286286
if annotation {
287287
let suggest_annotation = match expr.peel_drop_temps().kind {
@@ -343,7 +343,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
343343
err.span_label(sp, format!("{descr} `{name}` defined here"));
344344
}
345345
return true;
346-
} else if self.check_for_cast(err, expr, found, expected, expected_ty_expr) {
346+
} else if self.suggest_cast(err, expr, found, expected, expected_ty_expr) {
347347
return true;
348348
} else {
349349
let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id);

compiler/rustc_hir_typeck/src/method/suggest.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10451045
);
10461046
}
10471047

1048-
self.check_for_inner_self(&mut err, source, rcvr_ty, item_name);
1048+
self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);
10491049

10501050
bound_spans.sort();
10511051
bound_spans.dedup();
@@ -1132,7 +1132,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11321132
}
11331133
}
11341134

1135-
self.check_for_deref_method(&mut err, source, rcvr_ty, item_name, expected);
1135+
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
11361136
return Some(err);
11371137
}
11381138

@@ -1805,7 +1805,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18051805
}
18061806
}
18071807

1808-
fn check_for_inner_self(
1808+
fn suggest_unwrapping_inner_self(
18091809
&self,
18101810
err: &mut Diagnostic,
18111811
source: SelfSource<'tcx>,
@@ -2175,7 +2175,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21752175
}
21762176
}
21772177

2178-
fn check_for_deref_method(
2178+
fn note_derefed_ty_has_method(
21792179
&self,
21802180
err: &mut Diagnostic,
21812181
self_source: SelfSource<'tcx>,

tests/ui/argument-suggestions/issue-97484.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | fn foo(a: &A, d: D, e: &E, g: G) {}
1616
help: consider borrowing here
1717
|
1818
LL | foo(&&A, B, C, D, &E, F, G);
19-
| ~~
19+
| +
2020
help: remove the extra arguments
2121
|
2222
LL - foo(&&A, B, C, D, E, F, G);

tests/ui/async-await/issues/issue-102206.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ error[E0308]: mismatched types
22
--> $DIR/issue-102206.rs:6:27
33
|
44
LL | std::mem::size_of_val(foo());
5-
| --------------------- ^^^^^
6-
| | |
7-
| | expected `&_`, found future
8-
| | help: consider borrowing here: `&foo()`
5+
| --------------------- ^^^^^ expected `&_`, found future
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: function defined here
1210
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
11+
help: consider borrowing here
12+
|
13+
LL | std::mem::size_of_val(&foo());
14+
| +
1315

1416
error: aborting due to previous error
1517

tests/ui/coercion/coercion-slice.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ error[E0308]: mismatched types
22
--> $DIR/coercion-slice.rs:4:21
33
|
44
LL | let _: &[i32] = [0];
5-
| ------ ^^^
6-
| | |
7-
| | expected `&[i32]`, found `[{integer}; 1]`
8-
| | help: consider borrowing here: `&[0]`
5+
| ------ ^^^ expected `&[i32]`, found `[{integer}; 1]`
6+
| |
97
| expected due to this
8+
|
9+
help: consider borrowing here
10+
|
11+
LL | let _: &[i32] = &[0];
12+
| +
1013

1114
error: aborting due to previous error
1215

tests/ui/inference/deref-suggestion.stderr

+12-8
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,23 @@ error[E0308]: mismatched types
9898
--> $DIR/deref-suggestion.rs:40:17
9999
|
100100
LL | let s = S { u };
101-
| ^
102-
| |
103-
| expected `&u32`, found integer
104-
| help: consider borrowing here: `u: &u`
101+
| ^ expected `&u32`, found integer
102+
|
103+
help: consider borrowing here
104+
|
105+
LL | let s = S { u: &u };
106+
| ++++
105107

106108
error[E0308]: mismatched types
107109
--> $DIR/deref-suggestion.rs:42:20
108110
|
109111
LL | let s = S { u: u };
110-
| ^
111-
| |
112-
| expected `&u32`, found integer
113-
| help: consider borrowing here: `&u`
112+
| ^ expected `&u32`, found integer
113+
|
114+
help: consider borrowing here
115+
|
116+
LL | let s = S { u: &u };
117+
| +
114118

115119
error[E0308]: mismatched types
116120
--> $DIR/deref-suggestion.rs:45:17

tests/ui/issues/issue-11374.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ error[E0308]: mismatched types
22
--> $DIR/issue-11374.rs:26:15
33
|
44
LL | c.read_to(v);
5-
| ------- ^
6-
| | |
7-
| | expected `&mut [u8]`, found `Vec<_>`
8-
| | help: consider mutably borrowing here: `&mut v`
5+
| ------- ^ expected `&mut [u8]`, found `Vec<_>`
6+
| |
97
| arguments to this method are incorrect
108
|
119
= note: expected mutable reference `&mut [u8]`
@@ -15,6 +13,10 @@ note: method defined here
1513
|
1614
LL | pub fn read_to(&mut self, vec: &mut [u8]) {
1715
| ^^^^^^^ --------------
16+
help: consider mutably borrowing here
17+
|
18+
LL | c.read_to(&mut v);
19+
| ++++
1820

1921
error: aborting due to previous error
2022

tests/ui/issues/issue-17033.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ error[E0308]: mismatched types
22
--> $DIR/issue-17033.rs:2:10
33
|
44
LL | (*p)(())
5-
| ---- ^^
6-
| | |
7-
| | expected `&mut ()`, found `()`
8-
| | help: consider mutably borrowing here: `&mut ()`
5+
| ---- ^^ expected `&mut ()`, found `()`
6+
| |
97
| arguments to this function are incorrect
8+
|
9+
help: consider mutably borrowing here
10+
|
11+
LL | (*p)(&mut ())
12+
| ++++
1013

1114
error: aborting due to previous error
1215

tests/ui/issues/issue-18819.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | fn print_x(_: &dyn Foo<Item=bool>, extra: &str) {
1919
help: consider borrowing here
2020
|
2121
LL | print_x(&X);
22-
| ~~
22+
| +
2323
help: provide the argument
2424
|
2525
LL | print_x(/* &dyn Foo<Item = bool> */, /* &str */);

tests/ui/issues/issue-46302.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error[E0308]: mismatched types
22
--> $DIR/issue-46302.rs:3:27
33
|
44
LL | let u: &str = if true { s[..2] } else { s };
5-
| ^^^^^^
6-
| |
7-
| expected `&str`, found `str`
8-
| help: consider borrowing here: `&s[..2]`
5+
| ^^^^^^ expected `&str`, found `str`
6+
|
7+
help: consider borrowing here
8+
|
9+
LL | let u: &str = if true { &s[..2] } else { s };
10+
| +
911

1012
error: aborting due to previous error
1113

tests/ui/issues/issue-46756-consider-borrowing-cast-or-binexpr.stderr

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,37 @@ error[E0308]: mismatched types
22
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:12:42
33
|
44
LL | light_flows_our_war_of_mocking_words(behold as usize);
5-
| ------------------------------------ ^^^^^^^^^^^^^^^
6-
| | |
7-
| | expected `&usize`, found `usize`
8-
| | help: consider borrowing here: `&(behold as usize)`
5+
| ------------------------------------ ^^^^^^^^^^^^^^^ expected `&usize`, found `usize`
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: function defined here
1210
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:5:4
1311
|
1412
LL | fn light_flows_our_war_of_mocking_words(and_yet: &usize) -> usize {
1513
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---------------
14+
help: consider borrowing here
15+
|
16+
LL | light_flows_our_war_of_mocking_words(&(behold as usize));
17+
| ++ +
1618

1719
error[E0308]: mismatched types
1820
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:14:42
1921
|
2022
LL | light_flows_our_war_of_mocking_words(with_tears + 4);
21-
| ------------------------------------ ^^^^^^^^^^^^^^
22-
| | |
23-
| | expected `&usize`, found `usize`
24-
| | help: consider borrowing here: `&(with_tears + 4)`
23+
| ------------------------------------ ^^^^^^^^^^^^^^ expected `&usize`, found `usize`
24+
| |
2525
| arguments to this function are incorrect
2626
|
2727
note: function defined here
2828
--> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:5:4
2929
|
3030
LL | fn light_flows_our_war_of_mocking_words(and_yet: &usize) -> usize {
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---------------
32+
help: consider borrowing here
33+
|
34+
LL | light_flows_our_war_of_mocking_words(&(with_tears + 4));
35+
| ++ +
3236

3337
error: aborting due to 2 previous errors
3438

tests/ui/issues/issue-61106.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0308]: mismatched types
22
--> $DIR/issue-61106.rs:3:9
33
|
44
LL | foo(x.clone());
5-
| --- ^^^^^^^^^
6-
| | |
7-
| | expected `&str`, found `String`
8-
| | help: consider borrowing here: `&x`
5+
| --- ^^^^^^^^^ expected `&str`, found `String`
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: function defined here
1210
--> $DIR/issue-61106.rs:6:4
1311
|
1412
LL | fn foo(_: &str) {}
1513
| ^^^ -------
14+
help: consider borrowing here
15+
|
16+
LL | foo(&x.clone());
17+
| +
1618

1719
error: aborting due to previous error
1820

tests/ui/methods/method-self-arg-1.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0308]: mismatched types
22
--> $DIR/method-self-arg-1.rs:11:14
33
|
44
LL | Foo::bar(x);
5-
| -------- ^
6-
| | |
7-
| | expected `&Foo`, found `Foo`
8-
| | help: consider borrowing here: `&x`
5+
| -------- ^ expected `&Foo`, found `Foo`
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: method defined here
1210
--> $DIR/method-self-arg-1.rs:6:8
1311
|
1412
LL | fn bar(&self) {}
1513
| ^^^ -----
14+
help: consider borrowing here
15+
|
16+
LL | Foo::bar(&x);
17+
| +
1618

1719
error[E0308]: mismatched types
1820
--> $DIR/method-self-arg-1.rs:13:14

tests/ui/mismatched_types/dont-point-return-on-E0308.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0308]: mismatched types
22
--> $DIR/dont-point-return-on-E0308.rs:11:11
33
|
44
LL | f(());
5-
| - ^^
6-
| | |
7-
| | expected `&()`, found `()`
8-
| | help: consider borrowing here: `&()`
5+
| - ^^ expected `&()`, found `()`
6+
| |
97
| arguments to this function are incorrect
108
|
119
note: function defined here
1210
--> $DIR/dont-point-return-on-E0308.rs:3:10
1311
|
1412
LL | async fn f(_: &()) {}
1513
| ^ ------
14+
help: consider borrowing here
15+
|
16+
LL | f(&());
17+
| +
1618

1719
error: aborting due to previous error
1820

tests/ui/mut/mut-cross-borrowing.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ error[E0308]: mismatched types
22
--> $DIR/mut-cross-borrowing.rs:7:7
33
|
44
LL | f(x)
5-
| - ^
6-
| | |
7-
| | expected `&mut isize`, found `Box<{integer}>`
8-
| | help: consider mutably borrowing here: `&mut x`
5+
| - ^ expected `&mut isize`, found `Box<{integer}>`
6+
| |
97
| arguments to this function are incorrect
108
|
119
= note: expected mutable reference `&mut isize`
@@ -15,6 +13,10 @@ note: function defined here
1513
|
1614
LL | fn f(_: &mut isize) {}
1715
| ^ -------------
16+
help: consider mutably borrowing here
17+
|
18+
LL | f(&mut x)
19+
| ++++
1820

1921
error: aborting due to previous error
2022

0 commit comments

Comments
 (0)