@@ -47,6 +47,10 @@ impl Visibility {
47
47
Visibility :: PubCrate ( krate) => return from_module. krate == krate,
48
48
Visibility :: Public => return true ,
49
49
} ;
50
+ if from_module == to_module {
51
+ // if the modules are the same, visibility is trivially satisfied
52
+ return true ;
53
+ }
50
54
// if they're not in the same crate, it can't be visible
51
55
if from_module. krate != to_module. krate {
52
56
return false ;
@@ -70,6 +74,11 @@ impl Visibility {
70
74
if def_map. krate ( ) != to_module. krate {
71
75
return false ;
72
76
}
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
+ }
73
82
Self :: is_visible_from_def_map_ ( db, def_map, to_module, from_module)
74
83
}
75
84
@@ -93,9 +102,7 @@ impl Visibility {
93
102
// `to_module` is not a block, so there is no parent def map to use.
94
103
( None , _) => ( ) ,
95
104
// `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 => { }
99
106
_ => {
100
107
if let Some ( parent) = to_module. def_map ( db) . parent ( ) {
101
108
to_module = parent;
@@ -153,7 +160,22 @@ impl Visibility {
153
160
}
154
161
}
155
162
( 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 ( ) {
157
179
return None ;
158
180
}
159
181
@@ -201,7 +223,22 @@ impl Visibility {
201
223
if mod_. krate == krate { Some ( Visibility :: Module ( mod_, exp) ) } else { None }
202
224
}
203
225
( 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 ( ) {
205
242
return None ;
206
243
}
207
244
0 commit comments