@@ -219,7 +219,7 @@ fn is_ident_or_underscore(t: &token::Token) -> bool {
219
219
pub struct ModulePath {
220
220
pub name : String ,
221
221
pub path_exists : bool ,
222
- pub result : Result < ModulePathSuccess , ModulePathError > ,
222
+ pub result : Result < ModulePathSuccess , Error > ,
223
223
}
224
224
225
225
pub struct ModulePathSuccess {
@@ -233,6 +233,63 @@ pub struct ModulePathError {
233
233
pub help_msg : String ,
234
234
}
235
235
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
+
236
293
pub enum LhsExpr {
237
294
NotYetParsed ,
238
295
AttributesParsed ( ThinVec < Attribute > ) ,
@@ -461,10 +518,7 @@ impl<'a> Parser<'a> {
461
518
}
462
519
_ => {
463
520
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 )
468
522
} else {
469
523
let mut err = self . fatal ( & format ! ( "expected identifier, found `{}`" ,
470
524
self . this_token_to_string( ) ) ) ;
@@ -955,6 +1009,9 @@ impl<'a> Parser<'a> {
955
1009
pub fn span_fatal ( & self , sp : Span , m : & str ) -> DiagnosticBuilder < ' a > {
956
1010
self . sess . span_diagnostic . struct_span_fatal ( sp, m)
957
1011
}
1012
+ pub fn span_fatal_err ( & self , sp : Span , err : Error ) -> DiagnosticBuilder < ' a > {
1013
+ err. span_err ( sp, self . diagnostic ( ) )
1014
+ }
958
1015
pub fn span_fatal_help ( & self , sp : Span , m : & str , help : & str ) -> DiagnosticBuilder < ' a > {
959
1016
let mut err = self . sess . span_diagnostic . struct_span_fatal ( sp, m) ;
960
1017
err. help ( help) ;
@@ -1944,10 +2001,7 @@ impl<'a> Parser<'a> {
1944
2001
limits : RangeLimits )
1945
2002
-> PResult < ' a , ast:: ExprKind > {
1946
2003
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 ) )
1951
2005
} else {
1952
2006
Ok ( ExprKind :: Range ( start, end, limits) )
1953
2007
}
@@ -3862,10 +3916,7 @@ impl<'a> Parser<'a> {
3862
3916
let unused_attrs = |attrs : & [ _ ] , s : & mut Self | {
3863
3917
if attrs. len ( ) > 0 {
3864
3918
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 ( ) ;
3869
3920
} else {
3870
3921
s. span_err ( s. span , "expected statement after outer attribute" ) ;
3871
3922
}
@@ -4998,10 +5049,8 @@ impl<'a> Parser<'a> {
4998
5049
self . bump ( ) ;
4999
5050
}
5000
5051
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 ) ) ,
5005
5054
_ => return Err ( self . span_fatal_help ( self . span ,
5006
5055
& format ! ( "expected `,`, or `}}`, found `{}`" , self . this_token_to_string( ) ) ,
5007
5056
"struct fields should be separated by commas" ) ) ,
@@ -5162,8 +5211,7 @@ impl<'a> Parser<'a> {
5162
5211
}
5163
5212
5164
5213
/// 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 {
5167
5215
let mod_name = id. to_string ( ) ;
5168
5216
let default_path_str = format ! ( "{}.rs" , mod_name) ;
5169
5217
let secondary_path_str = format ! ( "{}/mod.rs" , mod_name) ;
@@ -5183,19 +5231,16 @@ impl<'a> Parser<'a> {
5183
5231
directory_ownership : DirectoryOwnership :: Owned ,
5184
5232
warn : false ,
5185
5233
} ) ,
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( ) ) ,
5192
5239
} ) ,
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,
5199
5244
} ) ,
5200
5245
} ;
5201
5246
@@ -5232,7 +5277,7 @@ impl<'a> Parser<'a> {
5232
5277
paths. name) ;
5233
5278
err. span_note ( id_sp, & msg) ;
5234
5279
}
5235
- return Err ( err) ;
5280
+ Err ( err)
5236
5281
} else if let DirectoryOwnership :: UnownedViaMod ( warn) = self . directory . ownership {
5237
5282
if warn {
5238
5283
if let Ok ( result) = paths. result {
@@ -5254,15 +5299,12 @@ impl<'a> Parser<'a> {
5254
5299
& format ! ( "... or maybe `use` the module `{}` instead \
5255
5300
of possibly redeclaring it",
5256
5301
paths. name) ) ;
5257
- return Err ( err) ;
5302
+ Err ( err)
5258
5303
} 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) )
5266
5308
}
5267
5309
}
5268
5310
0 commit comments