Skip to content

Commit 0c5d651

Browse files
committed
Auto merge of rust-lang#33688 - jonathandturner:fix_old_school, r=nikomatsakis
Fix for old school error issues, improvements to new school This PR: * Fixes some old school error issues, specifically rust-lang#33559, rust-lang#33543, rust-lang#33366 * Improves wording borrowck errors with match patterns * De-emphasize multi-line spans, so we don't color the single source character when we're trying to say "span starts here" * Rollup of rust-lang#33392 (which should help fix rust-lang#33390) r? @nikomatsakis
2 parents 9c6904c + b0a317d commit 0c5d651

File tree

13 files changed

+125
-68
lines changed

13 files changed

+125
-68
lines changed

src/librustc/infer/error_reporting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use std::cell::{Cell, RefCell};
9090
use std::char::from_u32;
9191
use std::fmt;
9292
use syntax::ast;
93-
use syntax::errors::DiagnosticBuilder;
93+
use syntax::errors::{DiagnosticBuilder, check_old_skool};
9494
use syntax::codemap::{self, Pos, Span};
9595
use syntax::parse::token;
9696
use syntax::ptr::P;
@@ -481,7 +481,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
481481
"{}",
482482
trace.origin);
483483

484-
if !is_simple_error {
484+
if !is_simple_error || check_old_skool() {
485485
err.note_expected_found(&"type", &expected, &found);
486486
}
487487

src/librustc_borrowck/borrowck/check_loans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
872872
&format!("borrow of `{}` occurs here",
873873
self.bccx.loan_path_to_string(loan_path)))
874874
.span_label(span,
875-
&format!("assignment to `{}` occurs here",
875+
&format!("assignment to borrowed `{}` occurs here",
876876
self.bccx.loan_path_to_string(loan_path)))
877877
.emit();
878878
}

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
126126
move_from.descriptive_string(bccx.tcx));
127127
err.span_label(
128128
move_from.span,
129-
&format!("move occurs here")
129+
&format!("cannot move out of {}", move_from.descriptive_string(bccx.tcx))
130130
);
131131
err
132132
}
@@ -138,7 +138,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
138138
"cannot move out of type `{}`, \
139139
a non-copy fixed-size array",
140140
b.ty);
141-
err.span_label(move_from.span, &format!("can not move out of here"));
141+
err.span_label(move_from.span, &format!("cannot move out of here"));
142142
err
143143
} else {
144144
span_bug!(move_from.span, "this path should not cause illegal move");
@@ -154,7 +154,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
154154
"cannot move out of type `{}`, \
155155
which defines the `Drop` trait",
156156
b.ty);
157-
err.span_label(move_from.span, &format!("can not move out of here"));
157+
err.span_label(move_from.span, &format!("cannot move out of here"));
158158
err
159159
},
160160
_ => {
@@ -175,16 +175,12 @@ fn note_move_destination(mut err: DiagnosticBuilder,
175175
if is_first_note {
176176
err.span_label(
177177
move_to_span,
178-
&format!("attempting to move value to here"));
179-
err.help(
180-
&format!("to prevent the move, \
181-
use `ref {0}` or `ref mut {0}` to capture value by \
182-
reference",
178+
&format!("hint: to prevent move, use `ref {0}` or `ref mut {0}`",
183179
pat_name));
184180
err
185181
} else {
186-
err.span_note(move_to_span,
187-
&format!("and here (use `ref {0}` or `ref mut {0}`)",
182+
err.span_label(move_to_span,
183+
&format!("...and here (use `ref {0}` or `ref mut {0}`)",
188184
pat_name));
189185
err
190186
}

src/libsyntax/errors/emitter.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ impl EmitterWriter {
367367
let mut output_vec = vec![];
368368

369369
for span_label in msp.span_labels() {
370-
let mut snippet_data = snippet_data.clone();
370+
let mut snippet_data = SnippetData::new(self.cm.clone(),
371+
Some(span_label.span));
371372

372373
snippet_data.push(span_label.span,
373374
span_label.is_primary,
@@ -524,6 +525,13 @@ impl Destination {
524525
}
525526
Style::Quotation => {
526527
}
528+
Style::OldSkoolNote => {
529+
self.start_attr(term::Attr::Bold)?;
530+
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_GREEN))?;
531+
}
532+
Style::OldSkoolNoteText => {
533+
self.start_attr(term::Attr::Bold)?;
534+
}
527535
Style::UnderlinePrimary | Style::LabelPrimary => {
528536
self.start_attr(term::Attr::Bold)?;
529537
self.start_attr(term::Attr::ForegroundColor(lvl.color()))?;

src/libsyntax/errors/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,13 @@ pub fn expect<T, M>(diag: &Handler, opt: Option<T>, msg: M) -> T where
699699
///
700700
/// FIXME(#33240)
701701
#[cfg(not(test))]
702-
fn check_old_skool() -> bool {
702+
pub fn check_old_skool() -> bool {
703703
use std::env;
704704
env::var("RUST_NEW_ERROR_FORMAT").is_err()
705705
}
706706

707707
/// For unit tests, use the new format.
708708
#[cfg(test)]
709-
fn check_old_skool() -> bool {
709+
pub fn check_old_skool() -> bool {
710710
false
711711
}

src/libsyntax/errors/snippet/mod.rs

+43-7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ struct Annotation {
5858
/// Is this annotation derived from primary span
5959
is_primary: bool,
6060

61+
/// Is this a large span minimized down to a smaller span
62+
is_minimized: bool,
63+
6164
/// Optional label to display adjacent to the annotation.
6265
label: Option<String>,
6366
}
@@ -90,6 +93,8 @@ pub enum Style {
9093
UnderlineSecondary,
9194
LabelPrimary,
9295
LabelSecondary,
96+
OldSkoolNoteText,
97+
OldSkoolNote,
9398
NoStyle,
9499
}
95100

@@ -382,10 +387,10 @@ impl FileInfo {
382387
// Basically, although this loses information, multi-line spans just
383388
// never look good.
384389

385-
let (line, start_col, mut end_col) = if lines.len() == 1 {
386-
(lines[0].line_index, lines[0].start_col, lines[0].end_col)
390+
let (line, start_col, mut end_col, is_minimized) = if lines.len() == 1 {
391+
(lines[0].line_index, lines[0].start_col, lines[0].end_col, false)
387392
} else {
388-
(lines[0].line_index, lines[0].start_col, CharPos(lines[0].start_col.0 + 1))
393+
(lines[0].line_index, lines[0].start_col, CharPos(lines[0].start_col.0 + 1), true)
389394
};
390395

391396
// Watch out for "empty spans". If we get a span like 6..6, we
@@ -401,6 +406,7 @@ impl FileInfo {
401406
self.lines[index].push_annotation(start_col,
402407
end_col,
403408
is_primary,
409+
is_minimized,
404410
label);
405411
}
406412

@@ -497,6 +503,30 @@ impl FileInfo {
497503
match self.primary_span {
498504
Some(span) => {
499505
let lo = codemap.lookup_char_pos(span.lo);
506+
let hi = codemap.lookup_char_pos(span.hi);
507+
//Before each secondary line in old skool-mode, print the label
508+
//as an old-style note
509+
if !line.annotations[0].is_primary {
510+
if let Some(ann) = line.annotations[0].label.clone() {
511+
output.push(RenderedLine {
512+
text: vec![StyledString {
513+
text: lo.file.name.clone(),
514+
style: Style::FileNameStyle,
515+
}, StyledString {
516+
text: format!(":{}:{}: {}:{} ", lo.line, lo.col.0 + 1,
517+
hi.line, hi.col.0+1),
518+
style: Style::LineAndColumn,
519+
}, StyledString {
520+
text: format!("note: "),
521+
style: Style::OldSkoolNote,
522+
}, StyledString {
523+
text: format!("{}", ann),
524+
style: Style::OldSkoolNoteText,
525+
}],
526+
kind: RenderedLineKind::Annotations,
527+
});
528+
}
529+
}
500530
rendered_lines[0].text.insert(0, StyledString {
501531
text: format!(":{} ", lo.line),
502532
style: Style::LineAndColumn,
@@ -598,15 +628,15 @@ impl FileInfo {
598628
if annotation.is_primary {
599629
Style::UnderlinePrimary
600630
} else {
601-
Style::UnderlineSecondary
631+
Style::OldSkoolNote
602632
});
603633
}
604634
else {
605635
styled_buffer.putc(1, p, '~',
606636
if annotation.is_primary {
607637
Style::UnderlinePrimary
608638
} else {
609-
Style::UnderlineSecondary
639+
Style::OldSkoolNote
610640
});
611641
}
612642
}
@@ -615,10 +645,14 @@ impl FileInfo {
615645
for p in annotation.start_col .. annotation.end_col {
616646
if annotation.is_primary {
617647
styled_buffer.putc(1, p, '^', Style::UnderlinePrimary);
618-
styled_buffer.set_style(0, p, Style::UnderlinePrimary);
648+
if !annotation.is_minimized {
649+
styled_buffer.set_style(0, p, Style::UnderlinePrimary);
650+
}
619651
} else {
620652
styled_buffer.putc(1, p, '-', Style::UnderlineSecondary);
621-
styled_buffer.set_style(0, p, Style::UnderlineSecondary);
653+
if !annotation.is_minimized {
654+
styled_buffer.set_style(0, p, Style::UnderlineSecondary);
655+
}
622656
}
623657
}
624658
}
@@ -819,11 +853,13 @@ impl Line {
819853
start: CharPos,
820854
end: CharPos,
821855
is_primary: bool,
856+
is_minimized: bool,
822857
label: Option<String>) {
823858
self.annotations.push(Annotation {
824859
start_col: start.0,
825860
end_col: end.0,
826861
is_primary: is_primary,
862+
is_minimized: is_minimized,
827863
label: label,
828864
});
829865
}

src/test/compile-fail/borrowck/borrowck-move-error-with-note.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ enum Foo {
1919
fn blah() {
2020
let f = &Foo::Foo1(box 1, box 2);
2121
match *f { //~ ERROR cannot move out of
22-
//~| move occurs here
23-
Foo::Foo1(num1, //~ NOTE attempting to move value to here
22+
//~| cannot move out
23+
Foo::Foo1(num1, //~ NOTE to prevent move
2424
num2) => (), //~ NOTE and here
2525
Foo::Foo2(num) => (), //~ NOTE and here
2626
Foo::Foo3 => ()
@@ -38,8 +38,8 @@ impl Drop for S {
3838
fn move_in_match() {
3939
match (S {f: "foo".to_string(), g: "bar".to_string()}) {
4040
S { //~ ERROR cannot move out of type `S`, which defines the `Drop` trait
41-
//~| can not move out of here
42-
f: _s, //~ NOTE attempting to move value to here
41+
//~| cannot move out of here
42+
f: _s, //~ NOTE to prevent move
4343
g: _t //~ NOTE and here
4444
} => {}
4545
}
@@ -55,8 +55,8 @@ fn free<T>(_: T) {}
5555
fn blah2() {
5656
let a = &A { a: box 1 };
5757
match a.a { //~ ERROR cannot move out of
58-
//~| move occurs here
59-
n => { //~ NOTE attempting to move value to here
58+
//~| cannot move out
59+
n => { //~ NOTE to prevent move
6060
free(n)
6161
}
6262
}

src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub fn main() {
2929
match tail {
3030
[Foo { string: a },
3131
//~^ ERROR cannot move out of borrowed content
32-
//~| move occurs here
33-
//~| attempting to move value to here
32+
//~| cannot move out
33+
//~| to prevent move
3434
Foo { string: b }] => {
3535
//~^ NOTE and here
3636
}

src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn a() {
1919
[box ref _a, _, _] => {
2020
//~^ borrow of `vec[..]` occurs here
2121
vec[0] = box 4; //~ ERROR cannot assign
22-
//~^ assignment to `vec[..]` occurs here
22+
//~^ assignment to borrowed `vec[..]` occurs here
2323
}
2424
}
2525
}
@@ -31,7 +31,7 @@ fn b() {
3131
[_b..] => {
3232
//~^ borrow of `vec[..]` occurs here
3333
vec[0] = box 4; //~ ERROR cannot assign
34-
//~^ assignment to `vec[..]` occurs here
34+
//~^ assignment to borrowed `vec[..]` occurs here
3535
}
3636
}
3737
}
@@ -41,8 +41,8 @@ fn c() {
4141
let vec: &mut [Box<isize>] = &mut vec;
4242
match vec {
4343
[_a, //~ ERROR cannot move out
44-
//~| move occurs here
45-
//~| attempting to move value to here
44+
//~| cannot move out
45+
//~| to prevent move
4646
_b..] => {
4747
// Note: `_a` is *moved* here, but `b` is borrowing,
4848
// hence illegal.
@@ -53,38 +53,38 @@ fn c() {
5353
_ => {}
5454
}
5555
let a = vec[0]; //~ ERROR cannot move out
56-
//~^ NOTE attempting to move value to here
57-
//~| can not move out of here
56+
//~^ NOTE to prevent move
57+
//~| cannot move out of here
5858
}
5959

6060
fn d() {
6161
let mut vec = vec!(box 1, box 2, box 3);
6262
let vec: &mut [Box<isize>] = &mut vec;
6363
match vec {
6464
[_a.., //~ ERROR cannot move out
65-
//~^ move occurs here
66-
_b] => {} //~ NOTE attempting to move value to here
65+
//~^ cannot move out
66+
_b] => {} //~ NOTE to prevent move
6767
_ => {}
6868
}
6969
let a = vec[0]; //~ ERROR cannot move out
70-
//~^ NOTE attempting to move value to here
71-
//~| can not move out of here
70+
//~^ NOTE to prevent move
71+
//~| cannot move out of here
7272
}
7373

7474
fn e() {
7575
let mut vec = vec!(box 1, box 2, box 3);
7676
let vec: &mut [Box<isize>] = &mut vec;
7777
match vec {
7878
[_a, _b, _c] => {} //~ ERROR cannot move out
79-
//~| move occurs here
80-
//~| NOTE attempting to move value to here
79+
//~| cannot move out
80+
//~| NOTE to prevent move
8181
//~| NOTE and here
8282
//~| NOTE and here
8383
_ => {}
8484
}
8585
let a = vec[0]; //~ ERROR cannot move out
86-
//~^ NOTE attempting to move value to here
87-
//~| can not move out of here
86+
//~^ NOTE to prevent move
87+
//~| cannot move out of here
8888
}
8989

9090
fn main() {}

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

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ macro_rules! write {
2626
$arr.len() * size_of($arr[0]));
2727
//~^ ERROR mismatched types
2828
//~| expected u64, found usize
29+
//~| expected type
30+
//~| found type
2931
}
3032
}}
3133
}
@@ -38,6 +40,8 @@ fn main() {
3840
let hello = ['H', 'e', 'y'];
3941
write!(hello);
4042
//~^ NOTE in this expansion of write!
43+
//~| NOTE in this expansion of write!
44+
//~| NOTE in this expansion of write!
4145

4246
cast!(2);
4347
//~^ NOTE in this expansion of cast!

src/test/compile-fail/moves-based-on-type-block-bad.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ fn main() {
3232
loop {
3333
f(&s, |hellothere| {
3434
match hellothere.x { //~ ERROR cannot move out
35-
//~| move occurs here
35+
//~| cannot move out of borrowed content
3636
box E::Foo(_) => {}
37-
box E::Bar(x) => println!("{}", x.to_string()), //~ NOTE attempting to move value to here
37+
box E::Bar(x) => println!("{}", x.to_string()),
38+
//~^ NOTE to prevent move
3839
box E::Baz => {}
3940
}
4041
})

0 commit comments

Comments
 (0)