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