Skip to content

Commit fe19b47

Browse files
committed
Rollup merge of rust-lang#33820 - jonathandturner:format_readability_updates, r=nikomatsakis
Increase spacing in error format for readability. Two small tweaks that seem to help readability quite a bit: * Add spacing header<->snippet, but use the |> on the side for visual consistency * Fix rust-lang#33819 * Fix rust-lang#33763 * Move format-sensitive test (issue-26480 in cfail) to ui test r? @nikomatsakis
2 parents 7d68b3d + df87a78 commit fe19b47

File tree

9 files changed

+117
-32
lines changed

9 files changed

+117
-32
lines changed

src/librustc_borrowck/borrowck/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
977977
if let Categorization::Local(local_id) = err.cmt.cat {
978978
let span = self.tcx.map.span(local_id);
979979
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
980-
if snippet != "self" {
980+
if snippet.starts_with("ref ") {
981+
db.span_label(span,
982+
&format!("use `{}` here to make mutable",
983+
snippet.replace("ref ", "ref mut ")));
984+
} else if snippet != "self" {
981985
db.span_label(span,
982986
&format!("use `mut {}` here to make mutable", snippet));
983987
}

src/libsyntax/errors/emitter.rs

+4
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ mod test {
682682
println!("r#\"\n{}\"#", str);
683683
assert_eq!(str, &r#"
684684
--> dummy.txt:11:1
685+
|>
685686
11 |> e-lä-vän
686687
|> ^
687688
"#[1..]);
@@ -746,6 +747,7 @@ mod test {
746747

747748
let expect_start = &r#"
748749
--> dummy.txt:1:6
750+
|>
749751
1 |> _____aaaaaa____bbbbbb__cccccdd_
750752
|> ^^^^^^ ^^^^^^ ^^^^^^^
751753
"#[1..];
@@ -818,6 +820,7 @@ mod test {
818820

819821
let expect0 = &r#"
820822
--> dummy.txt:5:1
823+
|>
821824
5 |> ccccc
822825
|> ^
823826
...
@@ -830,6 +833,7 @@ mod test {
830833

831834
let expect = &r#"
832835
--> dummy.txt:1:1
836+
|>
833837
1 |> aaaaa
834838
|> ^
835839
...

src/libsyntax/errors/snippet/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,13 @@ impl FileInfo {
478478
}],
479479
kind: RenderedLineKind::PrimaryFileName,
480480
});
481+
output.push(RenderedLine {
482+
text: vec![StyledString {
483+
text: "".to_string(),
484+
style: Style::FileNameStyle,
485+
}],
486+
kind: RenderedLineKind::Annotations,
487+
});
481488
}
482489
None => {
483490
output.push(RenderedLine {
@@ -487,6 +494,13 @@ impl FileInfo {
487494
}],
488495
kind: RenderedLineKind::OtherFileName,
489496
});
497+
output.push(RenderedLine {
498+
text: vec![StyledString {
499+
text: "".to_string(),
500+
style: Style::FileNameStyle,
501+
}],
502+
kind: RenderedLineKind::Annotations,
503+
});
490504
}
491505
}
492506
}

src/libsyntax/errors/snippet/test.rs

+14
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fn foo() {
9898
let text = make_string(&lines);
9999
assert_eq!(&text[..], &"
100100
--> foo.rs:3:2
101+
|>
101102
3 |> \tbar;
102103
|> \t^^^
103104
"[1..]);
@@ -130,6 +131,7 @@ fn foo() {
130131
println!("text=\n{}", text);
131132
assert_eq!(&text[..], &r#"
132133
::: foo.rs
134+
|>
133135
3 |> vec.push(vec.pop().unwrap());
134136
|> --- --- - previous borrow ends here
135137
|> | |
@@ -199,12 +201,14 @@ fn bar() {
199201
// Note that the `|>` remain aligned across both files:
200202
assert_eq!(&text[..], &r#"
201203
--> foo.rs:3:14
204+
|>
202205
3 |> vec.push(vec.pop().unwrap());
203206
|> --- ^^^ - c
204207
|> | |
205208
|> | b
206209
|> a
207210
::: bar.rs
211+
|>
208212
17 |> vec.push();
209213
|> --- - f
210214
|> |
@@ -249,6 +253,7 @@ fn foo() {
249253
println!("text=\n{}", text);
250254
assert_eq!(&text[..], &r#"
251255
::: foo.rs
256+
|>
252257
3 |> let name = find_id(&data, 22).unwrap();
253258
|> ---- immutable borrow begins here
254259
...
@@ -288,6 +293,7 @@ fn foo() {
288293
println!("text=r#\"\n{}\".trim_left()", text);
289294
assert_eq!(&text[..], &r#"
290295
::: foo.rs
296+
|>
291297
3 |> vec.push(vec.pop().unwrap());
292298
|> -------- ------ D
293299
|> ||
@@ -324,6 +330,7 @@ fn foo() {
324330
println!("text=r#\"\n{}\".trim_left()", text);
325331
assert_eq!(&text[..], &r#"
326332
::: foo.rs
333+
|>
327334
3 |> vec.push(vec.pop().unwrap());
328335
|> --- --- - previous borrow ends here
329336
|> | |
@@ -362,6 +369,7 @@ fn foo() {
362369
println!("text=r#\"\n{}\".trim_left()", text);
363370
assert_eq!(&text[..], &r#"
364371
::: foo.rs
372+
|>
365373
4 |> let mut vec2 = vec;
366374
|> --- `vec` moved here because it has type `collections::vec::Vec<i32>`
367375
...
@@ -398,6 +406,7 @@ fn foo() {
398406
println!("text=&r#\"\n{}\n\"#[1..]", text);
399407
assert_eq!(text, &r#"
400408
::: foo.rs
409+
|>
401410
3 |> let mut vec = vec![0, 1, 2];
402411
|> --- ---
403412
4 |> let mut vec2 = vec;
@@ -429,6 +438,7 @@ impl SomeTrait for () {
429438
println!("r#\"\n{}\"", text);
430439
assert_eq!(text, &r#"
431440
::: foo.rs
441+
|>
432442
3 |> fn foo(x: u32) {
433443
|> -
434444
"#[1..]);
@@ -458,6 +468,7 @@ fn span_overlap_label() {
458468
println!("r#\"\n{}\"", text);
459469
assert_eq!(text, &r#"
460470
::: foo.rs
471+
|>
461472
2 |> fn foo(x: u32) {
462473
|> --------------
463474
|> | |
@@ -492,6 +503,7 @@ fn span_overlap_label2() {
492503
println!("r#\"\n{}\"", text);
493504
assert_eq!(text, &r#"
494505
::: foo.rs
506+
|>
495507
2 |> fn foo(x: u32) {
496508
|> --------------
497509
|> | |
@@ -537,6 +549,7 @@ fn span_overlap_label3() {
537549
println!("r#\"\n{}\"", text);
538550
assert_eq!(text, &r#"
539551
::: foo.rs
552+
|>
540553
3 |> let closure = || {
541554
|> - foo
542555
4 |> inner
@@ -577,6 +590,7 @@ fn main() {
577590
println!("r#\"\n{}\"", text);
578591
assert_eq!(text, &r#"
579592
--> foo.rs:11:2
593+
|>
580594
11 |> }
581595
|> -
582596
"#[1..]);

src/test/compile-fail/issue-33819.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
fn main() {
11+
let mut op = Some(2);
12+
match op {
13+
Some(ref v) => { let a = &mut v; },
14+
//~^ ERROR:cannot borrow immutable
15+
//~| use `ref mut v` here to make mutable
16+
None => {},
17+
}
18+
}

src/test/run-make/unicode-input/span_length.rs

+44-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rand, core)]
11+
#![feature(rand)]
1212

1313
use std::fs::File;
1414
use std::io::prelude::*;
@@ -18,6 +18,11 @@ use std::process::Command;
1818
use std::__rand::{thread_rng, Rng};
1919
use std::{char, env};
2020

21+
pub fn check_old_skool() -> bool {
22+
use std::env;
23+
env::var("RUST_NEW_ERROR_FORMAT").is_err()
24+
}
25+
2126
// creates a file with `fn main() { <random ident> }` and checks the
2227
// compiler emits a span of the appropriate length (for the
2328
// "unresolved name" message); currently just using the number of code
@@ -65,10 +70,17 @@ fn main() {
6570

6671
let err = String::from_utf8_lossy(&result.stderr);
6772

68-
// the span should end the line (e.g no extra ~'s)
69-
let expected_span = format!("^{}\n", repeat("~").take(n - 1)
70-
.collect::<String>());
71-
assert!(err.contains(&expected_span));
73+
if check_old_skool() {
74+
// the span should end the line (e.g no extra ~'s)
75+
let expected_span = format!("^{}\n", repeat("~").take(n - 1)
76+
.collect::<String>());
77+
assert!(err.contains(&expected_span));
78+
} else {
79+
// the span should end the line (e.g no extra ~'s)
80+
let expected_span = format!("^{}\n", repeat("^").take(n - 1)
81+
.collect::<String>());
82+
assert!(err.contains(&expected_span));
83+
}
7284
}
7385

7486
// Test multi-column characters and tabs
@@ -77,9 +89,6 @@ fn main() {
7789
r#"extern "路濫狼á́́" fn foo() {{}} extern "路濫狼á́" fn bar() {{}}"#);
7890
}
7991

80-
// Extra characters. Every line is preceded by `filename:lineno <actual code>`
81-
let offset = main_file.to_str().unwrap().len() + 3;
82-
8392
let result = Command::new("sh")
8493
.arg("-c")
8594
.arg(format!("{} {}",
@@ -91,17 +100,31 @@ fn main() {
91100

92101
// Test both the length of the snake and the leading spaces up to it
93102

94-
// First snake is 8 ~s long, with 7 preceding spaces (excluding file name/line offset)
95-
let expected_span = format!("\n{}^{}\n",
96-
repeat(" ").take(offset + 7).collect::<String>(),
97-
repeat("~").take(8).collect::<String>());
98-
assert!(err.contains(&expected_span));
99-
// Second snake is only 7 ~s long, with 36 preceding spaces,
100-
// because rustc counts chars() now rather than width(). This
101-
// is because width() functions are to be removed from
102-
// librustc_unicode
103-
let expected_span = format!("\n{}^{}\n",
104-
repeat(" ").take(offset + 36).collect::<String>(),
105-
repeat("~").take(7).collect::<String>());
106-
assert!(err.contains(&expected_span));
103+
if check_old_skool() {
104+
// Extra characters. Every line is preceded by `filename:lineno <actual code>`
105+
let offset = main_file.to_str().unwrap().len() + 3;
106+
107+
// First snake is 8 ~s long, with 7 preceding spaces (excluding file name/line offset)
108+
let expected_span = format!("\n{}^{}\n",
109+
repeat(" ").take(offset + 7).collect::<String>(),
110+
repeat("~").take(8).collect::<String>());
111+
assert!(err.contains(&expected_span));
112+
// Second snake is only 7 ~s long, with 36 preceding spaces,
113+
// because rustc counts chars() now rather than width(). This
114+
// is because width() functions are to be removed from
115+
// librustc_unicode
116+
let expected_span = format!("\n{}^{}\n",
117+
repeat(" ").take(offset + 36).collect::<String>(),
118+
repeat("~").take(7).collect::<String>());
119+
assert!(err.contains(&expected_span));
120+
} else {
121+
let expected_span = format!("\n |>{}{}\n",
122+
repeat(" ").take(8).collect::<String>(),
123+
repeat("^").take(9).collect::<String>());
124+
assert!(err.contains(&expected_span));
125+
let expected_span = format!("\n |>{}{}\n",
126+
repeat(" ").take(37).collect::<String>(),
127+
repeat("^").take(8).collect::<String>());
128+
assert!(err.contains(&expected_span));
129+
}
107130
}

src/test/compile-fail/issue-26480.rs renamed to src/test/ui/mismatched_types/issue-26480.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// rustc-env:RUST_NEW_ERROR_FORMAT
1112
extern {
1213
fn write(fildes: i32, buf: *const i8, nbyte: u64) -> i64;
1314
}
@@ -24,25 +25,16 @@ macro_rules! write {
2425
unsafe {
2526
write(stdout, $arr.as_ptr() as *const i8,
2627
$arr.len() * size_of($arr[0]));
27-
//~^ ERROR mismatched types
28-
//~| expected u64, found usize
29-
//~| expected type
30-
//~| found type
3128
}
3229
}}
3330
}
3431

3532
macro_rules! cast {
36-
($x:expr) => ($x as ()) //~ ERROR non-scalar cast
33+
($x:expr) => ($x as ())
3734
}
3835

3936
fn main() {
4037
let hello = ['H', 'e', 'y'];
4138
write!(hello);
42-
//~^ NOTE in this expansion of write!
43-
//~| NOTE in this expansion of write!
44-
//~| NOTE in this expansion of write!
45-
4639
cast!(2);
47-
//~^ NOTE in this expansion of cast!
4840
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: mismatched types [--explain E0308]
2+
--> $DIR/issue-26480.rs:27:19
3+
|>
4+
27 |> $arr.len() * size_of($arr[0]));
5+
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u64, found usize
6+
$DIR/issue-26480.rs:38:5: 38:19: note: in this expansion of write! (defined in $DIR/issue-26480.rs)
7+
8+
error: non-scalar cast: `_` as `()`
9+
--> $DIR/issue-26480.rs:33:19
10+
|>
11+
33 |> ($x:expr) => ($x as ())
12+
|> ^^^^^^^^
13+
$DIR/issue-26480.rs:39:5: 39:14: note: in this expansion of cast! (defined in $DIR/issue-26480.rs)
14+
15+
error: aborting due to 2 previous errors

src/test/ui/mismatched_types/main.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
error: mismatched types [--explain E0308]
22
--> $DIR/main.rs:14:18
3+
|>
34
14 |> let x: u32 = (
45
|> ^ expected u32, found ()
56
note: expected type `u32`

0 commit comments

Comments
 (0)