From 175ecfefd50932c0ae8fa328579e26baadd6a110 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 16 May 2016 18:40:50 -0400 Subject: [PATCH] Improve a few errors and fix #33366 --- src/librustc/infer/error_reporting.rs | 4 +- src/librustc_borrowck/borrowck/check_loans.rs | 2 +- .../borrowck/gather_loans/move_error.rs | 16 +++----- src/libsyntax/errors/mod.rs | 4 +- .../borrowck/borrowck-move-error-with-note.rs | 12 +++--- .../borrowck/borrowck-move-out-of-vec-tail.rs | 4 +- .../borrowck/borrowck-vec-pattern-nesting.rs | 28 ++++++------- src/test/compile-fail/issue-26480.rs | 4 ++ .../moves-based-on-type-block-bad.rs | 5 ++- src/tools/compiletest/src/json.rs | 6 +-- src/tools/compiletest/src/runtest.rs | 41 +++++++++++-------- 11 files changed, 68 insertions(+), 58 deletions(-) diff --git a/src/librustc/infer/error_reporting.rs b/src/librustc/infer/error_reporting.rs index 950b7f68ed87..d2c2ecf24c56 100644 --- a/src/librustc/infer/error_reporting.rs +++ b/src/librustc/infer/error_reporting.rs @@ -90,7 +90,7 @@ use std::cell::{Cell, RefCell}; use std::char::from_u32; use std::fmt; use syntax::ast; -use syntax::errors::DiagnosticBuilder; +use syntax::errors::{DiagnosticBuilder, check_old_skool}; use syntax::codemap::{self, Pos, Span}; use syntax::parse::token; use syntax::ptr::P; @@ -481,7 +481,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { "{}", trace.origin); - if !is_simple_error { + if !is_simple_error || check_old_skool() { err.note_expected_found(&"type", &expected, &found); } diff --git a/src/librustc_borrowck/borrowck/check_loans.rs b/src/librustc_borrowck/borrowck/check_loans.rs index fcfab96b9dfe..36f95f62d060 100644 --- a/src/librustc_borrowck/borrowck/check_loans.rs +++ b/src/librustc_borrowck/borrowck/check_loans.rs @@ -872,7 +872,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> { &format!("borrow of `{}` occurs here", self.bccx.loan_path_to_string(loan_path))) .span_label(span, - &format!("assignment to `{}` occurs here", + &format!("assignment to borrowed `{}` occurs here", self.bccx.loan_path_to_string(loan_path))) .emit(); } diff --git a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs index cdc68edbf300..5ebb1ab32b8f 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs @@ -126,7 +126,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, move_from.descriptive_string(bccx.tcx)); err.span_label( move_from.span, - &format!("move occurs here") + &format!("cannot move out of {}", move_from.descriptive_string(bccx.tcx)) ); err } @@ -138,7 +138,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, "cannot move out of type `{}`, \ a non-copy fixed-size array", b.ty); - err.span_label(move_from.span, &format!("can not move out of here")); + err.span_label(move_from.span, &format!("cannot move out of here")); err } else { 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>, "cannot move out of type `{}`, \ which defines the `Drop` trait", b.ty); - err.span_label(move_from.span, &format!("can not move out of here")); + err.span_label(move_from.span, &format!("cannot move out of here")); err }, _ => { @@ -175,16 +175,12 @@ fn note_move_destination(mut err: DiagnosticBuilder, if is_first_note { err.span_label( move_to_span, - &format!("attempting to move value to here")); - err.help( - &format!("to prevent the move, \ - use `ref {0}` or `ref mut {0}` to capture value by \ - reference", + &format!("hint: to prevent move, use `ref {0}` or `ref mut {0}`", pat_name)); err } else { - err.span_note(move_to_span, - &format!("and here (use `ref {0}` or `ref mut {0}`)", + err.span_label(move_to_span, + &format!("...and here (use `ref {0}` or `ref mut {0}`)", pat_name)); err } diff --git a/src/libsyntax/errors/mod.rs b/src/libsyntax/errors/mod.rs index 7592214c0ab9..b541a6ccb8d8 100644 --- a/src/libsyntax/errors/mod.rs +++ b/src/libsyntax/errors/mod.rs @@ -699,13 +699,13 @@ pub fn expect(diag: &Handler, opt: Option, msg: M) -> T where /// /// FIXME(#33240) #[cfg(not(test))] -fn check_old_skool() -> bool { +pub fn check_old_skool() -> bool { use std::env; env::var("RUST_NEW_ERROR_FORMAT").is_err() } /// For unit tests, use the new format. #[cfg(test)] -fn check_old_skool() -> bool { +pub fn check_old_skool() -> bool { false } diff --git a/src/test/compile-fail/borrowck/borrowck-move-error-with-note.rs b/src/test/compile-fail/borrowck/borrowck-move-error-with-note.rs index ffa7d1925569..438a548819bc 100644 --- a/src/test/compile-fail/borrowck/borrowck-move-error-with-note.rs +++ b/src/test/compile-fail/borrowck/borrowck-move-error-with-note.rs @@ -19,8 +19,8 @@ enum Foo { fn blah() { let f = &Foo::Foo1(box 1, box 2); match *f { //~ ERROR cannot move out of - //~| move occurs here - Foo::Foo1(num1, //~ NOTE attempting to move value to here + //~| cannot move out + Foo::Foo1(num1, //~ NOTE to prevent move num2) => (), //~ NOTE and here Foo::Foo2(num) => (), //~ NOTE and here Foo::Foo3 => () @@ -38,8 +38,8 @@ impl Drop for S { fn move_in_match() { match (S {f: "foo".to_string(), g: "bar".to_string()}) { S { //~ ERROR cannot move out of type `S`, which defines the `Drop` trait - //~| can not move out of here - f: _s, //~ NOTE attempting to move value to here + //~| cannot move out of here + f: _s, //~ NOTE to prevent move g: _t //~ NOTE and here } => {} } @@ -55,8 +55,8 @@ fn free(_: T) {} fn blah2() { let a = &A { a: box 1 }; match a.a { //~ ERROR cannot move out of - //~| move occurs here - n => { //~ NOTE attempting to move value to here + //~| cannot move out + n => { //~ NOTE to prevent move free(n) } } diff --git a/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs b/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs index 2f1c69d0d7da..15771295743c 100644 --- a/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs +++ b/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs @@ -29,8 +29,8 @@ pub fn main() { match tail { [Foo { string: a }, //~^ ERROR cannot move out of borrowed content - //~| move occurs here - //~| attempting to move value to here + //~| cannot move out + //~| to prevent move Foo { string: b }] => { //~^ NOTE and here } diff --git a/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs b/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs index 1c63b458e62e..eec6c8473eb3 100644 --- a/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs +++ b/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs @@ -19,7 +19,7 @@ fn a() { [box ref _a, _, _] => { //~^ borrow of `vec[..]` occurs here vec[0] = box 4; //~ ERROR cannot assign - //~^ assignment to `vec[..]` occurs here + //~^ assignment to borrowed `vec[..]` occurs here } } } @@ -31,7 +31,7 @@ fn b() { [_b..] => { //~^ borrow of `vec[..]` occurs here vec[0] = box 4; //~ ERROR cannot assign - //~^ assignment to `vec[..]` occurs here + //~^ assignment to borrowed `vec[..]` occurs here } } } @@ -41,8 +41,8 @@ fn c() { let vec: &mut [Box] = &mut vec; match vec { [_a, //~ ERROR cannot move out - //~| move occurs here - //~| attempting to move value to here + //~| cannot move out + //~| to prevent move _b..] => { // Note: `_a` is *moved* here, but `b` is borrowing, // hence illegal. @@ -53,8 +53,8 @@ fn c() { _ => {} } let a = vec[0]; //~ ERROR cannot move out - //~^ NOTE attempting to move value to here - //~| can not move out of here + //~^ NOTE to prevent move + //~| cannot move out of here } fn d() { @@ -62,13 +62,13 @@ fn d() { let vec: &mut [Box] = &mut vec; match vec { [_a.., //~ ERROR cannot move out - //~^ move occurs here - _b] => {} //~ NOTE attempting to move value to here + //~^ cannot move out + _b] => {} //~ NOTE to prevent move _ => {} } let a = vec[0]; //~ ERROR cannot move out - //~^ NOTE attempting to move value to here - //~| can not move out of here + //~^ NOTE to prevent move + //~| cannot move out of here } fn e() { @@ -76,15 +76,15 @@ fn e() { let vec: &mut [Box] = &mut vec; match vec { [_a, _b, _c] => {} //~ ERROR cannot move out - //~| move occurs here - //~| NOTE attempting to move value to here + //~| cannot move out + //~| NOTE to prevent move //~| NOTE and here //~| NOTE and here _ => {} } let a = vec[0]; //~ ERROR cannot move out - //~^ NOTE attempting to move value to here - //~| can not move out of here + //~^ NOTE to prevent move + //~| cannot move out of here } fn main() {} diff --git a/src/test/compile-fail/issue-26480.rs b/src/test/compile-fail/issue-26480.rs index adcf8484f782..634a4014e118 100644 --- a/src/test/compile-fail/issue-26480.rs +++ b/src/test/compile-fail/issue-26480.rs @@ -26,6 +26,8 @@ macro_rules! write { $arr.len() * size_of($arr[0])); //~^ ERROR mismatched types //~| expected u64, found usize + //~| expected type + //~| found type } }} } @@ -38,6 +40,8 @@ fn main() { let hello = ['H', 'e', 'y']; write!(hello); //~^ NOTE in this expansion of write! + //~| NOTE in this expansion of write! + //~| NOTE in this expansion of write! cast!(2); //~^ NOTE in this expansion of cast! diff --git a/src/test/compile-fail/moves-based-on-type-block-bad.rs b/src/test/compile-fail/moves-based-on-type-block-bad.rs index 1fd69e2dbfe7..ea6afcd62141 100644 --- a/src/test/compile-fail/moves-based-on-type-block-bad.rs +++ b/src/test/compile-fail/moves-based-on-type-block-bad.rs @@ -32,9 +32,10 @@ fn main() { loop { f(&s, |hellothere| { match hellothere.x { //~ ERROR cannot move out - //~| move occurs here + //~| cannot move out of borrowed content box E::Foo(_) => {} - box E::Bar(x) => println!("{}", x.to_string()), //~ NOTE attempting to move value to here + box E::Bar(x) => println!("{}", x.to_string()), + //~^ NOTE to prevent move box E::Baz => {} } }) diff --git a/src/tools/compiletest/src/json.rs b/src/tools/compiletest/src/json.rs index 665f304d69fa..84b78547ab9a 100644 --- a/src/tools/compiletest/src/json.rs +++ b/src/tools/compiletest/src/json.rs @@ -12,7 +12,7 @@ use errors::{Error, ErrorKind}; use rustc_serialize::json; use std::str::FromStr; use std::path::Path; -use runtest::{fatal_proc_rec, ProcRes}; +use runtest::{ProcRes}; // These structs are a subset of the ones found in // `syntax::errors::json`. @@ -73,9 +73,9 @@ fn parse_line(file_name: &str, line: &str, output: &str, proc_res: &ProcRes) -> expected_errors } Err(error) => { - fatal_proc_rec(None, &format!( + proc_res.fatal(Some(&format!( "failed to decode compiler output as json: `{}`\noutput: {}\nline: {}", - error, line, output), proc_res); + error, line, output))); } } } else { diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 208057a423b2..280c87a28746 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1528,23 +1528,9 @@ actual:\n\ self.error(err); panic!(); } - pub fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! { + fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! { self.error(err); - print!("\ - status: {}\n\ - command: {}\n\ - stdout:\n\ - ------------------------------------------\n\ - {}\n\ - ------------------------------------------\n\ - stderr:\n\ - ------------------------------------------\n\ - {}\n\ - ------------------------------------------\n\ - \n", - proc_res.status, proc_res.cmdline, proc_res.stdout, - proc_res.stderr); - panic!(); + proc_res.fatal(None); } fn _arm_exec_compiled_test(&self, env: Vec<(String, String)>) -> ProcRes { @@ -2209,6 +2195,29 @@ enum Status { Normal(ExitStatus), } +impl ProcRes { + pub fn fatal(&self, err: Option<&str>) -> ! { + if let Some(e) = err { + println!("\nerror: {}", e); + } + print!("\ + status: {}\n\ + command: {}\n\ + stdout:\n\ + ------------------------------------------\n\ + {}\n\ + ------------------------------------------\n\ + stderr:\n\ + ------------------------------------------\n\ + {}\n\ + ------------------------------------------\n\ + \n", + self.status, self.cmdline, self.stdout, + self.stderr); + panic!(); + } +} + impl Status { fn code(&self) -> Option { match *self {