Skip to content

Commit

Permalink
pub incorrectly consuming a field type (#78)
Browse files Browse the repository at this point in the history
* Fixed issue 77; pub incorrectly consuming a field type

* Fixed typo in type check

* Added specific check for pub(crate | self | super | in)

---------

Co-authored-by: Victor Koenders <[email protected]>
  • Loading branch information
VictorKoenders and VictorKoenders authored Jan 16, 2024
1 parent de08068 commit 7c390ea
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
30 changes: 30 additions & 0 deletions src/parse/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ fn test_struct_body_take() {
}
}

#[test]
fn issue_77() {
// https://github.com/bincode-org/virtue/issues/77
use crate::token_stream;

let stream = &mut token_stream("struct Test(pub [u8; 32])");
let (data_type, ident) = super::DataType::take(stream).unwrap();
assert_eq!(data_type, super::DataType::Struct);
assert_eq!(ident, "Test");
let body = StructBody::take(stream).unwrap();
let fields = body.fields.unwrap();
let Fields::Tuple(t) = fields else {
panic!("Fields is not a tuple")
};
assert_eq!(t.len(), 1);
assert_eq!(t[0].r#type[0].to_string(), "[u8 ; 32]");

let stream = &mut token_stream("struct Foo(pub (u8, ))");
let (data_type, ident) = super::DataType::take(stream).unwrap();
assert_eq!(data_type, super::DataType::Struct);
assert_eq!(ident, "Foo");
let body = StructBody::take(stream).unwrap();
let fields = body.fields.unwrap();
let Fields::Tuple(t) = fields else {
panic!("Fields is not a tuple")
};
assert_eq!(t.len(), 1);
assert_eq!(t[0].r#type[0].to_string(), "(u8 ,)");
}

/// The body of an enum
#[derive(Debug)]
pub struct EnumBody {
Expand Down
19 changes: 15 additions & 4 deletions src/parse/visibility.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::utils::*;
use crate::prelude::TokenTree;
use crate::prelude::{Delimiter, TokenTree};
use crate::Result;
use std::iter::Peekable;

Expand All @@ -21,9 +21,20 @@ impl Visibility {
assume_ident(input.next());

// check if the next token is `pub(...)`
if let Some(TokenTree::Group(_)) = input.peek() {
// we just consume the visibility, we're not actually using it for generation
assume_group(input.next());
if let Some(TokenTree::Group(g)) = input.peek() {
if g.delimiter() == Delimiter::Parenthesis {
// check if this is one of:
// - pub ( crate )
// - pub ( self )
// - pub ( super )
// - pub ( in ... )
if let Some(TokenTree::Ident(i)) = g.stream().into_iter().next() {
if matches!(i.to_string().as_str(), "crate" | "self" | "super" | "in") {
// it is, ignore this token
assume_group(input.next());
}
}
}
}

Ok(Visibility::Pub)
Expand Down

0 comments on commit 7c390ea

Please sign in to comment.