Skip to content

Commit 3a5567b

Browse files
committed
Address PR comments
1 parent ca701d7 commit 3a5567b

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

src/librustc_errors/emitter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ impl Emitter for EmitterWriter {
3737

3838
if let Some(sugg) = db.suggestion.clone() {
3939
assert_eq!(sugg.msp.primary_spans().len(), sugg.substitutes.len());
40-
if sugg.substitutes.len() == 1 {
40+
if sugg.substitutes.len() == 1 && // don't display multispans as labels
41+
sugg.msg.split_whitespace().count() < 10 && // don't display long messages as labels
42+
sugg.substitutes[0].find('\n').is_none() { // don't display multiline suggestions as labels
4143
let msg = format!("{} `{}`", sugg.msg, sugg.substitutes[0]);
4244
primary_span.push_span_label(sugg.msp.primary_spans()[0], msg);
4345
} else {

src/librustc_typeck/check/op.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
197197
// error types are considered "builtin"
198198
if !lhs_ty.references_error() {
199199
if let IsAssign::Yes = is_assign {
200-
struct_span_err!(self.tcx.sess, lhs_expr.span, E0368,
200+
struct_span_err!(self.tcx.sess, expr.span, E0368,
201201
"binary assignment operation `{}=` \
202202
cannot be applied to type `{}`",
203203
op.node.as_str(),
@@ -207,7 +207,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
207207
op.node.as_str(), lhs_ty))
208208
.emit();
209209
} else {
210-
let mut err = struct_span_err!(self.tcx.sess, lhs_expr.span, E0369,
210+
let mut err = struct_span_err!(self.tcx.sess, expr.span, E0369,
211211
"binary operation `{}` cannot be applied to type `{}`",
212212
op.node.as_str(),
213213
lhs_ty);
@@ -244,7 +244,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
244244

245245
if let Some(missing_trait) = missing_trait {
246246
if missing_trait == "std::ops::Add" &&
247-
self.check_str_addition(lhs_expr, lhs_ty,
247+
self.check_str_addition(expr, lhs_expr, lhs_ty,
248248
rhs_expr, rhs_ty_var, &mut err) {
249249
// This has nothing here because it means we did string
250250
// concatenation (e.g. "Hello " + "World!"). This means
@@ -266,6 +266,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
266266
}
267267

268268
fn check_str_addition(&self,
269+
expr: &'gcx hir::Expr,
269270
lhs_expr: &'gcx hir::Expr,
270271
lhs_ty: Ty<'tcx>,
271272
rhs_expr: &'gcx hir::Expr,
@@ -277,7 +278,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
277278
if let TyRef(_, l_ty) = lhs_ty.sty {
278279
if let TyRef(_, r_ty) = rhs_ty.sty {
279280
if l_ty.ty.sty == TyStr && r_ty.ty.sty == TyStr {
280-
err.note("`+` can't be used to concatenate two `&str` strings");
281+
err.span_label(expr.span,
282+
&"`+` can't be used to concatenate two `&str` strings");
281283
let codemap = self.tcx.sess.codemap();
282284
let suggestion =
283285
match codemap.span_to_snippet(lhs_expr.span) {
@@ -289,7 +291,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
289291
from a string reference. String concatenation \
290292
appends the string on the right to the string \
291293
on the left and may require reallocation. This \
292-
requires ownership of the string on the left:"), suggestion);
294+
requires ownership of the string on the left."), suggestion);
293295
is_string_addition = true;
294296
}
295297

src/librustc_typeck/diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,6 +3181,13 @@ x << 2; // ok!
31813181
31823182
It is also possible to overload most operators for your own type by
31833183
implementing traits from `std::ops`.
3184+
3185+
String concatenation appends the string on the right to the string on the
3186+
left and may require reallocation. This requires ownership of the string
3187+
on the left. If something should be added to a string literal, move the
3188+
literal to the heap by allocating it with `to_owned()` like in
3189+
`"Your text".to_owned()`.
3190+
31843191
"##,
31853192

31863193
E0370: r##"

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5160,9 +5160,7 @@ impl<'a> Parser<'a> {
51605160
`pub(in path::to::module)`: visible only on the specified path"##;
51615161
let path = self.parse_path(PathStyle::Mod)?;
51625162
let path_span = self.prev_span;
5163-
let help_msg = format!("to make this visible only to module `{}`, add `in` before \
5164-
the path:",
5165-
path);
5163+
let help_msg = format!("make this visible only to module `{}` with `in`:", path);
51665164
self.expect(&token::CloseDelim(token::Paren))?; // `)`
51675165
let mut err = self.span_fatal_help(path_span, &msg, &suggestion);
51685166
err.span_suggestion(path_span, &help_msg, format!("in {}", path));

src/test/ui/pub/pub-restricted.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: incorrect visibility restriction
22
--> $DIR/pub-restricted.rs:15:6
33
|
44
15 | pub (a) fn afn() {}
5-
| ^ to make this visible only to module `a`, add `in` before the path: `in a`
5+
| ^ make this visible only to module `a` with `in`: `in a`
66
|
77
= help: some possible visibility restrictions are:
88
`pub(crate)`: visible only on the current crate
@@ -13,7 +13,7 @@ error: incorrect visibility restriction
1313
--> $DIR/pub-restricted.rs:16:6
1414
|
1515
16 | pub (b) fn bfn() {}
16-
| ^ to make this visible only to module `b`, add `in` before the path: `in b`
16+
| ^ make this visible only to module `b` with `in`: `in b`
1717
|
1818
= help: some possible visibility restrictions are:
1919
`pub(crate)`: visible only on the current crate
@@ -24,7 +24,7 @@ error: incorrect visibility restriction
2424
--> $DIR/pub-restricted.rs:32:14
2525
|
2626
32 | pub (a) invalid: usize,
27-
| ^ to make this visible only to module `a`, add `in` before the path: `in a`
27+
| ^ make this visible only to module `a` with `in`: `in a`
2828
|
2929
= help: some possible visibility restrictions are:
3030
`pub(crate)`: visible only on the current crate
@@ -35,7 +35,7 @@ error: incorrect visibility restriction
3535
--> $DIR/pub-restricted.rs:41:6
3636
|
3737
41 | pub (xyz) fn xyz() {}
38-
| ^^^ to make this visible only to module `xyz`, add `in` before the path: `in xyz`
38+
| ^^^ make this visible only to module `xyz` with `in`: `in xyz`
3939
|
4040
= help: some possible visibility restrictions are:
4141
`pub(crate)`: visible only on the current crate

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ error[E0369]: binary operation `+` cannot be applied to type `&'static str`
22
--> $DIR/issue-39018.rs:12:13
33
|
44
12 | let x = "Hello " + "World!";
5-
| ^^^^^^^^ `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: `"Hello ".to_owned()`
5+
| ^^^^^^^^^^^^^^^^^^^ `+` can't be used to concatenate two `&str` strings
66
|
7-
= note: `+` can't be used to concatenate two `&str` strings
7+
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.
8+
| let x = "Hello ".to_owned() + "World!";
89

910
error[E0369]: binary operation `+` cannot be applied to type `World`
1011
--> $DIR/issue-39018.rs:17:13
1112
|
1213
17 | let y = World::Hello + World::Goodbye;
13-
| ^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1415
|
1516
= note: an implementation of `std::ops::Add` might be missing for `World`
1617

0 commit comments

Comments
 (0)