From f143b5771cec274e9b5660d8b56edf5d4cafdd77 Mon Sep 17 00:00:00 2001 From: Will Li Date: Fri, 8 Nov 2024 20:37:36 -0800 Subject: [PATCH] attr impl return starlark instead of anyhow 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 --- starlark/src/docs/parse.rs | 2 +- starlark/src/docs/tests/markdown.rs | 4 ++-- starlark/src/stdlib.rs | 2 +- starlark/src/stdlib/call_stack.rs | 4 ++-- starlark/src/tests/derive/docs.rs | 2 +- starlark/src/tests/derive/module/other_attributes.rs | 2 +- starlark/src/tests/derive/module/return_impl.rs | 2 +- starlark/src/values/type_repr.rs | 4 ++-- starlark/src/values/types/enumeration/enum_type.rs | 2 +- starlark/src/values/types/enumeration/value.rs | 4 ++-- starlark/src/values/types/record/record_type.rs | 2 +- 11 files changed, 15 insertions(+), 15 deletions(-) diff --git a/starlark/src/docs/parse.rs b/starlark/src/docs/parse.rs index 72e3d3b7a..4e7012a1b 100644 --- a/starlark/src/docs/parse.rs +++ b/starlark/src/docs/parse.rs @@ -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 { + /// fn attr1<'v>(this: Value<'v>) -> starlark::Result { /// let _ = this; /// Ok("attr1".to_owned()) /// } diff --git a/starlark/src/docs/tests/markdown.rs b/starlark/src/docs/tests/markdown.rs index 149aedda0..bb9a98b19 100644 --- a/starlark/src/docs/tests/markdown.rs +++ b/starlark/src/docs/tests/markdown.rs @@ -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 { + fn attr1<'v>(this: Value<'v>) -> starlark::Result { Ok("attr1".to_owned()) } #[starlark(attribute)] - fn attr2<'v>(this: Value<'v>) -> anyhow::Result { + fn attr2<'v>(this: Value<'v>) -> starlark::Result { Ok("attr2".to_owned()) } diff --git a/starlark/src/stdlib.rs b/starlark/src/stdlib.rs index 4984e2737..6e30f1a88 100644 --- a/starlark/src/stdlib.rs +++ b/starlark/src/stdlib.rs @@ -239,7 +239,7 @@ mod tests { #[starlark_module] fn methods(builder: &mut MethodsBuilder) { #[starlark(attribute)] - fn invert1(this: Bool2) -> anyhow::Result { + fn invert1(this: Bool2) -> starlark::Result { Ok(Bool2(!this.0)) } diff --git a/starlark/src/stdlib/call_stack.rs b/starlark/src/stdlib/call_stack.rs index 1d6e2cf26..12bb797f8 100644 --- a/starlark/src/stdlib/call_stack.rs +++ b/starlark/src/stdlib/call_stack.rs @@ -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 { + fn func_name(this: &StackFrame) -> starlark::Result { 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> { + fn module_path(this: &StackFrame) -> starlark::Result> { match this.location { Some(ref location) => Ok(NoneOr::Other(location.file.filename().to_owned())), None => Ok(NoneOr::None), diff --git a/starlark/src/tests/derive/docs.rs b/starlark/src/tests/derive/docs.rs index 50a46404f..3842a355a 100644 --- a/starlark/src/tests/derive/docs.rs +++ b/starlark/src/tests/derive/docs.rs @@ -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 { + fn foo(this: &TestExample) -> starlark::Result { Ok("foo".to_owned()) } } diff --git a/starlark/src/tests/derive/module/other_attributes.rs b/starlark/src/tests/derive/module/other_attributes.rs index 0744aa46b..e63df7306 100644 --- a/starlark/src/tests/derive/module/other_attributes.rs +++ b/starlark/src/tests/derive/module/other_attributes.rs @@ -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 { + ) -> starlark::Result { Ok(NoneType) } } diff --git a/starlark/src/tests/derive/module/return_impl.rs b/starlark/src/tests/derive/module/return_impl.rs index 925e7e884..a5421c479 100644 --- a/starlark/src/tests/derive/module/return_impl.rs +++ b/starlark/src/tests/derive/module/return_impl.rs @@ -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> { + fn attr<'v>(this: Value<'v>) -> starlark::Result> { Ok(this) } } diff --git a/starlark/src/values/type_repr.rs b/starlark/src/values/type_repr.rs index f11b16618..dcea6280f 100644 --- a/starlark/src/values/type_repr.rs +++ b/starlark/src/values/type_repr.rs @@ -133,8 +133,8 @@ impl 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, +pub fn type_repr_from_attr_impl<'v, T: StarlarkTypeRepr, E>( + _f: fn(Value<'v>, &'v Heap) -> Result, ) -> Ty { T::starlark_type_repr() } diff --git a/starlark/src/values/types/enumeration/enum_type.rs b/starlark/src/values/types/enumeration/enum_type.rs index 116af1648..168f1ebac 100644 --- a/starlark/src/values/types/enumeration/enum_type.rs +++ b/starlark/src/values/types/enumeration/enum_type.rs @@ -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> { + fn r#type<'v>(this: Value, heap: &Heap) -> starlark::Result> { let this = EnumType::from_value(this).unwrap(); let ty_enum_type = match this { Either::Left(x) => x.ty_enum_data(), diff --git a/starlark/src/values/types/enumeration/value.rs b/starlark/src/values/types/enumeration/value.rs index 8d087853f..1310b1b99 100644 --- a/starlark/src/values/types/enumeration/value.rs +++ b/starlark/src/values/types/enumeration/value.rs @@ -146,12 +146,12 @@ impl<'v, V: ValueLike<'v>> serde::Serialize for EnumValueGen { #[starlark_module] fn enum_value_methods(methods: &mut MethodsBuilder) { #[starlark(attribute)] - fn index(this: &EnumValue) -> anyhow::Result { + fn index(this: &EnumValue) -> starlark::Result { Ok(this.index) } #[starlark(attribute)] - fn value<'v>(this: &EnumValue<'v>) -> anyhow::Result> { + fn value<'v>(this: &EnumValue<'v>) -> starlark::Result> { Ok(this.value.to_value()) } } diff --git a/starlark/src/values/types/record/record_type.rs b/starlark/src/values/types/record/record_type.rs index dcdadc173..771462d6a 100644 --- a/starlark/src/values/types/record/record_type.rs +++ b/starlark/src/values/types/record/record_type.rs @@ -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(),