@@ -21,7 +21,7 @@ use middle::typeck::{method_static, method_object};
21
21
22
22
use syntax:: ast;
23
23
use syntax:: ast_map;
24
- use syntax:: ast_util:: { is_local, def_id_of_def } ;
24
+ use syntax:: ast_util:: is_local;
25
25
use syntax:: attr;
26
26
use syntax:: codemap:: Span ;
27
27
use syntax:: parse:: token;
@@ -250,12 +250,6 @@ struct PrivacyVisitor<'self> {
250
250
last_private_map : resolve:: LastPrivateMap ,
251
251
}
252
252
253
- enum PrivacyResult {
254
- Allowable ,
255
- ExternallyDenied ,
256
- DisallowedBy ( ast:: NodeId ) ,
257
- }
258
-
259
253
impl < ' self > PrivacyVisitor < ' self > {
260
254
// used when debugging
261
255
fn nodestr ( & self , id : ast:: NodeId ) -> ~str {
@@ -264,11 +258,11 @@ impl<'self> PrivacyVisitor<'self> {
264
258
265
259
// Determines whether the given definition is public from the point of view
266
260
// of the current item.
267
- fn def_privacy ( & self , did : ast:: DefId ) -> PrivacyResult {
261
+ fn def_public ( & self , did : ast:: DefId ) -> bool {
268
262
if !is_local ( did) {
269
263
if self . external_exports . contains ( & did) {
270
264
debug2 ! ( "privacy - {:?} was externally exported" , did) ;
271
- return Allowable ;
265
+ return true ;
272
266
}
273
267
debug2 ! ( "privacy - is {:?} a public method" , did) ;
274
268
return match self . tcx . methods . find ( & did) {
@@ -277,42 +271,38 @@ impl<'self> PrivacyVisitor<'self> {
277
271
match meth. container {
278
272
ty:: TraitContainer ( id) => {
279
273
debug2 ! ( "privacy - recursing on trait {:?}" , id) ;
280
- self . def_privacy ( id)
274
+ self . def_public ( id)
281
275
}
282
276
ty:: ImplContainer ( id) => {
283
277
match ty:: impl_trait_ref ( self . tcx , id) {
284
278
Some ( t) => {
285
279
debug2 ! ( "privacy - impl of trait {:?}" , id) ;
286
- self . def_privacy ( t. def_id )
280
+ self . def_public ( t. def_id )
287
281
}
288
282
None => {
289
283
debug2 ! ( "privacy - found a method {:?}" ,
290
284
meth. vis) ;
291
- if meth. vis == ast:: public {
292
- Allowable
293
- } else {
294
- ExternallyDenied
295
- }
285
+ meth. vis == ast:: public
296
286
}
297
287
}
298
288
}
299
289
}
300
290
}
301
291
None => {
302
292
debug2 ! ( "privacy - nope, not even a method" ) ;
303
- ExternallyDenied
293
+ false
304
294
}
305
295
} ;
306
296
} else if self . exported_items . contains ( & did. node ) {
307
297
debug2 ! ( "privacy - exported item {}" , self . nodestr( did. node) ) ;
308
- return Allowable ;
298
+ return true ;
309
299
}
310
300
311
301
debug2 ! ( "privacy - local {:?} not public all the way down" , did) ;
312
302
// return quickly for things in the same module
313
303
if self . parents . find ( & did. node ) == self . parents . find ( & self . curitem ) {
314
304
debug2 ! ( "privacy - same parent, we're done here" ) ;
315
- return Allowable ;
305
+ return true ;
316
306
}
317
307
318
308
// We now know that there is at least one private member between the
@@ -340,11 +330,7 @@ impl<'self> PrivacyVisitor<'self> {
340
330
assert ! ( closest_private_id != ast:: DUMMY_NODE_ID ) ;
341
331
}
342
332
debug2 ! ( "privacy - closest priv {}" , self . nodestr( closest_private_id) ) ;
343
- if self . private_accessible ( closest_private_id) {
344
- Allowable
345
- } else {
346
- DisallowedBy ( closest_private_id)
347
- }
333
+ return self . private_accessible ( closest_private_id) ;
348
334
}
349
335
350
336
/// For a local private node in the AST, this function will determine
@@ -379,51 +365,12 @@ impl<'self> PrivacyVisitor<'self> {
379
365
}
380
366
}
381
367
382
- /// Guarantee that a particular definition is public, possibly emitting an
383
- /// error message if it's not.
384
- fn ensure_public ( & self , span : Span , to_check : ast:: DefId ,
385
- source_did : Option < ast:: DefId > , msg : & str ) -> bool {
386
- match self . def_privacy ( to_check) {
387
- ExternallyDenied => {
388
- self . tcx . sess . span_err ( span, format ! ( "{} is private" , msg) )
389
- }
390
- DisallowedBy ( id) => {
391
- if id == source_did. unwrap_or ( to_check) . node {
392
- self . tcx . sess . span_err ( span, format ! ( "{} is private" , msg) ) ;
393
- return false ;
394
- } else {
395
- self . tcx . sess . span_err ( span, format ! ( "{} is inaccessible" ,
396
- msg) ) ;
397
- }
398
- match self . tcx . items . find ( & id) {
399
- Some ( & ast_map:: node_item( item, _) ) => {
400
- let desc = match item. node {
401
- ast:: item_mod( * ) => "module" ,
402
- ast:: item_trait( * ) => "trait" ,
403
- _ => return false ,
404
- } ;
405
- let msg = format ! ( "{} `{}` is private" , desc,
406
- token:: ident_to_str( & item. ident) ) ;
407
- self . tcx . sess . span_note ( span, msg) ;
408
- }
409
- Some ( * ) | None => { }
410
- }
411
- }
412
- Allowable => return true
413
- }
414
- return false ;
415
- }
416
-
417
368
// Checks that a dereference of a univariant enum can occur.
418
369
fn check_variant ( & self , span : Span , enum_id : ast:: DefId ) {
419
370
let variant_info = ty:: enum_variants ( self . tcx , enum_id) [ 0 ] ;
420
-
421
- match self . def_privacy ( variant_info. id ) {
422
- Allowable => { }
423
- ExternallyDenied | DisallowedBy ( * ) => {
424
- self . tcx . sess . span_err ( span, "can only dereference enums \
425
- with a single, public variant") ;
426
- }
371
+ if !self . def_public ( variant_info. id ) {
372
+ self . tcx . sess . span_err ( span, "can only dereference enums \
373
+ with a single, public variant") ;
427
374
}
428
375
}
429
376
@@ -452,24 +399,29 @@ impl<'self> PrivacyVisitor<'self> {
452
399
let method_id = ty:: method ( self . tcx , method_id) . provided_source
453
400
. unwrap_or ( method_id) ;
454
401
455
- self . ensure_public ( span, method_id, None ,
456
- format ! ( "method `{}`" , token:: ident_to_str( name) ) ) ;
402
+ if !self . def_public ( method_id) {
403
+ debug2 ! ( "private: {:?}" , method_id) ;
404
+ self . tcx . sess . span_err ( span, format ! ( "method `{}` is private" ,
405
+ token:: ident_to_str( name) ) ) ;
406
+ }
457
407
}
458
408
459
409
// Checks that a path is in scope.
460
410
fn check_path ( & mut self , span : Span , path_id : ast:: NodeId , path : & ast:: Path ) {
461
411
debug2 ! ( "privacy - path {}" , self . nodestr( path_id) ) ;
462
- let def = self . tcx . def_map . get_copy ( & path_id) ;
463
412
let ck = |tyname : & str | {
464
- let origdid = def_id_of_def ( def) ;
465
- match * self . last_private_map . get ( & path_id) {
466
- resolve:: AllPublic => { } ,
467
- resolve:: DependsOn ( def) => {
468
- let name = token:: ident_to_str ( & path. segments . last ( )
469
- . identifier ) ;
470
- self . ensure_public ( span, def, Some ( origdid) ,
471
- format ! ( "{} `{}`" , tyname, name) ) ;
472
- }
413
+ let last_private = * self . last_private_map . get ( & path_id) ;
414
+ debug2 ! ( "privacy - {:?}" , last_private) ;
415
+ let public = match last_private {
416
+ resolve:: AllPublic => true ,
417
+ resolve:: DependsOn ( def) => self . def_public ( def) ,
418
+ } ;
419
+ if !public {
420
+ debug2 ! ( "denying {:?}" , path) ;
421
+ let name = token:: ident_to_str ( & path. segments . last ( )
422
+ . identifier ) ;
423
+ self . tcx . sess . span_err ( span,
424
+ format ! ( "{} `{}` is private" , tyname, name) ) ;
473
425
}
474
426
} ;
475
427
match self . tcx . def_map . get_copy ( & path_id) {
@@ -504,8 +456,9 @@ impl<'self> PrivacyVisitor<'self> {
504
456
method_num : method_num,
505
457
_
506
458
} ) => {
507
- if !self . ensure_public ( span, trait_id, None , "source trait" ) {
508
- return
459
+ if !self . def_public ( trait_id) {
460
+ self . tcx . sess . span_err ( span, "source trait is private" ) ;
461
+ return ;
509
462
}
510
463
match self . tcx . items . find ( & trait_id. node ) {
511
464
Some ( & ast_map:: node_item( item, _) ) => {
@@ -517,10 +470,12 @@ impl<'self> PrivacyVisitor<'self> {
517
470
node : method. id ,
518
471
crate : trait_id. crate ,
519
472
} ;
520
- self . ensure_public ( span, def, None ,
521
- format ! ( "method `{}`" ,
473
+ if self . def_public ( def) { return }
474
+ let msg = format ! ( "method `{}` is \
475
+ private",
522
476
token:: ident_to_str(
523
- & method. ident) ) ) ;
477
+ & method. ident) ) ;
478
+ self . tcx . sess . span_err ( span, msg) ;
524
479
}
525
480
ast:: required( _) => {
526
481
// Required methods can't be private.
0 commit comments