Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Box<dyn FnOnce> trailing comma #113

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions crates/swift-bridge-ir/src/bridged_type/boxed_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ impl BuiltInBoxedFnOnce {
let args: FunctionArguments = syn::parse2(args).unwrap();

let ret = if let Some(ret) = ret {
// Parse out the comma in:
// Box<dyn FnOnce() -> (),>
let ret = ret.trim_end_matches(",");

let ret = syn::parse2::<Type>(TokenStream::from_str(ret).unwrap()).unwrap();
BridgedType::new_with_type(&ret, types)?
} else {
Expand Down Expand Up @@ -327,4 +331,25 @@ mod tests {
.is_null(),
);
}

/// Verify that we can parse a boxed fn that has a comma after the FnOnce.
/// rustfmt adds a trailing comma when it puts a long function signature on its own line.
#[test]
fn comma_after_fn_once() {
let tests = vec![
quote! {Box<dyn FnOnce(),>},
quote! {Box<dyn FnOnce() -> (),>},
];

for test in tests {
let tokens = test.to_token_stream().to_string();

assert!(
BuiltInBoxedFnOnce::from_str_tokens(&tokens, &TypeDeclarations::default())
.unwrap()
.ret
.is_null(),
);
}
}
}