Skip to content

Commit 7738eb4

Browse files
authored
Rollup merge of rust-lang#48392 - estebank:string, r=petrochenkov
Handle custom diagnostic for `&str + String` Now all of `&str + &str`, `&str + String` and `String + String` have relevant diagnostic output.
2 parents d45c4a6 + 20bc72e commit 7738eb4

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

src/librustc_typeck/check/op.rs

+43-26
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use super::{FnCtxt, Needs};
1414
use super::method::MethodCallee;
1515
use rustc::ty::{self, Ty, TypeFoldable, TypeVariants};
16-
use rustc::ty::TypeVariants::{TyStr, TyRef};
16+
use rustc::ty::TypeVariants::{TyStr, TyRef, TyAdt};
1717
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
1818
use rustc::infer::type_variable::TypeVariableOrigin;
1919
use errors;
@@ -299,7 +299,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
299299

300300
if let Some(missing_trait) = missing_trait {
301301
if missing_trait == "std::ops::Add" &&
302-
self.check_str_addition(expr, lhs_expr, lhs_ty,
302+
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
303303
rhs_ty, &mut err) {
304304
// This has nothing here because it means we did string
305305
// concatenation (e.g. "Hello " + "World!"). This means
@@ -328,37 +328,54 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
328328
fn check_str_addition(&self,
329329
expr: &'gcx hir::Expr,
330330
lhs_expr: &'gcx hir::Expr,
331+
rhs_expr: &'gcx hir::Expr,
331332
lhs_ty: Ty<'tcx>,
332333
rhs_ty: Ty<'tcx>,
333334
err: &mut errors::DiagnosticBuilder) -> bool {
335+
let codemap = self.tcx.sess.codemap();
336+
let msg = "`to_owned()` can be used to create an owned `String` \
337+
from a string reference. String concatenation \
338+
appends the string on the right to the string \
339+
on the left and may require reallocation. This \
340+
requires ownership of the string on the left";
334341
// If this function returns true it means a note was printed, so we don't need
335342
// to print the normal "implementation of `std::ops::Add` might be missing" note
336-
let mut is_string_addition = false;
337-
if let TyRef(_, l_ty) = lhs_ty.sty {
338-
if let TyRef(_, r_ty) = rhs_ty.sty {
339-
if l_ty.ty.sty == TyStr && r_ty.ty.sty == TyStr {
340-
err.span_label(expr.span,
341-
"`+` can't be used to concatenate two `&str` strings");
342-
let codemap = self.tcx.sess.codemap();
343-
let suggestion =
344-
match codemap.span_to_snippet(lhs_expr.span) {
345-
Ok(lstring) => format!("{}.to_owned()", lstring),
346-
_ => format!("<expression>")
347-
};
348-
err.span_suggestion(lhs_expr.span,
349-
&format!("`to_owned()` can be used to create an owned `String` \
350-
from a string reference. String concatenation \
351-
appends the string on the right to the string \
352-
on the left and may require reallocation. This \
353-
requires ownership of the string on the left"), suggestion);
354-
is_string_addition = true;
355-
}
356-
343+
match (&lhs_ty.sty, &rhs_ty.sty) {
344+
(&TyRef(_, ref l_ty), &TyRef(_, ref r_ty))
345+
if l_ty.ty.sty == TyStr && r_ty.ty.sty == TyStr => {
346+
err.span_label(expr.span,
347+
"`+` can't be used to concatenate two `&str` strings");
348+
match codemap.span_to_snippet(lhs_expr.span) {
349+
Ok(lstring) => err.span_suggestion(lhs_expr.span,
350+
msg,
351+
format!("{}.to_owned()", lstring)),
352+
_ => err.help(msg),
353+
};
354+
true
357355
}
358-
356+
(&TyRef(_, ref l_ty), &TyAdt(..))
357+
if l_ty.ty.sty == TyStr && &format!("{:?}", rhs_ty) == "std::string::String" => {
358+
err.span_label(expr.span,
359+
"`+` can't be used to concatenate a `&str` with a `String`");
360+
match codemap.span_to_snippet(lhs_expr.span) {
361+
Ok(lstring) => err.span_suggestion(lhs_expr.span,
362+
msg,
363+
format!("{}.to_owned()", lstring)),
364+
_ => err.help(msg),
365+
};
366+
match codemap.span_to_snippet(rhs_expr.span) {
367+
Ok(rstring) => {
368+
err.span_suggestion(rhs_expr.span,
369+
"you also need to borrow the `String` on the right to \
370+
get a `&str`",
371+
format!("&{}", rstring));
372+
}
373+
_ => {}
374+
};
375+
true
376+
}
377+
_ => false,
359378
}
360-
361-
is_string_addition
362379
}
363380

364381
pub fn check_user_unop(&self,

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

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub fn main() {
1717
// that won't output for the above string concatenation
1818
let y = World::Hello + World::Goodbye;
1919
//~^ ERROR cannot be applied to type
20+
21+
let x = "Hello " + "World!".to_owned();
22+
//~^ ERROR cannot be applied to type
2023
}
2124

2225
enum World {

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,19 @@ error[E0369]: binary operation `+` cannot be applied to type `World`
1616
|
1717
= note: an implementation of `std::ops::Add` might be missing for `World`
1818

19-
error: aborting due to 2 previous errors
19+
error[E0369]: binary operation `+` cannot be applied to type `&str`
20+
--> $DIR/issue-39018.rs:21:13
21+
|
22+
21 | let x = "Hello " + "World!".to_owned();
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `+` can't be used to concatenate a `&str` with a `String`
24+
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
25+
|
26+
21 | let x = "Hello ".to_owned() + "World!".to_owned();
27+
| ^^^^^^^^^^^^^^^^^^^
28+
help: you also need to borrow the `String` on the right to get a `&str`
29+
|
30+
21 | let x = "Hello " + &"World!".to_owned();
31+
| ^^^^^^^^^^^^^^^^^^^^
32+
33+
error: aborting due to 3 previous errors
2034

0 commit comments

Comments
 (0)