Skip to content

Commit dddede4

Browse files
authored
Rollup merge of #109472 - MU001999:patch-3, r=eholk
Add parentheses properly for method calls Fixes #109436
2 parents 2a39cf5 + 2580348 commit dddede4

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,31 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
13561356
Applicability::MaybeIncorrect,
13571357
);
13581358
} else {
1359+
let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_mut;
1360+
let sugg_prefix = format!("&{}", if is_mut { "mut " } else { "" });
1361+
let sugg_msg = &format!(
1362+
"consider{} borrowing here",
1363+
if is_mut { " mutably" } else { "" }
1364+
);
1365+
1366+
// Issue #109436, we need to add parentheses properly for method calls
1367+
// for example, `foo.into()` should be `(&foo).into()`
1368+
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(
1369+
self.tcx.sess.source_map().span_look_ahead(span, Some("."), Some(50)),
1370+
) {
1371+
if snippet == "." {
1372+
err.multipart_suggestion_verbose(
1373+
sugg_msg,
1374+
vec![
1375+
(span.shrink_to_lo(), format!("({}", sugg_prefix)),
1376+
(span.shrink_to_hi(), ")".to_string()),
1377+
],
1378+
Applicability::MaybeIncorrect,
1379+
);
1380+
return true;
1381+
}
1382+
}
1383+
13591384
// Issue #104961, we need to add parentheses properly for compond expressions
13601385
// for example, `x.starts_with("hi".to_string() + "you")`
13611386
// should be `x.starts_with(&("hi".to_string() + "you"))`
@@ -1372,14 +1397,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
13721397
_ => false,
13731398
};
13741399

1375-
let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_mut;
13761400
let span = if needs_parens { span } else { span.shrink_to_lo() };
1377-
let sugg_prefix = format!("&{}", if is_mut { "mut " } else { "" });
1378-
let sugg_msg = &format!(
1379-
"consider{} borrowing here",
1380-
if is_mut { " mutably" } else { "" }
1381-
);
1382-
13831401
let suggestions = if !needs_parens {
13841402
vec![(span.shrink_to_lo(), format!("{}", sugg_prefix))]
13851403
} else {

tests/ui/suggestions/issue-109436.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Foo;
2+
struct Bar;
3+
4+
impl From<&Foo> for Bar {
5+
fn from(foo: &Foo) -> Bar {
6+
Bar
7+
}
8+
}
9+
10+
fn main() {
11+
let foo = Foo;
12+
let b: Bar = foo.into(); //~ ERROR E0277
13+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: the trait bound `Foo: Into<_>` is not satisfied
2+
--> $DIR/issue-109436.rs:12:22
3+
|
4+
LL | let b: Bar = foo.into();
5+
| ^^^^ the trait `~const Into<_>` is not implemented for `Foo`
6+
|
7+
= note: required for `Foo` to implement `Into<Bar>`
8+
help: consider borrowing here
9+
|
10+
LL | let b: Bar = (&foo).into();
11+
| ++ +
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)