Skip to content

Commit

Permalink
Merge pull request #4 from Nemo157/impl-trait
Browse files Browse the repository at this point in the history
Fix usage with impl trait returning functions
  • Loading branch information
Nemo157 authored Jun 29, 2023
2 parents 9348b56 + aa15f07 commit 9ae83c7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "culpa"
version = "1.0.0"
version = "1.0.1"
edition = "2018"
license = "MIT OR Apache-2.0"

Expand All @@ -10,6 +10,6 @@ keywords = ["error-handling", "exceptions"]

[dependencies.culpa-macros]
path = "macros"
version = "=1.0.0"
version = "=1.0.1"

[workspace]
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "culpa-macros"
version = "1.0.0"
version = "1.0.1"
edition = "2018"
license = "MIT OR Apache-2.0"

Expand Down
15 changes: 12 additions & 3 deletions macros/src/throws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ use crate::Args;
pub struct Throws {
args: Args,
outer_fn: bool,
return_type: Box<syn::Type>,
return_type: syn::Type,
}

impl Throws {
pub fn new(args: Args) -> Throws {
Throws {
args,
outer_fn: true,
return_type: Box::new(syn::parse_quote!(())),
return_type: syn::parse_quote!(()),
}
}

Expand Down Expand Up @@ -112,7 +112,16 @@ impl Fold for Throws {
}
let return_type = self.args.ret(i);
let syn::ReturnType::Type(_, ty) = &return_type else { unreachable!() };
self.return_type = ty.clone();
struct ImplTraitToInfer;
impl Fold for ImplTraitToInfer {
fn fold_type(&mut self, i: syn::Type) -> syn::Type {
match i {
syn::Type::ImplTrait(_) => syn::Type::Infer(syn::parse_quote!(_)),
i => syn::fold::fold_type(self, i),
}
}
}
self.return_type = ImplTraitToInfer.fold_type(ty.as_ref().clone());
return_type
}

Expand Down
3 changes: 3 additions & 0 deletions tests/throws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ pub fn let_else(a: Option<u8>) -> u8 {
a
}

#[throws]
pub fn impl_trait() -> impl std::fmt::Debug {}

#[throws(i32)]
#[deny(unreachable_code)]
pub fn unreachable() {
Expand Down

0 comments on commit 9ae83c7

Please sign in to comment.