Skip to content

Commit b6818be

Browse files
Add long error explanations
1 parent 0a45dd7 commit b6818be

File tree

2 files changed

+85
-20
lines changed

2 files changed

+85
-20
lines changed

src/libsyntax/diagnostic_list.rs

+68-3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,74 @@ where appropriate is ongoing. Try using an unquoted name instead:
201201
pub fn something() {}
202202
```
203203
"##,
204+
205+
E0583: r##"
206+
A file wasn't found for an out-of-line module.
207+
208+
Erroneous code example:
209+
210+
```compile_fail,E0583
211+
mod file_that_doesnt_exist; // error: file not found for module
212+
213+
fn main() {}
214+
```
215+
216+
Please be sure that a file corresponding to the module exists. If you
217+
want to use a module named `file_that_doesnt_exist`, you need to have a file
218+
named `file_that_doesnt_exist.rs` or `file_that_doesnt_exist/mod.rs` in the
219+
same directory.
220+
"##,
221+
222+
E0585: r##"
223+
A documentation comment that doesn't document anything was found.
224+
225+
Erroneous code example:
226+
227+
```compile_fail,E0585
228+
fn main() {
229+
// The following doc comment will fail:
230+
/// This is a useless doc comment!
231+
}
232+
```
233+
234+
Documentation comments need to be followed by items, including functions,
235+
types, modules, etc. Examples:
236+
237+
```
238+
/// I'm documenting the following struct:
239+
struct Foo;
240+
241+
/// I'm documenting the following function:
242+
fn foo() {}
243+
```
244+
"##,
245+
246+
E0586: r##"
247+
An inclusive range was used with no end.
248+
249+
Erroneous code example:
250+
251+
```compile_fail,E0586
252+
let tmp = vec![0, 1, 2, 3, 4, 4, 3, 3, 2, 1];
253+
let x = &tmp[1...]; // error: inclusive range was used with no end
254+
```
255+
256+
An inclusive range needs an end in order to *include* it. If you just need a
257+
start and no end, use a non-inclusive range (with `..`):
258+
259+
```
260+
let tmp = vec![0, 1, 2, 3, 4, 4, 3, 3, 2, 1];
261+
let x = &tmp[1..]; // ok!
262+
```
263+
264+
Or put an end to your inclusive range:
265+
266+
```
267+
let tmp = vec![0, 1, 2, 3, 4, 4, 3, 3, 2, 1];
268+
let x = &tmp[1...3]; // ok!
269+
```
270+
"##,
271+
204272
}
205273

206274
register_diagnostics! {
@@ -224,8 +292,5 @@ register_diagnostics! {
224292
E0555, // malformed feature attribute, expected #![feature(...)]
225293
E0556, // malformed feature, expected just one word
226294
E0557, // feature has been removed
227-
E0583, // file not found for module
228295
E0584, // file for module `..` found at both .. and ..
229-
E0585, // documentation comment that doesn't document anything
230-
E0586, // inclusive range with no end
231296
}

src/libsyntax/parse/parser.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn is_ident_or_underscore(t: &token::Token) -> bool {
219219
pub struct ModulePath {
220220
pub name: String,
221221
pub path_exists: bool,
222-
pub result: Result<ModulePathSuccess, Errors>,
222+
pub result: Result<ModulePathSuccess, Error>,
223223
}
224224

225225
pub struct ModulePathSuccess {
@@ -233,7 +233,7 @@ pub struct ModulePathError {
233233
pub help_msg: String,
234234
}
235235

236-
pub enum Errors {
236+
pub enum Error {
237237
FileNotFoundForModule {
238238
mod_name: String,
239239
default_path: String,
@@ -249,13 +249,13 @@ pub enum Errors {
249249
InclusiveRangeWithNoEnd,
250250
}
251251

252-
impl Errors {
252+
impl Error {
253253
pub fn span_err<'a>(self, sp: Span, handler: &'a errors::Handler) -> DiagnosticBuilder<'a> {
254254
match self {
255-
Errors::FileNotFoundForModule { ref mod_name,
256-
ref default_path,
257-
ref secondary_path,
258-
ref dir_path } => {
255+
Error::FileNotFoundForModule { ref mod_name,
256+
ref default_path,
257+
ref secondary_path,
258+
ref dir_path } => {
259259
let mut err = struct_span_err!(handler, sp, E0583,
260260
"file not found for module `{}`", mod_name);
261261
err.help(&format!("name the file either {} or {} inside the directory {:?}",
@@ -264,7 +264,7 @@ impl Errors {
264264
dir_path));
265265
err
266266
}
267-
Errors::DuplicatePaths { ref mod_name, ref default_path, ref secondary_path } => {
267+
Error::DuplicatePaths { ref mod_name, ref default_path, ref secondary_path } => {
268268
let mut err = struct_span_err!(handler, sp, E0584,
269269
"file for module `{}` found at both {} and {}",
270270
mod_name,
@@ -273,14 +273,14 @@ impl Errors {
273273
err.help("delete or rename one of them to remove the ambiguity");
274274
err
275275
}
276-
Errors::UselessDocComment => {
276+
Error::UselessDocComment => {
277277
let mut err = struct_span_err!(handler, sp, E0585,
278278
"found a documentation comment that doesn't document anything");
279279
err.help("doc comments must come before what they document, maybe a comment was \
280280
intended with `//`?");
281281
err
282282
}
283-
Errors::InclusiveRangeWithNoEnd => {
283+
Error::InclusiveRangeWithNoEnd => {
284284
let mut err = struct_span_err!(handler, sp, E0586,
285285
"inclusive range with no end");
286286
err.help("inclusive ranges must be bounded at the end (`...b` or `a...b`)");
@@ -518,7 +518,7 @@ impl<'a> Parser<'a> {
518518
}
519519
_ => {
520520
Err(if self.prev_token_kind == PrevTokenKind::DocComment {
521-
self.span_fatal_err(self.prev_span, Errors::UselessDocComment)
521+
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
522522
} else {
523523
let mut err = self.fatal(&format!("expected identifier, found `{}`",
524524
self.this_token_to_string()));
@@ -1009,7 +1009,7 @@ impl<'a> Parser<'a> {
10091009
pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> {
10101010
self.sess.span_diagnostic.struct_span_fatal(sp, m)
10111011
}
1012-
pub fn span_fatal_err(&self, sp: Span, err: Errors) -> DiagnosticBuilder<'a> {
1012+
pub fn span_fatal_err(&self, sp: Span, err: Error) -> DiagnosticBuilder<'a> {
10131013
err.span_err(sp, self.diagnostic())
10141014
}
10151015
pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> {
@@ -2001,7 +2001,7 @@ impl<'a> Parser<'a> {
20012001
limits: RangeLimits)
20022002
-> PResult<'a, ast::ExprKind> {
20032003
if end.is_none() && limits == RangeLimits::Closed {
2004-
Err(self.span_fatal_err(self.span, Errors::InclusiveRangeWithNoEnd))
2004+
Err(self.span_fatal_err(self.span, Error::InclusiveRangeWithNoEnd))
20052005
} else {
20062006
Ok(ExprKind::Range(start, end, limits))
20072007
}
@@ -3916,7 +3916,7 @@ impl<'a> Parser<'a> {
39163916
let unused_attrs = |attrs: &[_], s: &mut Self| {
39173917
if attrs.len() > 0 {
39183918
if s.prev_token_kind == PrevTokenKind::DocComment {
3919-
self.span_fatal_err(s.prev_span, Errors::UselessDocComment).emit();
3919+
s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit();
39203920
} else {
39213921
s.span_err(s.span, "expected statement after outer attribute");
39223922
}
@@ -5050,7 +5050,7 @@ impl<'a> Parser<'a> {
50505050
}
50515051
token::CloseDelim(token::Brace) => {}
50525052
token::DocComment(_) => return Err(self.span_fatal_err(self.span,
5053-
Errors::UselessDocComment)),
5053+
Error::UselessDocComment)),
50545054
_ => return Err(self.span_fatal_help(self.span,
50555055
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
50565056
"struct fields should be separated by commas")),
@@ -5231,13 +5231,13 @@ impl<'a> Parser<'a> {
52315231
directory_ownership: DirectoryOwnership::Owned,
52325232
warn: false,
52335233
}),
5234-
(false, false) => Err(Errors::FileNotFoundForModule {
5234+
(false, false) => Err(Error::FileNotFoundForModule {
52355235
mod_name: mod_name.clone(),
52365236
default_path: default_path_str,
52375237
secondary_path: secondary_path_str,
52385238
dir_path: format!("{}", dir_path.display()),
52395239
}),
5240-
(true, true) => Err(Errors::DuplicatePaths {
5240+
(true, true) => Err(Error::DuplicatePaths {
52415241
mod_name: mod_name.clone(),
52425242
default_path: default_path_str,
52435243
secondary_path: secondary_path_str,

0 commit comments

Comments
 (0)