@@ -11,7 +11,7 @@ use crate::resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleIm
11
11
use crate :: { Module , ModuleData , ModuleKind , NameBinding , NameBindingKind , Segment , ToNameBinding } ;
12
12
use crate :: { ModuleOrUniformRoot , ParentScope , PerNS , Resolver , ResolverArenas , ExternPreludeEntry } ;
13
13
use crate :: Namespace :: { self , TypeNS , ValueNS , MacroNS } ;
14
- use crate :: { ResolutionError , Determinacy , PathResult , CrateLint } ;
14
+ use crate :: { ResolutionError , VisResolutionError , Determinacy , PathResult , CrateLint } ;
15
15
16
16
use rustc:: bug;
17
17
use rustc:: hir:: def:: { self , * } ;
@@ -35,7 +35,7 @@ use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind};
35
35
use syntax:: feature_gate:: is_builtin_attr;
36
36
use syntax:: parse:: token:: { self , Token } ;
37
37
use syntax:: print:: pprust;
38
- use syntax:: { span_err, struct_span_err } ;
38
+ use syntax:: span_err;
39
39
use syntax:: source_map:: { respan, Spanned } ;
40
40
use syntax:: symbol:: { kw, sym} ;
41
41
use syntax:: visit:: { self , Visitor } ;
@@ -194,22 +194,25 @@ impl<'a> AsMut<Resolver<'a>> for BuildReducedGraphVisitor<'a, '_> {
194
194
195
195
impl < ' a , ' b > BuildReducedGraphVisitor < ' a , ' b > {
196
196
fn resolve_visibility ( & mut self , vis : & ast:: Visibility ) -> ty:: Visibility {
197
- self . resolve_visibility_speculative ( vis, false )
197
+ self . resolve_visibility_speculative ( vis, false ) . unwrap_or_else ( |err| {
198
+ self . r . report_vis_error ( err) ;
199
+ ty:: Visibility :: Public
200
+ } )
198
201
}
199
202
200
- fn resolve_visibility_speculative (
203
+ fn resolve_visibility_speculative < ' ast > (
201
204
& mut self ,
202
- vis : & ast:: Visibility ,
205
+ vis : & ' ast ast:: Visibility ,
203
206
speculative : bool ,
204
- ) -> ty:: Visibility {
207
+ ) -> Result < ty:: Visibility , VisResolutionError < ' ast > > {
205
208
let parent_scope = & self . parent_scope ;
206
209
match vis. node {
207
- ast:: VisibilityKind :: Public => ty:: Visibility :: Public ,
210
+ ast:: VisibilityKind :: Public => Ok ( ty:: Visibility :: Public ) ,
208
211
ast:: VisibilityKind :: Crate ( ..) => {
209
- ty:: Visibility :: Restricted ( DefId :: local ( CRATE_DEF_INDEX ) )
212
+ Ok ( ty:: Visibility :: Restricted ( DefId :: local ( CRATE_DEF_INDEX ) ) )
210
213
}
211
214
ast:: VisibilityKind :: Inherited => {
212
- ty:: Visibility :: Restricted ( parent_scope. module . normal_ancestor_id )
215
+ Ok ( ty:: Visibility :: Restricted ( parent_scope. module . normal_ancestor_id ) )
213
216
}
214
217
ast:: VisibilityKind :: Restricted { ref path, id, .. } => {
215
218
// For visibilities we are not ready to provide correct implementation of "uniform
@@ -219,32 +222,19 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
219
222
let ident = path. segments . get ( 0 ) . expect ( "empty path in visibility" ) . ident ;
220
223
let crate_root = if ident. is_path_segment_keyword ( ) {
221
224
None
222
- } else if ident. span . rust_2018 ( ) {
223
- let msg = "relative paths are not supported in visibilities on 2018 edition" ;
224
- self . r . session . struct_span_err ( ident. span , msg)
225
- . span_suggestion (
226
- path. span ,
227
- "try" ,
228
- format ! ( "crate::{}" , pprust:: path_to_string( & path) ) ,
229
- Applicability :: MaybeIncorrect ,
230
- )
231
- . emit ( ) ;
232
- return ty:: Visibility :: Public ;
233
- } else {
234
- let ctxt = ident. span . ctxt ( ) ;
225
+ } else if ident. span . rust_2015 ( ) {
235
226
Some ( Segment :: from_ident ( Ident :: new (
236
- kw:: PathRoot , path. span . shrink_to_lo ( ) . with_ctxt ( ctxt)
227
+ kw:: PathRoot , path. span . shrink_to_lo ( ) . with_ctxt ( ident . span . ctxt ( ) )
237
228
) ) )
229
+ } else {
230
+ return Err ( VisResolutionError :: Relative2018 ( ident. span , path) ) ;
238
231
} ;
239
232
240
233
let segments = crate_root. into_iter ( )
241
234
. chain ( path. segments . iter ( ) . map ( |seg| seg. into ( ) ) ) . collect :: < Vec < _ > > ( ) ;
242
- let expected_found_error = |this : & Self , res : Res | {
243
- let path_str = Segment :: names_to_string ( & segments) ;
244
- struct_span_err ! ( this. r. session, path. span, E0577 ,
245
- "expected module, found {} `{}`" , res. descr( ) , path_str)
246
- . span_label ( path. span , "not a module" ) . emit ( ) ;
247
- } ;
235
+ let expected_found_error = |res| Err ( VisResolutionError :: ExpectedFound (
236
+ path. span , Segment :: names_to_string ( & segments) , res
237
+ ) ) ;
248
238
match self . r . resolve_path (
249
239
& segments,
250
240
Some ( TypeNS ) ,
@@ -260,42 +250,27 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
260
250
}
261
251
if module. is_normal ( ) {
262
252
if res == Res :: Err {
263
- ty:: Visibility :: Public
253
+ Ok ( ty:: Visibility :: Public )
264
254
} else {
265
255
let vis = ty:: Visibility :: Restricted ( res. def_id ( ) ) ;
266
256
if self . r . is_accessible_from ( vis, parent_scope. module ) {
267
- vis
257
+ Ok ( vis)
268
258
} else {
269
- struct_span_err ! ( self . r. session, path. span, E0742 ,
270
- "visibilities can only be restricted to ancestor modules" )
271
- . emit ( ) ;
272
- ty:: Visibility :: Public
259
+ Err ( VisResolutionError :: AncestorOnly ( path. span ) )
273
260
}
274
261
}
275
262
} else {
276
- expected_found_error ( self , res) ;
277
- ty:: Visibility :: Public
263
+ expected_found_error ( res)
278
264
}
279
265
}
280
- PathResult :: Module ( ..) => {
281
- self . r . session . span_err ( path. span , "visibility must resolve to a module" ) ;
282
- ty:: Visibility :: Public
283
- }
284
- PathResult :: NonModule ( partial_res) => {
285
- expected_found_error ( self , partial_res. base_res ( ) ) ;
286
- ty:: Visibility :: Public
287
- }
288
- PathResult :: Failed { span, label, suggestion, .. } => {
289
- self . r . report_error (
290
- span, ResolutionError :: FailedToResolve { label, suggestion }
291
- ) ;
292
- ty:: Visibility :: Public
293
- }
294
- PathResult :: Indeterminate => {
295
- span_err ! ( self . r. session, path. span, E0578 ,
296
- "cannot determine resolution for the visibility" ) ;
297
- ty:: Visibility :: Public
298
- }
266
+ PathResult :: Module ( ..) =>
267
+ Err ( VisResolutionError :: ModuleOnly ( path. span ) ) ,
268
+ PathResult :: NonModule ( partial_res) =>
269
+ expected_found_error ( partial_res. base_res ( ) ) ,
270
+ PathResult :: Failed { span, label, suggestion, .. } =>
271
+ Err ( VisResolutionError :: FailedToResolve ( span, label, suggestion) ) ,
272
+ PathResult :: Indeterminate =>
273
+ Err ( VisResolutionError :: Indeterminate ( path. span ) ) ,
299
274
}
300
275
}
301
276
}
@@ -764,9 +739,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
764
739
// NOTE: The field may be an expansion placeholder, but expansion sets
765
740
// correct visibilities for unnamed field placeholders specifically, so the
766
741
// constructor visibility should still be determined correctly.
767
- let field_vis = self . resolve_visibility_speculative ( & field. vis , true ) ;
768
- if ctor_vis. is_at_least ( field_vis, & * self . r ) {
769
- ctor_vis = field_vis;
742
+ if let Ok ( field_vis) =
743
+ self . resolve_visibility_speculative ( & field. vis , true ) {
744
+ if ctor_vis. is_at_least ( field_vis, & * self . r ) {
745
+ ctor_vis = field_vis;
746
+ }
770
747
}
771
748
}
772
749
let ctor_res = Res :: Def (
0 commit comments