Skip to content

Commit

Permalink
Merge pull request #222 from mobusoperandi/no-panics
Browse files Browse the repository at this point in the history
feat: better error when fn with default return type
  • Loading branch information
josemanuelp authored Jun 15, 2022
2 parents d1d3616 + 4ddeb42 commit 108b107
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn expand(args: TokenStream, input: TokenStream) -> syn::Result<ImplItemMethod>
signature_constness_none(&method.sig)?;
let mut expanded_fn = method.clone();
let original_fn_block = method.block;
let return_type = obtain_return_type(method.sig.output);
let return_type = obtain_return_type(method.sig.output)?;
expanded_fn.block = expand_fn_block(original_fn_block, return_type, attr_args);
Ok(expanded_fn)
}
Expand All @@ -34,10 +34,13 @@ struct AttrArgs {
store_type: Option<Type>,
store_init: Option<Expr>,
}
fn obtain_return_type(return_type: ReturnType) -> Type {
fn obtain_return_type(return_type: ReturnType) -> syn::Result<Type> {
match return_type {
syn::ReturnType::Type(_, return_type) => *return_type,
syn::ReturnType::Default => unimplemented!("default return types are not supported"),
syn::ReturnType::Type(_, return_type) => Ok(*return_type),
syn::ReturnType::Default => Err(syn::Error::new(
Span::call_site(),
"default return types are not supported",
)),
}
}
fn expand_fn_block(original_fn_block: Block, return_type: Type, attr_args: AttrArgs) -> Block {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: custom attribute panicked
error: default return types are not supported
--> tests/compile_fail/on_a_function_with_default_return_type.rs:3:1
|
3 | #[memoized(key_expr = ())]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: message: not implemented: default return types are not supported
= note: this error originates in the attribute macro `memoized` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 108b107

Please sign in to comment.