Skip to content

Commit

Permalink
Fix #29: accept pub modifiers in fn definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Sep 5, 2017
1 parent b575166 commit 96476ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ mod parse_generics_shim_util;
macro_rules! contract {
(
$(#[$attribute: meta])*
fn $fn_name: ident $($tail: tt)*
$(pub$(($access_modifier: ident))*)* fn $fn_name: ident $($tail: tt)*
) => {
parse_generics_shim! {
{ constr },
then contract!(@after_bracket_generics, $(#[$attribute])* $fn_name,),
then contract!(@after_bracket_generics, $(#[$attribute])* $(pub$(($access_modifier))*)* fn $fn_name,),
$($tail)*
}
};
(
@after_bracket_generics,
$(#[$attribute: meta])* $fn_name: ident,
$(#[$attribute: meta])* $(pub$(($access_modifier: ident))*)* fn $fn_name: ident,
{
constr: [$($constr: tt)*],
},
Expand All @@ -87,7 +87,7 @@ macro_rules! contract {
{ clause, preds },
then contract!(
@after_where_generics,
$(#[$attribute])* $fn_name,
$(#[$attribute])* $(pub$(($access_modifier))*)* fn $fn_name,
{
constr: [$($constr)*],
},
Expand All @@ -98,7 +98,7 @@ macro_rules! contract {
};
(
@after_bracket_generics,
$(#[$attribute: meta])* $fn_name: ident,
$(#[$attribute: meta])* $(pub$(($access_modifier: ident))*)* fn $fn_name: ident,
{
constr: [$($constr: tt)*],
},
Expand All @@ -110,7 +110,7 @@ macro_rules! contract {
) => {
contract! {
@after_where_generics,
$(#[$attribute])* $fn_name,
$(#[$attribute])* $(pub$(($access_modifier))*)* fn $fn_name,
{
constr: [$($constr)*],
},
Expand All @@ -127,7 +127,7 @@ macro_rules! contract {
};
(
@after_where_generics,
$(#[$attribute: meta])* $fn_name: ident,
$(#[$attribute: meta])* $(pub$(($access_modifier: ident))*)* fn $fn_name: ident,
{
constr: [$($constr: tt)*],
},
Expand All @@ -141,7 +141,7 @@ macro_rules! contract {
}
$($tail: tt)*
) => {
$(#[$attribute])* fn $fn_name <$($constr)*> $args $( -> $return_type )* $($where_clause)* {
$(#[$attribute])* $(pub$(($access_modifier))*)* fn $fn_name <$($constr)*> $args $( -> $return_type )* $($where_clause)* {
contract_body! {
(pre {}, body {}, post (def) {}, invariant {})
$($block)*
Expand Down
23 changes: 18 additions & 5 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ fn structs() {

impl Counter {
contract! {
fn new() -> Counter {
pub fn new() -> Counter {
body {
Counter {
count: 0
}
}
}

fn increment(&mut self) {
pub fn increment(&mut self) {
pre {
assert!(self.count != u64::max_value(), "cannot increment counter with max value");
}
Expand All @@ -178,7 +178,7 @@ fn structs() {
}
}

fn decrement(&mut self) {
pub fn decrement(&mut self) {
pre {
assert!(self.count != 0, "cannot decrement counter with count of 0");
}
Expand All @@ -187,13 +187,13 @@ fn structs() {
}
}

fn borrow_count(&self) -> &u64 {
pub fn borrow_count(&self) -> &u64 {
body {
&self.count
}
}

fn consume(self) -> u64 {
pub fn consume(self) -> u64 {
body {
self.count
}
Expand Down Expand Up @@ -235,3 +235,16 @@ fn generics() {

assert!(add_together2(2, 4) == 6, "add impl broken (!?)");
}

#[test]
fn visibility() {
contract! {
fn no_pub() {}
pub fn just_pub() {}
pub(crate) fn pub_crate() {}
}

no_pub();
just_pub();
pub_crate();
}

0 comments on commit 96476ff

Please sign in to comment.