Skip to content

Commit

Permalink
feat: better error when fn with default return type
Browse files Browse the repository at this point in the history
Co-authored-by: Vimal Patel <mailtovimal@gmail.com>
Co-authored-by: José Manuel Peña <josemanuelp2@gmail.com>
  • Loading branch information
3 people committed Jun 15, 2022
1 parent d1d3616 commit 4ddeb42
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
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
@@ -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 4ddeb42

Please sign in to comment.