@@ -154,23 +154,39 @@ pub struct Module {
154
154
impl FrozenModule {
155
155
/// Get value, exported or private by name.
156
156
#[ doc( hidden) ] // TODO(nga): Buck2 depends on this function
157
- pub fn get_any_visibility ( & self , name : & str ) -> Option < ( OwnedFrozenValue , Visibility ) > {
158
- let ( slot, vis) = self . module . 0 . names . get_name ( name) ?;
157
+ pub fn get_any_visibility ( & self , name : & str ) -> anyhow:: Result < ( OwnedFrozenValue , Visibility ) > {
158
+ self . module
159
+ . 0
160
+ . names
161
+ . get_name ( name)
162
+ . and_then ( |( slot, vis) |
159
163
// This code is safe because we know the frozen module ref keeps the values alive
160
164
self . module
161
165
. 0
162
166
. slots
163
167
. get_slot ( slot)
164
- . map ( |x| ( unsafe { OwnedFrozenValue :: new ( self . heap . dupe ( ) , x) } , vis) )
168
+ . map ( |x| ( unsafe { OwnedFrozenValue :: new ( self . heap . dupe ( ) , x) } , vis) ) )
169
+ . ok_or_else (
170
+ || match did_you_mean ( name, self . names ( ) . map ( |s| s. as_str ( ) ) ) {
171
+ Some ( better) => EnvironmentError :: ModuleHasNoSymbolDidYouMean (
172
+ name. to_owned ( ) ,
173
+ better. to_owned ( ) ,
174
+ )
175
+ . into ( ) ,
176
+ None => EnvironmentError :: ModuleHasNoSymbol ( name. to_owned ( ) ) . into ( ) ,
177
+ } ,
178
+ )
165
179
}
166
180
167
181
/// Get the value of the exported variable `name`.
168
- /// Returns [`None`] if the variable isn't defined in the module or it is private.
169
- pub fn get ( & self , name : & str ) -> Option < OwnedFrozenValue > {
182
+ /// Returns an error if the variable isn't defined in the module or it is private.
183
+ pub fn get ( & self , name : & str ) -> anyhow :: Result < OwnedFrozenValue > {
170
184
self . get_any_visibility ( name)
171
185
. and_then ( |( value, vis) | match vis {
172
- Visibility :: Private => None ,
173
- Visibility :: Public => Some ( value) ,
186
+ Visibility :: Private => {
187
+ Err ( EnvironmentError :: ModuleSymbolIsNotExported ( name. to_owned ( ) ) . into ( ) )
188
+ }
189
+ Visibility :: Public => Ok ( value) ,
174
190
} )
175
191
}
176
192
@@ -207,6 +223,7 @@ impl FrozenModule {
207
223
. filter ( |n| Module :: default_visibility ( n) == Visibility :: Public )
208
224
. filter_map ( |n| {
209
225
self . get ( & n)
226
+ . ok ( )
210
227
. map ( |fv| ( n. as_str ( ) . to_owned ( ) , fv. value ( ) . get_ref ( ) . documentation ( ) ) )
211
228
} )
212
229
. collect ( ) ;
@@ -436,19 +453,9 @@ impl Module {
436
453
if Self :: default_visibility ( symbol) != Visibility :: Public {
437
454
return Err ( EnvironmentError :: CannotImportPrivateSymbol ( symbol. to_owned ( ) ) . into ( ) ) ;
438
455
}
439
- match module. get_any_visibility ( symbol) {
440
- None => Err ( {
441
- match did_you_mean ( symbol, module. names ( ) . map ( |s| s. as_str ( ) ) ) {
442
- Some ( better) => EnvironmentError :: ModuleHasNoSymbolDidYouMean (
443
- symbol. to_owned ( ) ,
444
- better. to_owned ( ) ,
445
- )
446
- . into ( ) ,
447
- None => EnvironmentError :: ModuleHasNoSymbol ( symbol. to_owned ( ) ) . into ( ) ,
448
- }
449
- } ) ,
450
- Some ( ( v, Visibility :: Public ) ) => Ok ( v. owned_value ( self . frozen_heap ( ) ) ) ,
451
- Some ( ( _, Visibility :: Private ) ) => {
456
+ match module. get_any_visibility ( symbol) ? {
457
+ ( v, Visibility :: Public ) => Ok ( v. owned_value ( self . frozen_heap ( ) ) ) ,
458
+ ( _, Visibility :: Private ) => {
452
459
Err ( EnvironmentError :: ModuleSymbolIsNotExported ( symbol. to_owned ( ) ) . into ( ) )
453
460
}
454
461
}
0 commit comments