@@ -17,15 +17,23 @@ use hir_ty::{
1717} ;
1818
1919use crate :: {
20- Adt , Const , ConstParam , Enum , Field , Function , GenericParam , HasCrate , HasVisibility ,
21- LifetimeParam , Macro , Module , Static , Struct , Trait , TyBuilder , Type , TypeAlias ,
22- TypeOrConstParam , TypeParam , Union , Variant ,
20+ Adt , AsAssocItem , AssocItemContainer , Const , ConstParam , Enum , Field , Function , GenericParam ,
21+ HasCrate , HasVisibility , LifetimeParam , Macro , Module , Static , Struct , Trait , TyBuilder , Type ,
22+ TypeAlias , TypeOrConstParam , TypeParam , Union , Variant ,
2323} ;
2424
2525impl HirDisplay for Function {
2626 fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
27- let data = f. db . function_data ( self . id ) ;
28- write_visibility ( self . module ( f. db ) . id , self . visibility ( f. db ) , f) ?;
27+ let db = f. db ;
28+ let data = db. function_data ( self . id ) ;
29+ let container = self . as_assoc_item ( db) . map ( |it| it. container ( db) ) ;
30+ let mut module = self . module ( db) ;
31+ if let Some ( AssocItemContainer :: Impl ( _) ) = container {
32+ // Block-local impls are "hoisted" to the nearest (non-block) module.
33+ module = module. nearest_non_block_module ( db) ;
34+ }
35+ let module_id = module. id ;
36+ write_visibility ( module_id, self . visibility ( db) , f) ?;
2937 if data. has_default_kw ( ) {
3038 f. write_str ( "default " ) ?;
3139 }
@@ -35,7 +43,7 @@ impl HirDisplay for Function {
3543 if data. has_async_kw ( ) {
3644 f. write_str ( "async " ) ?;
3745 }
38- if self . is_unsafe_to_call ( f . db ) {
46+ if self . is_unsafe_to_call ( db) {
3947 f. write_str ( "unsafe " ) ?;
4048 }
4149 if let Some ( abi) = & data. abi {
@@ -442,8 +450,15 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter<'_>) -> Result<(),
442450
443451impl HirDisplay for Const {
444452 fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
445- write_visibility ( self . module ( f. db ) . id , self . visibility ( f. db ) , f) ?;
446- let data = f. db . const_data ( self . id ) ;
453+ let db = f. db ;
454+ let container = self . as_assoc_item ( db) . map ( |it| it. container ( db) ) ;
455+ let mut module = self . module ( db) ;
456+ if let Some ( AssocItemContainer :: Impl ( _) ) = container {
457+ // Block-local impls are "hoisted" to the nearest (non-block) module.
458+ module = module. nearest_non_block_module ( db) ;
459+ }
460+ write_visibility ( module. id , self . visibility ( db) , f) ?;
461+ let data = db. const_data ( self . id ) ;
447462 f. write_str ( "const " ) ?;
448463 match & data. name {
449464 Some ( name) => write ! ( f, "{name}: " ) ?,
0 commit comments