Skip to content

Commit bb2168c

Browse files
committed
auto merge of #18840 : huonw/rust/tweaks, r=alexcrichton
Fix some old papercuts with diagnostics, e.g. tweaking spans, rewording messages. See individual commits.
2 parents 6f7081f + 661598c commit bb2168c

File tree

11 files changed

+129
-32
lines changed

11 files changed

+129
-32
lines changed

src/librustc/metadata/loader.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ impl<'a> Context<'a> {
307307
format!("found possibly newer version of crate `{}`",
308308
self.ident)
309309
} else if self.rejected_via_triple.len() > 0 {
310-
format!("found incorrect triple for crate `{}`", self.ident)
310+
format!("couldn't find crate `{}` with expected target triple {}",
311+
self.ident, self.triple)
311312
} else {
312313
format!("can't find crate for `{}`", self.ident)
313314
};
@@ -318,15 +319,12 @@ impl<'a> Context<'a> {
318319
};
319320
self.sess.span_err(self.span, message.as_slice());
320321

321-
let mismatches = self.rejected_via_triple.iter();
322322
if self.rejected_via_triple.len() > 0 {
323-
self.sess.span_note(self.span,
324-
format!("expected triple of {}",
325-
self.triple).as_slice());
323+
let mismatches = self.rejected_via_triple.iter();
326324
for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() {
327325
self.sess.fileline_note(self.span,
328-
format!("crate `{}` path {}{}, triple {}: {}",
329-
self.ident, "#", i+1, got, path.display()).as_slice());
326+
format!("crate `{}`, path #{}, triple {}: {}",
327+
self.ident, i+1, got, path.display()).as_slice());
330328
}
331329
}
332330
if self.rejected_via_hash.len() > 0 {

src/libsyntax/parse/lexer/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,13 @@ impl<'a> StringReader<'a> {
806806
if ascii_only { "unknown byte escape" }
807807
else { "unknown character escape" },
808808
c);
809+
if e == '\r' {
810+
let sp = codemap::mk_sp(escaped_pos, last_pos);
811+
self.span_diagnostic.span_help(
812+
sp,
813+
"this is an isolated carriage return; consider checking \
814+
your editor and version control settings.")
815+
}
809816
false
810817
}
811818
}

src/libsyntax/parse/parser.rs

+40-24
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use ast_util::{as_prec, ident_to_path, operator_prec};
6666
use ast_util;
6767
use codemap::{Span, BytePos, Spanned, spanned, mk_sp};
6868
use codemap;
69+
use diagnostic;
6970
use ext::tt::macro_parser;
7071
use parse;
7172
use parse::attr::ParserAttr;
@@ -941,6 +942,11 @@ impl<'a> Parser<'a> {
941942
pub fn span_fatal(&mut self, sp: Span, m: &str) -> ! {
942943
self.sess.span_diagnostic.span_fatal(sp, m)
943944
}
945+
pub fn span_fatal_help(&mut self, sp: Span, m: &str, help: &str) -> ! {
946+
self.span_err(sp, m);
947+
self.span_help(sp, help);
948+
panic!(diagnostic::FatalError);
949+
}
944950
pub fn span_note(&mut self, sp: Span, m: &str) {
945951
self.sess.span_diagnostic.span_note(sp, m)
946952
}
@@ -1641,7 +1647,8 @@ impl<'a> Parser<'a> {
16411647
token::LitByte(i) => LitByte(parse::byte_lit(i.as_str()).val0()),
16421648
token::LitChar(i) => LitChar(parse::char_lit(i.as_str()).val0()),
16431649
token::LitInteger(s) => parse::integer_lit(s.as_str(),
1644-
&self.sess.span_diagnostic, self.span),
1650+
&self.sess.span_diagnostic,
1651+
self.last_span),
16451652
token::LitFloat(s) => parse::float_lit(s.as_str()),
16461653
token::LitStr(s) => {
16471654
LitStr(token::intern_and_get_ident(parse::str_lit(s.as_str()).as_slice()),
@@ -3710,7 +3717,14 @@ impl<'a> Parser<'a> {
37103717
maybe_whole!(no_clone self, NtBlock);
37113718

37123719
let lo = self.span.lo;
3713-
self.expect(&token::OpenDelim(token::Brace));
3720+
3721+
if !self.eat(&token::OpenDelim(token::Brace)) {
3722+
let sp = self.span;
3723+
let tok = self.this_token_to_string();
3724+
self.span_fatal_help(sp,
3725+
format!("expected `{{`, found `{}`", tok).as_slice(),
3726+
"place this code inside a block");
3727+
}
37143728

37153729
return self.parse_block_tail_(lo, DefaultBlock, Vec::new());
37163730
}
@@ -4701,9 +4715,10 @@ impl<'a> Parser<'a> {
47014715
_ => {
47024716
let span = self.span;
47034717
let token_str = self.this_token_to_string();
4704-
self.span_fatal(span,
4705-
format!("expected `,`, or `}}`, found `{}`",
4706-
token_str).as_slice())
4718+
self.span_fatal_help(span,
4719+
format!("expected `,`, or `}}`, found `{}`",
4720+
token_str).as_slice(),
4721+
"struct fields should be separated by commas")
47074722
}
47084723
}
47094724
a_var
@@ -4905,19 +4920,24 @@ impl<'a> Parser<'a> {
49054920
(true, false) => (default_path, false),
49064921
(false, true) => (secondary_path, true),
49074922
(false, false) => {
4908-
self.span_fatal(id_sp,
4909-
format!("file not found for module \
4910-
`{}`",
4911-
mod_name).as_slice());
4923+
self.span_fatal_help(id_sp,
4924+
format!("file not found for module `{}`",
4925+
mod_name).as_slice(),
4926+
format!("name the file either {} or {} inside \
4927+
the directory {}",
4928+
default_path_str,
4929+
secondary_path_str,
4930+
dir_path.display()).as_slice());
49124931
}
49134932
(true, true) => {
4914-
self.span_fatal(
4933+
self.span_fatal_help(
49154934
id_sp,
49164935
format!("file for module `{}` found at both {} \
49174936
and {}",
49184937
mod_name,
49194938
default_path_str,
4920-
secondary_path_str).as_slice());
4939+
secondary_path_str).as_slice(),
4940+
"delete or rename one of them to remove the ambiguity");
49214941
}
49224942
}
49234943
}
@@ -5070,9 +5090,10 @@ impl<'a> Parser<'a> {
50705090
// skip the ident if there is one
50715091
if self.token.is_ident() { self.bump(); }
50725092

5073-
self.span_err(span,
5074-
format!("expected `;`, found `as`; perhaps you meant \
5075-
to enclose the crate name `{}` in a string?",
5093+
self.span_err(span, "expected `;`, found `as`");
5094+
self.span_help(span,
5095+
format!("perhaps you meant to enclose the crate name `{}` in \
5096+
a string?",
50765097
the_ident.as_str()).as_slice());
50775098
None
50785099
} else {
@@ -5582,16 +5603,12 @@ impl<'a> Parser<'a> {
55825603
}
55835604

55845605
// FAILURE TO PARSE ITEM
5585-
if visibility != Inherited {
5586-
let mut s = String::from_str("unmatched visibility `");
5587-
if visibility == Public {
5588-
s.push_str("pub")
5589-
} else {
5590-
s.push_str("priv")
5606+
match visibility {
5607+
Inherited => {}
5608+
Public => {
5609+
let last_span = self.last_span;
5610+
self.span_fatal(last_span, "unmatched visibility `pub`");
55915611
}
5592-
s.push('`');
5593-
let last_span = self.last_span;
5594-
self.span_fatal(last_span, s.as_slice());
55955612
}
55965613
return IoviNone(attrs);
55975614
}
@@ -5913,4 +5930,3 @@ impl<'a> Parser<'a> {
59135930
}
59145931
}
59155932
}
5916-

src/test/compile-fail/.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trailing-carriage-return-in-string.rs -text

src/test/compile-fail/extern-crate-as-no-string-help.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
// Tests that the proper help is displayed in the error message
1212

1313
extern crate foo as bar;
14-
//~^ ERROR expected `;`, found `as`; perhaps you meant to enclose the crate name `foo` in a string?
14+
//~^ ERROR expected `;`, found `as`
15+
//~^^ HELP perhaps you meant to enclose the crate name `foo` in a string?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 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+
11+
// issue #17123
12+
13+
fn main() {
14+
100000000000000000000000000000000 //~ ERROR int literal is too large
15+
16+
; // the span shouldn't point to this.
17+
}

src/test/compile-fail/mod_file_not_exist.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
mod not_a_real_file; //~ ERROR file not found for module `not_a_real_file`
12+
//~^ HELP name the file either not_a_real_file.rs or not_a_real_file/mod.rs inside the directory
1213

1314
fn main() {
1415
assert_eq!(mod_file_aux::bar(), 10);

src/test/compile-fail/trailing-carriage-return-in-string.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 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+
11+
// ignore-tidy-cr
12+
// Issue #11669
13+
14+
fn main() {
15+
// \r\n
16+
let ok = "This is \
17+
a test";
18+
// \r only
19+
let bad = "This is \ a test";
20+
//~^ ERROR unknown character escape: \r
21+
//~^^ HELP this is an isolated carriage return
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-include ../tools.mk
2+
3+
# Issue #10814
4+
#
5+
# these are no_std to avoid having to have the standard library or any
6+
# linkers/assemblers for the relevant platform
7+
8+
all:
9+
$(RUSTC) foo.rs --target=i686-unknown-linux-gnu
10+
$(RUSTC) bar.rs --target=x86_64-unknown-linux-gnu 2>&1 \
11+
| grep "couldn't find crate .foo. with expected target triple x86_64-unknown-linux-gnu"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 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+
#![no_std]
11+
extern crate foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 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+
#![no_std]
11+
#![crate_type = "lib"]

0 commit comments

Comments
 (0)