@@ -47,6 +47,10 @@ impl Visibility {
4747 Visibility :: PubCrate ( krate) => return from_module. krate == krate,
4848 Visibility :: Public => return true ,
4949 } ;
50+ if from_module == to_module {
51+ // if the modules are the same, visibility is trivially satisfied
52+ return true ;
53+ }
5054 // if they're not in the same crate, it can't be visible
5155 if from_module. krate != to_module. krate {
5256 return false ;
@@ -70,6 +74,11 @@ impl Visibility {
7074 if def_map. krate ( ) != to_module. krate {
7175 return false ;
7276 }
77+
78+ if from_module == to_module. local_id && def_map. block_id ( ) == to_module. block {
79+ // if the modules are the same, visibility is trivially satisfied
80+ return true ;
81+ }
7382 Self :: is_visible_from_def_map_ ( db, def_map, to_module, from_module)
7483 }
7584
@@ -93,9 +102,7 @@ impl Visibility {
93102 // `to_module` is not a block, so there is no parent def map to use.
94103 ( None , _) => ( ) ,
95104 // `to_module` is at `def_map`'s block, no need to move further.
96- ( Some ( a) , Some ( b) ) if a == b => {
97- cov_mark:: hit!( is_visible_from_same_block_def_map) ;
98- }
105+ ( Some ( a) , Some ( b) ) if a == b => { }
99106 _ => {
100107 if let Some ( parent) = to_module. def_map ( db) . parent ( ) {
101108 to_module = parent;
@@ -153,7 +160,22 @@ impl Visibility {
153160 }
154161 }
155162 ( Visibility :: Module ( mod_a, expl_a) , Visibility :: Module ( mod_b, expl_b) ) => {
156- if mod_a. krate != def_map. krate ( ) || mod_b. krate != def_map. krate ( ) {
163+ if mod_a == mod_b {
164+ // Most module visibilities are `pub(self)`, and assuming no errors
165+ // this will be the common and thus fast path.
166+ return Some ( Visibility :: Module (
167+ mod_a,
168+ match ( expl_a, expl_b) {
169+ ( VisibilityExplicitness :: Explicit , _)
170+ | ( _, VisibilityExplicitness :: Explicit ) => {
171+ VisibilityExplicitness :: Explicit
172+ }
173+ _ => VisibilityExplicitness :: Implicit ,
174+ } ,
175+ ) ) ;
176+ }
177+
178+ if mod_a. krate ( ) != def_map. krate ( ) || mod_b. krate ( ) != def_map. krate ( ) {
157179 return None ;
158180 }
159181
@@ -201,7 +223,22 @@ impl Visibility {
201223 if mod_. krate == krate { Some ( Visibility :: Module ( mod_, exp) ) } else { None }
202224 }
203225 ( Visibility :: Module ( mod_a, expl_a) , Visibility :: Module ( mod_b, expl_b) ) => {
204- if mod_a. krate != def_map. krate ( ) || mod_b. krate != def_map. krate ( ) {
226+ if mod_a == mod_b {
227+ // Most module visibilities are `pub(self)`, and assuming no errors
228+ // this will be the common and thus fast path.
229+ return Some ( Visibility :: Module (
230+ mod_a,
231+ match ( expl_a, expl_b) {
232+ ( VisibilityExplicitness :: Explicit , _)
233+ | ( _, VisibilityExplicitness :: Explicit ) => {
234+ VisibilityExplicitness :: Explicit
235+ }
236+ _ => VisibilityExplicitness :: Implicit ,
237+ } ,
238+ ) ) ;
239+ }
240+
241+ if mod_a. krate ( ) != def_map. krate ( ) || mod_b. krate ( ) != def_map. krate ( ) {
205242 return None ;
206243 }
207244
0 commit comments