@@ -383,14 +383,26 @@ fn encode_info_for_mod(ecx: @EncodeContext, ebml_w: writer::Encoder,
383383 ebml_w. end_tag ( ) ;
384384}
385385
386- fn encode_visibility ( ebml_w : writer:: Encoder , visibility : visibility ) {
386+ fn encode_struct_field_family ( ebml_w : writer:: Encoder ,
387+ visibility : visibility ) {
387388 encode_family ( ebml_w, match visibility {
388389 public => 'g' ,
389390 private => 'j' ,
390391 inherited => 'N'
391392 } ) ;
392393}
393394
395+ fn encode_visibility ( ebml_w : writer:: Encoder , visibility : visibility ) {
396+ ebml_w. start_tag ( tag_items_data_item_visibility) ;
397+ let ch = match visibility {
398+ public => 'y' ,
399+ private => 'n' ,
400+ inherited => 'i' ,
401+ } ;
402+ ebml_w. wr_str ( str:: from_char ( ch) ) ;
403+ ebml_w. end_tag ( ) ;
404+ }
405+
394406fn encode_self_type ( ebml_w : writer:: Encoder , self_type : ast:: self_ty_ ) {
395407 ebml_w. start_tag ( tag_item_trait_method_self_ty) ;
396408
@@ -456,7 +468,7 @@ fn encode_info_for_struct(ecx: @EncodeContext, ebml_w: writer::Encoder,
456468 ebml_w. start_tag ( tag_items_data_item) ;
457469 debug ! ( "encode_info_for_struct: doing %s %d" ,
458470 * tcx. sess. str_of( nm) , id) ;
459- encode_visibility ( ebml_w, vis) ;
471+ encode_struct_field_family ( ebml_w, vis) ;
460472 encode_name ( ecx, ebml_w, nm) ;
461473 encode_path ( ecx, ebml_w, path, ast_map:: path_name ( nm) ) ;
462474 encode_type ( ecx, ebml_w, node_id_to_type ( tcx, id) ) ;
@@ -525,6 +537,7 @@ fn encode_info_for_method(ecx: @EncodeContext,
525537 should_inline : bool ,
526538 parent_id : node_id ,
527539 m : @method ,
540+ parent_visibility : ast:: visibility ,
528541 owner_generics : & ast:: Generics ,
529542 method_generics : & ast:: Generics ) {
530543 debug ! ( "encode_info_for_method: %d %s %u %u" , m. id,
@@ -533,6 +546,7 @@ fn encode_info_for_method(ecx: @EncodeContext,
533546 method_generics. ty_params. len( ) ) ;
534547 ebml_w. start_tag ( tag_items_data_item) ;
535548 encode_def_id ( ebml_w, local_def ( m. id ) ) ;
549+
536550 match m. self_ty . node {
537551 ast:: sty_static => {
538552 encode_family ( ebml_w, purity_static_method_family ( m. purity ) ) ;
@@ -550,6 +564,14 @@ fn encode_info_for_method(ecx: @EncodeContext,
550564 encode_name ( ecx, ebml_w, m. ident ) ;
551565 encode_path ( ecx, ebml_w, impl_path, ast_map:: path_name ( m. ident ) ) ;
552566 encode_self_type ( ebml_w, m. self_ty . node ) ;
567+
568+ // Combine parent visibility and this visibility.
569+ let visibility = match m. vis {
570+ ast:: inherited => parent_visibility,
571+ vis => vis,
572+ } ;
573+ encode_visibility ( ebml_w, visibility) ;
574+
553575 if len > 0 u || should_inline {
554576 ( ecx. encode_inlined_item ) (
555577 ecx, ebml_w, impl_path,
@@ -568,6 +590,7 @@ fn purity_fn_family(p: purity) -> char {
568590 extern_fn => 'e'
569591 }
570592}
593+
571594fn purity_static_method_family( p : purity ) -> char {
572595 match p {
573596 unsafe_fn => 'U' ,
@@ -757,7 +780,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
757780 match f.node.kind {
758781 named_field(ident, _, vis) => {
759782 ebml_w.start_tag(tag_item_field);
760- encode_visibility (ebml_w, vis);
783+ encode_struct_field_family (ebml_w, vis);
761784 encode_name(ecx, ebml_w, ident);
762785 encode_def_id(ebml_w, local_def(f.node.id));
763786 ebml_w.end_tag();
@@ -808,12 +831,28 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
808831 let mut impl_path = vec::append(~[], path);
809832 impl_path += ~[ast_map::path_name(item.ident)];
810833
834+ // If there is a trait reference, treat the methods as always public.
835+ // This is to work around some incorrect behavior in privacy checking:
836+ // when the method belongs to a trait, it should acquire the privacy
837+ // from the trait, not the impl. Forcing the visibility to be public
838+ // makes things sorta work.
839+ let parent_visibility = if opt_trait.is_some() {
840+ ast::public
841+ } else {
842+ item.vis
843+ };
844+
811845 for methods.each |m| {
812846 index.push(entry {val: m.id, pos: ebml_w.writer.tell()});
813- encode_info_for_method(ecx, ebml_w, impl_path,
847+ encode_info_for_method(ecx,
848+ ebml_w,
849+ impl_path,
814850 should_inline(m.attrs),
815- item.id, *m,
816- generics, &m.generics);
851+ item.id,
852+ *m,
853+ parent_visibility,
854+ generics,
855+ &m.generics);
817856 }
818857 }
819858 item_trait(ref generics, ref traits, ref ms) => {
@@ -902,9 +941,15 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
902941 // of provided methods. I am not sure why this is. -ndm
903942 let owner_generics = ast_util::empty_generics();
904943
905- encode_info_for_method(ecx, ebml_w, /*bad*/copy path,
906- true, item.id, *m,
907- &owner_generics, &m.generics);
944+ encode_info_for_method(ecx,
945+ ebml_w,
946+ /*bad*/copy path,
947+ true,
948+ item.id,
949+ *m,
950+ item.vis,
951+ &owner_generics,
952+ &m.generics);
908953 }
909954 }
910955 item_mac(*) => fail!(~" item macros unimplemented")
0 commit comments