Skip to content

Commit

Permalink
attr impl return starlark instead of anyhow
Browse files Browse the repository at this point in the history
Summary: Have starlark attr return a starlark error instead of an anyhow error. Required for anyhow removal in `buck2/app`

Reviewed By: JakobDegen

Differential Revision: D65612232

fbshipit-source-id: f5d1872351381a516a408f87acdfe0216998b30a
  • Loading branch information
Will-MingLun-Li authored and facebook-github-bot committed Nov 9, 2024
1 parent 5beb0e2 commit f143b57
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion starlark/src/docs/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub enum DocStringKind {
/// fn add_some_value(builder: &mut MethodsBuilder) {
/// /// attr1 is an attribute that does nothing interesting.
/// #[starlark(attribute)]
/// fn attr1<'v>(this: Value<'v>) -> anyhow::Result<String> {
/// fn attr1<'v>(this: Value<'v>) -> starlark::Result<String> {
/// let _ = this;
/// Ok("attr1".to_owned())
/// }
Expand Down
4 changes: 2 additions & 2 deletions starlark/src/docs/tests/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ impl<'v> StarlarkValue<'v> for Obj {
fn object(builder: &mut MethodsBuilder) {
/// Docs for attr1
#[starlark(attribute)]
fn attr1<'v>(this: Value<'v>) -> anyhow::Result<String> {
fn attr1<'v>(this: Value<'v>) -> starlark::Result<String> {
Ok("attr1".to_owned())
}

#[starlark(attribute)]
fn attr2<'v>(this: Value<'v>) -> anyhow::Result<String> {
fn attr2<'v>(this: Value<'v>) -> starlark::Result<String> {
Ok("attr2".to_owned())
}

Expand Down
2 changes: 1 addition & 1 deletion starlark/src/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ mod tests {
#[starlark_module]
fn methods(builder: &mut MethodsBuilder) {
#[starlark(attribute)]
fn invert1(this: Bool2) -> anyhow::Result<Bool2> {
fn invert1(this: Bool2) -> starlark::Result<Bool2> {
Ok(Bool2(!this.0))
}

Expand Down
4 changes: 2 additions & 2 deletions starlark/src/stdlib/call_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl Display for StackFrame {
fn stack_frame_methods(builder: &mut MethodsBuilder) {
/// Returns the name of the entry on the call-stack.
#[starlark(attribute)]
fn func_name(this: &StackFrame) -> anyhow::Result<String> {
fn func_name(this: &StackFrame) -> starlark::Result<String> {
Ok(this.name.clone())
}

/// Returns a path of the module from which the entry was called, or [`None`] for native Rust functions.
#[starlark(attribute)]
fn module_path(this: &StackFrame) -> anyhow::Result<NoneOr<String>> {
fn module_path(this: &StackFrame) -> starlark::Result<NoneOr<String>> {
match this.location {
Some(ref location) => Ok(NoneOr::Other(location.file.filename().to_owned())),
None => Ok(NoneOr::None),
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/tests/derive/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::values::ValueLike;
fn object_docs_1(_: &mut MethodsBuilder) {
/// Returns the string "foo"
#[starlark(attribute)]
fn foo(this: &TestExample) -> anyhow::Result<String> {
fn foo(this: &TestExample) -> starlark::Result<String> {
Ok("foo".to_owned())
}
}
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/tests/derive/module/other_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn test_other_attributes_in_atributes(methods: &mut MethodsBuilder) {
fn test_attribute(
// TODO(nga): this marker is no-op.
#[allow(unused_variables)] this: u32,
) -> anyhow::Result<NoneType> {
) -> starlark::Result<NoneType> {
Ok(NoneType)
}
}
2 changes: 1 addition & 1 deletion starlark/src/tests/derive/module/return_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn _test_return_impl_alloc_value(globals: &mut GlobalsBuilder) {
#[starlark_module]
fn _test_return_impl_alloc_value_for_attr(methods: &mut MethodsBuilder) {
#[starlark(attribute)]
fn attr<'v>(this: Value<'v>) -> anyhow::Result<impl AllocValue<'v>> {
fn attr<'v>(this: Value<'v>) -> starlark::Result<impl AllocValue<'v>> {
Ok(this)
}
}
4 changes: 2 additions & 2 deletions starlark/src/values/type_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ impl<TLeft: StarlarkTypeRepr, TRight: StarlarkTypeRepr> StarlarkTypeRepr for Eit
/// Derive macros generate a reference to this method to be able to get the `type_repr` of types
/// they can't name
#[doc(hidden)]
pub fn type_repr_from_attr_impl<'v, T: StarlarkTypeRepr>(
_f: fn(Value<'v>, &'v Heap) -> anyhow::Result<T>,
pub fn type_repr_from_attr_impl<'v, T: StarlarkTypeRepr, E>(
_f: fn(Value<'v>, &'v Heap) -> Result<T, E>,
) -> Ty {
T::starlark_type_repr()
}
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/values/types/enumeration/enum_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ where
#[starlark_module]
fn enum_type_methods(builder: &mut MethodsBuilder) {
#[starlark(attribute)]
fn r#type<'v>(this: Value, heap: &Heap) -> anyhow::Result<Value<'v>> {
fn r#type<'v>(this: Value, heap: &Heap) -> starlark::Result<Value<'v>> {
let this = EnumType::from_value(this).unwrap();
let ty_enum_type = match this {
Either::Left(x) => x.ty_enum_data(),
Expand Down
4 changes: 2 additions & 2 deletions starlark/src/values/types/enumeration/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ impl<'v, V: ValueLike<'v>> serde::Serialize for EnumValueGen<V> {
#[starlark_module]
fn enum_value_methods(methods: &mut MethodsBuilder) {
#[starlark(attribute)]
fn index(this: &EnumValue) -> anyhow::Result<i32> {
fn index(this: &EnumValue) -> starlark::Result<i32> {
Ok(this.index)
}

#[starlark(attribute)]
fn value<'v>(this: &EnumValue<'v>) -> anyhow::Result<Value<'v>> {
fn value<'v>(this: &EnumValue<'v>) -> starlark::Result<Value<'v>> {
Ok(this.value.to_value())
}
}
2 changes: 1 addition & 1 deletion starlark/src/values/types/record/record_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ where
#[starlark_module]
fn record_type_methods(methods: &mut MethodsBuilder) {
#[starlark(attribute)]
fn r#type<'v>(this: ValueTypedComplex<'v, RecordType<'v>>) -> anyhow::Result<&'v str> {
fn r#type<'v>(this: ValueTypedComplex<'v, RecordType<'v>>) -> starlark::Result<&'v str> {
let ty_record_type = match this.unpack() {
Either::Left(x) => x.ty_record_data.get(),
Either::Right(x) => x.ty_record_data.as_ref(),
Expand Down

0 comments on commit f143b57

Please sign in to comment.