Skip to content

Commit

Permalink
Merge pull request #203 from dtolnay/fn
Browse files Browse the repository at this point in the history
Parse unsafe and extern fn pointer types
  • Loading branch information
dtolnay authored Nov 27, 2021
2 parents 6e1e0cc + 9bbd883 commit 210d741
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/ensure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ macro_rules! __parse_ensure {
$crate::__parse_ensure!(type $stack $bail ($($fuel)*) {($($buf)* $and) $($parse)*} ($($rest)*) $($rest)*)
};

(type $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($unsafe:tt $(extern $($abi:literal)?)? fn $($dup:tt)*) unsafe $($rest:tt)*) => {
$crate::__parse_ensure!(type $stack $bail ($($fuel)*) {($($buf)* $unsafe) $($parse)*} ($($rest)*) $($rest)*)
};

(type $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($extern:tt $abi:literal fn $($dup:tt)*) extern $lit:literal $($rest:tt)*) => {
$crate::__parse_ensure!(type $stack $bail ($($fuel)*) {($($buf)* $extern $abi) $($parse)*} ($($rest)*) $($rest)*)
};

(type $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($extern:tt fn $($dup:tt)*) extern $($rest:tt)*) => {
$crate::__parse_ensure!(type $stack $bail ($($fuel)*) {($($buf)* $extern) $($parse)*} ($($rest)*) $($rest)*)
};

(type $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($fn:tt $paren:tt $arrow:tt $($dup:tt)*) fn ($($args:tt)*) -> $($rest:tt)*) => {
$crate::__parse_ensure!(type $stack $bail ($($fuel)*) {($($buf)* $fn $paren $arrow) $($parse)*} ($($rest)*) $($rest)*)
};
Expand Down
22 changes: 22 additions & 0 deletions tests/test_ensure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
clippy::never_loop,
clippy::redundant_closure_call,
clippy::redundant_pattern_matching,
clippy::too_many_lines,
clippy::unit_arg,
clippy::while_immutable_condition,
clippy::zero_ptr
Expand Down Expand Up @@ -507,6 +508,27 @@ fn test_as() {
"Condition failed: `f as for<'a>fn() as usize * 0 != 0` (0 vs 0)",
);

let test = || Ok(ensure!(f as unsafe fn() as usize * 0 != 0));
assert_err(
test,
"Condition failed: `f as unsafe fn() as usize * 0 != 0` (0 vs 0)",
);

#[rustfmt::skip]
let test = || Ok(ensure!(f as extern "Rust" fn() as usize * 0 != 0));
assert_err(
test,
"Condition failed: `f as extern \"Rust\" fn() as usize * 0 != 0` (0 vs 0)",
);

extern "C" fn extern_fn() {}
#[rustfmt::skip]
let test = || Ok(ensure!(extern_fn as extern fn() as usize * 0 != 0));
assert_err(
test,
"Condition failed: `extern_fn as extern fn() as usize * 0 != 0` (0 vs 0)",
);

let f = || -> ! { panic!() };
let test = || Ok(ensure!(f as fn() -> ! as usize * 0 != 0));
assert_err(
Expand Down

0 comments on commit 210d741

Please sign in to comment.