Skip to content

Commit 2cb9181

Browse files
committed
Fix binop span
1 parent 234adf8 commit 2cb9181

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/librustc_typeck/check/op.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
305305
};
306306
if let Some(missing_trait) = missing_trait {
307307
if op.node == hir::BinOpKind::Add &&
308-
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
309-
rhs_ty, &mut err, true, op) {
308+
self.check_str_addition(
309+
lhs_expr, rhs_expr, lhs_ty, rhs_ty, &mut err, true, op) {
310310
// This has nothing here because it means we did string
311311
// concatenation (e.g., "Hello " += "World!"). This means
312312
// we don't want the note in the else clause to be emitted
@@ -400,8 +400,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
400400
};
401401
if let Some(missing_trait) = missing_trait {
402402
if op.node == hir::BinOpKind::Add &&
403-
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
404-
rhs_ty, &mut err, false, op) {
403+
self.check_str_addition(
404+
lhs_expr, rhs_expr, lhs_ty, rhs_ty, &mut err, false, op) {
405405
// This has nothing here because it means we did string
406406
// concatenation (e.g., "Hello " + "World!"). This means
407407
// we don't want the note in the else clause to be emitted
@@ -509,7 +509,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
509509
/// to print the normal "implementation of `std::ops::Add` might be missing" note
510510
fn check_str_addition(
511511
&self,
512-
expr: &'gcx hir::Expr,
513512
lhs_expr: &'gcx hir::Expr,
514513
rhs_expr: &'gcx hir::Expr,
515514
lhs_ty: Ty<'tcx>,
@@ -537,7 +536,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
537536
&format!("{:?}", rhs_ty) == "&&str"
538537
) =>
539538
{
540-
if !is_assign {
539+
if !is_assign { // Do not supply this message if `&str += &str`
541540
err.span_label(
542541
op.span,
543542
"`+` can't be used to concatenate two `&str` strings",
@@ -570,8 +569,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
570569
if (l_ty.sty == Str || &format!("{:?}", l_ty) == "std::string::String") &&
571570
&format!("{:?}", rhs_ty) == "std::string::String" =>
572571
{
573-
err.span_label(expr.span,
574-
"`+` can't be used to concatenate a `&str` with a `String`");
572+
err.span_label(
573+
op.span,
574+
"`+` can't be used to concatenate a `&str` with a `String`",
575+
);
575576
match (
576577
source_map.span_to_snippet(lhs_expr.span),
577578
source_map.span_to_snippet(rhs_expr.span),

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,8 +3540,7 @@ impl<'a> Parser<'a> {
35403540
let binary = self.mk_binary(source_map::respan(cur_op_span, ast_op), lhs, rhs);
35413541
self.mk_expr(span, binary, ThinVec::new())
35423542
}
3543-
AssocOp::Assign =>
3544-
self.mk_expr(span, ExprKind::Assign(lhs, rhs), ThinVec::new()),
3543+
AssocOp::Assign => self.mk_expr(span, ExprKind::Assign(lhs, rhs), ThinVec::new()),
35453544
AssocOp::ObsoleteInPlace =>
35463545
self.mk_expr(span, ExprKind::ObsoleteInPlace(lhs, rhs), ThinVec::new()),
35473546
AssocOp::AssignOp(k) => {

src/test/ui/span/issue-39018.stderr

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&str`
2525
--> $DIR/issue-39018.rs:11:22
2626
|
2727
LL | let x = "Hello " + "World!".to_owned();
28-
| ---------^--------------------
29-
| | |
30-
| | std::string::String
28+
| -------- ^ ------------------- std::string::String
29+
| | |
30+
| | `+` can't be used to concatenate a `&str` with a `String`
3131
| &str
32-
| `+` can't be used to concatenate a `&str` with a `String`
3332
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
3433
|
3534
LL | let x = "Hello ".to_owned() + &"World!".to_owned();
@@ -52,11 +51,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri
5251
--> $DIR/issue-39018.rs:27:16
5352
|
5453
LL | let _ = &a + b;
55-
| ---^--
56-
| | |
57-
| | std::string::String
54+
| -- ^ - std::string::String
55+
| | |
56+
| | `+` can't be used to concatenate a `&str` with a `String`
5857
| &std::string::String
59-
| `+` can't be used to concatenate a `&str` with a `String`
6058
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
6159
|
6260
LL | let _ = &a.to_owned() + &b;
@@ -78,11 +76,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri
7876
--> $DIR/issue-39018.rs:30:15
7977
|
8078
LL | let _ = e + b;
81-
| --^--
82-
| | |
83-
| | std::string::String
79+
| - ^ - std::string::String
80+
| | |
81+
| | `+` can't be used to concatenate a `&str` with a `String`
8482
| &std::string::String
85-
| `+` can't be used to concatenate a `&str` with a `String`
8683
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
8784
|
8885
LL | let _ = e.to_owned() + &b;

0 commit comments

Comments
 (0)