Skip to content

Commit 4475145

Browse files
jaspercbfacebook-github-bot
authored andcommitted
FrozenModule::get_any_visibility(name) returns result
Summary: We already had nice "did you mean?" errors for missing names in `Module::load_symbol`, but some code (e.g. BXL) uses `FrozenModule::get_any_visibility()` directly. Let's have it return a Result and move the "name not found" error message into name resolution. Reviewed By: bobyangyf Differential Revision: D38590094 fbshipit-source-id: 02e310e1f4fde829419d9f53fc2424b43dc5e0e5
1 parent 08a2a94 commit 4475145

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

starlark/src/environment/modules.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,39 @@ pub struct Module {
154154
impl FrozenModule {
155155
/// Get value, exported or private by name.
156156
#[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)|
159163
// This code is safe because we know the frozen module ref keeps the values alive
160164
self.module
161165
.0
162166
.slots
163167
.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+
)
165179
}
166180

167181
/// 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> {
170184
self.get_any_visibility(name)
171185
.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),
174190
})
175191
}
176192

@@ -207,6 +223,7 @@ impl FrozenModule {
207223
.filter(|n| Module::default_visibility(n) == Visibility::Public)
208224
.filter_map(|n| {
209225
self.get(&n)
226+
.ok()
210227
.map(|fv| (n.as_str().to_owned(), fv.value().get_ref().documentation()))
211228
})
212229
.collect();
@@ -436,19 +453,9 @@ impl Module {
436453
if Self::default_visibility(symbol) != Visibility::Public {
437454
return Err(EnvironmentError::CannotImportPrivateSymbol(symbol.to_owned()).into());
438455
}
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) => {
452459
Err(EnvironmentError::ModuleSymbolIsNotExported(symbol.to_owned()).into())
453460
}
454461
}

0 commit comments

Comments
 (0)