@@ -51,6 +51,10 @@ impl Visibility {
5151 Visibility :: PubCrate ( krate) => return from_module. krate ( db) == krate,
5252 Visibility :: Public => return true ,
5353 } ;
54+ if from_module == to_module {
55+ // if the modules are the same, visibility is trivially satisfied
56+ return true ;
57+ }
5458 // if they're not in the same crate, it can't be visible
5559 if from_module. krate ( db) != to_module. krate ( db) {
5660 return false ;
@@ -73,6 +77,10 @@ impl Visibility {
7377 Visibility :: PubCrate ( krate) => return from_module. krate ( db) == krate,
7478 Visibility :: Public => return true ,
7579 } ;
80+ if from_module == to_module {
81+ // if the modules are the same, visibility is trivially satisfied
82+ return true ;
83+ }
7684 // if they're not in the same crate, it can't be visible
7785 if def_map. krate ( ) != to_module. krate ( db) {
7886 return false ;
@@ -100,9 +108,7 @@ impl Visibility {
100108 // `to_module` is not a block, so there is no parent def map to use.
101109 ( None , _) => ( ) ,
102110 // `to_module` is at `def_map`'s block, no need to move further.
103- ( Some ( a) , Some ( b) ) if a == b => {
104- cov_mark:: hit!( nested_module_scoping) ;
105- }
111+ ( Some ( a) , Some ( b) ) if a == b => { }
106112 _ => {
107113 if let Some ( parent) = to_module. def_map ( db) . parent ( ) {
108114 to_module = parent;
@@ -161,6 +167,21 @@ impl Visibility {
161167 if mod_. krate ( db) == krate { Some ( Visibility :: PubCrate ( krate) ) } else { None }
162168 }
163169 ( Visibility :: Module ( mod_a, expl_a) , Visibility :: Module ( mod_b, expl_b) ) => {
170+ if mod_a == mod_b {
171+ // Most module visibilities are `pub(self)`, and assuming no errors
172+ // this will be the common and thus fast path.
173+ return Some ( Visibility :: Module (
174+ mod_a,
175+ match ( expl_a, expl_b) {
176+ ( VisibilityExplicitness :: Explicit , _)
177+ | ( _, VisibilityExplicitness :: Explicit ) => {
178+ VisibilityExplicitness :: Explicit
179+ }
180+ _ => VisibilityExplicitness :: Implicit ,
181+ } ,
182+ ) ) ;
183+ }
184+
164185 if mod_a. krate ( db) != def_map. krate ( ) || mod_b. krate ( db) != def_map. krate ( ) {
165186 return None ;
166187 }
@@ -212,6 +233,21 @@ impl Visibility {
212233 if mod_. krate ( db) == krate { Some ( Visibility :: Module ( mod_, exp) ) } else { None }
213234 }
214235 ( Visibility :: Module ( mod_a, expl_a) , Visibility :: Module ( mod_b, expl_b) ) => {
236+ if mod_a == mod_b {
237+ // Most module visibilities are `pub(self)`, and assuming no errors
238+ // this will be the common and thus fast path.
239+ return Some ( Visibility :: Module (
240+ mod_a,
241+ match ( expl_a, expl_b) {
242+ ( VisibilityExplicitness :: Explicit , _)
243+ | ( _, VisibilityExplicitness :: Explicit ) => {
244+ VisibilityExplicitness :: Explicit
245+ }
246+ _ => VisibilityExplicitness :: Implicit ,
247+ } ,
248+ ) ) ;
249+ }
250+
215251 if mod_a. krate ( db) != def_map. krate ( ) || mod_b. krate ( db) != def_map. krate ( ) {
216252 return None ;
217253 }
0 commit comments