Skip to content

Commit 0f34b53

Browse files
committed
Auto merge of #39765 - GuillaumeGomez:file-not-found-for-module-error, r=jseyfried
File not found for module error Fixes #39542. r? @jonathandturner Maybe you want to take a look @pnkfelix?
2 parents 8a1ce40 + b6818be commit 0f34b53

File tree

5 files changed

+193
-40
lines changed

5 files changed

+193
-40
lines changed

src/libsyntax/diagnostic_list.rs

+69
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,4 +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
295+
E0584, // file for module `..` found at both .. and ..
227296
}

src/libsyntax/parse/parser.rs

+82-40
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, ModulePathError>,
222+
pub result: Result<ModulePathSuccess, Error>,
223223
}
224224

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

236+
pub enum Error {
237+
FileNotFoundForModule {
238+
mod_name: String,
239+
default_path: String,
240+
secondary_path: String,
241+
dir_path: String,
242+
},
243+
DuplicatePaths {
244+
mod_name: String,
245+
default_path: String,
246+
secondary_path: String,
247+
},
248+
UselessDocComment,
249+
InclusiveRangeWithNoEnd,
250+
}
251+
252+
impl Error {
253+
pub fn span_err<'a>(self, sp: Span, handler: &'a errors::Handler) -> DiagnosticBuilder<'a> {
254+
match self {
255+
Error::FileNotFoundForModule { ref mod_name,
256+
ref default_path,
257+
ref secondary_path,
258+
ref dir_path } => {
259+
let mut err = struct_span_err!(handler, sp, E0583,
260+
"file not found for module `{}`", mod_name);
261+
err.help(&format!("name the file either {} or {} inside the directory {:?}",
262+
default_path,
263+
secondary_path,
264+
dir_path));
265+
err
266+
}
267+
Error::DuplicatePaths { ref mod_name, ref default_path, ref secondary_path } => {
268+
let mut err = struct_span_err!(handler, sp, E0584,
269+
"file for module `{}` found at both {} and {}",
270+
mod_name,
271+
default_path,
272+
secondary_path);
273+
err.help("delete or rename one of them to remove the ambiguity");
274+
err
275+
}
276+
Error::UselessDocComment => {
277+
let mut err = struct_span_err!(handler, sp, E0585,
278+
"found a documentation comment that doesn't document anything");
279+
err.help("doc comments must come before what they document, maybe a comment was \
280+
intended with `//`?");
281+
err
282+
}
283+
Error::InclusiveRangeWithNoEnd => {
284+
let mut err = struct_span_err!(handler, sp, E0586,
285+
"inclusive range with no end");
286+
err.help("inclusive ranges must be bounded at the end (`...b` or `a...b`)");
287+
err
288+
}
289+
}
290+
}
291+
}
292+
236293
pub enum LhsExpr {
237294
NotYetParsed,
238295
AttributesParsed(ThinVec<Attribute>),
@@ -461,10 +518,7 @@ impl<'a> Parser<'a> {
461518
}
462519
_ => {
463520
Err(if self.prev_token_kind == PrevTokenKind::DocComment {
464-
self.span_fatal_help(self.prev_span,
465-
"found a documentation comment that doesn't document anything",
466-
"doc comments must come before what they document, maybe a comment was \
467-
intended with `//`?")
521+
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
468522
} else {
469523
let mut err = self.fatal(&format!("expected identifier, found `{}`",
470524
self.this_token_to_string()));
@@ -955,6 +1009,9 @@ impl<'a> Parser<'a> {
9551009
pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> {
9561010
self.sess.span_diagnostic.struct_span_fatal(sp, m)
9571011
}
1012+
pub fn span_fatal_err(&self, sp: Span, err: Error) -> DiagnosticBuilder<'a> {
1013+
err.span_err(sp, self.diagnostic())
1014+
}
9581015
pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> {
9591016
let mut err = self.sess.span_diagnostic.struct_span_fatal(sp, m);
9601017
err.help(help);
@@ -1944,10 +2001,7 @@ impl<'a> Parser<'a> {
19442001
limits: RangeLimits)
19452002
-> PResult<'a, ast::ExprKind> {
19462003
if end.is_none() && limits == RangeLimits::Closed {
1947-
Err(self.span_fatal_help(self.span,
1948-
"inclusive range with no end",
1949-
"inclusive ranges must be bounded at the end \
1950-
(`...b` or `a...b`)"))
2004+
Err(self.span_fatal_err(self.span, Error::InclusiveRangeWithNoEnd))
19512005
} else {
19522006
Ok(ExprKind::Range(start, end, limits))
19532007
}
@@ -3862,10 +3916,7 @@ impl<'a> Parser<'a> {
38623916
let unused_attrs = |attrs: &[_], s: &mut Self| {
38633917
if attrs.len() > 0 {
38643918
if s.prev_token_kind == PrevTokenKind::DocComment {
3865-
s.span_err_help(s.prev_span,
3866-
"found a documentation comment that doesn't document anything",
3867-
"doc comments must come before what they document, maybe a \
3868-
comment was intended with `//`?");
3919+
s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit();
38693920
} else {
38703921
s.span_err(s.span, "expected statement after outer attribute");
38713922
}
@@ -4998,10 +5049,8 @@ impl<'a> Parser<'a> {
49985049
self.bump();
49995050
}
50005051
token::CloseDelim(token::Brace) => {}
5001-
token::DocComment(_) => return Err(self.span_fatal_help(self.span,
5002-
"found a documentation comment that doesn't document anything",
5003-
"doc comments must come before what they document, maybe a comment was \
5004-
intended with `//`?")),
5052+
token::DocComment(_) => return Err(self.span_fatal_err(self.span,
5053+
Error::UselessDocComment)),
50055054
_ => return Err(self.span_fatal_help(self.span,
50065055
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
50075056
"struct fields should be separated by commas")),
@@ -5162,8 +5211,7 @@ impl<'a> Parser<'a> {
51625211
}
51635212

51645213
/// Returns either a path to a module, or .
5165-
pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath
5166-
{
5214+
pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath {
51675215
let mod_name = id.to_string();
51685216
let default_path_str = format!("{}.rs", mod_name);
51695217
let secondary_path_str = format!("{}/mod.rs", mod_name);
@@ -5183,19 +5231,16 @@ impl<'a> Parser<'a> {
51835231
directory_ownership: DirectoryOwnership::Owned,
51845232
warn: false,
51855233
}),
5186-
(false, false) => Err(ModulePathError {
5187-
err_msg: format!("file not found for module `{}`", mod_name),
5188-
help_msg: format!("name the file either {} or {} inside the directory {:?}",
5189-
default_path_str,
5190-
secondary_path_str,
5191-
dir_path.display()),
5234+
(false, false) => Err(Error::FileNotFoundForModule {
5235+
mod_name: mod_name.clone(),
5236+
default_path: default_path_str,
5237+
secondary_path: secondary_path_str,
5238+
dir_path: format!("{}", dir_path.display()),
51925239
}),
5193-
(true, true) => Err(ModulePathError {
5194-
err_msg: format!("file for module `{}` found at both {} and {}",
5195-
mod_name,
5196-
default_path_str,
5197-
secondary_path_str),
5198-
help_msg: "delete or rename one of them to remove the ambiguity".to_owned(),
5240+
(true, true) => Err(Error::DuplicatePaths {
5241+
mod_name: mod_name.clone(),
5242+
default_path: default_path_str,
5243+
secondary_path: secondary_path_str,
51995244
}),
52005245
};
52015246

@@ -5232,7 +5277,7 @@ impl<'a> Parser<'a> {
52325277
paths.name);
52335278
err.span_note(id_sp, &msg);
52345279
}
5235-
return Err(err);
5280+
Err(err)
52365281
} else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership {
52375282
if warn {
52385283
if let Ok(result) = paths.result {
@@ -5254,15 +5299,12 @@ impl<'a> Parser<'a> {
52545299
&format!("... or maybe `use` the module `{}` instead \
52555300
of possibly redeclaring it",
52565301
paths.name));
5257-
return Err(err);
5302+
Err(err)
52585303
} else {
5259-
return Err(err);
5260-
};
5261-
}
5262-
5263-
match paths.result {
5264-
Ok(succ) => Ok(succ),
5265-
Err(err) => Err(self.span_fatal_help(id_sp, &err.err_msg, &err.help_msg)),
5304+
Err(err)
5305+
}
5306+
} else {
5307+
paths.result.map_err(|err| self.span_fatal_err(id_sp, err))
52665308
}
52675309
}
52685310

src/test/compile-fail/E0583.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 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+
mod module_that_doesnt_exist; //~ ERROR E0583
12+
13+
fn main() {
14+
}

src/test/compile-fail/E0585.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 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+
fn main() {
12+
/// Hello! I'm useless...
13+
//~^ ERROR E0585
14+
}

src/test/compile-fail/E0586.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 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+
fn main() {
12+
let tmp = vec![0, 1, 2, 3, 4, 4, 3, 3, 2, 1];
13+
let x = &tmp[1...]; //~ ERROR E0586
14+
}

0 commit comments

Comments
 (0)