@@ -219,7 +219,7 @@ fn is_ident_or_underscore(t: &token::Token) -> bool {
219219pub 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
225225pub 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+
236293pub 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
0 commit comments