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

Add test of unnamed tuple struct field with inherited visibility #1601

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 49 additions & 8 deletions tests/test_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
mod macros;

use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{DeriveInput, Result, Visibility};

Expand Down Expand Up @@ -100,7 +101,7 @@ fn test_junk_after_in() {
}

#[test]
fn test_empty_group_vis() {
fn test_inherited_vis_named_field() {
// mimics `struct S { $vis $field: () }` where $vis is empty
let tokens = TokenStream::from_iter(vec![
TokenTree::Ident(Ident::new("struct", Span::call_site())),
Expand All @@ -109,13 +110,7 @@ fn test_empty_group_vis() {
Delimiter::Brace,
TokenStream::from_iter(vec![
TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())),
TokenTree::Group(Group::new(
Delimiter::None,
TokenStream::from_iter(vec![TokenTree::Ident(Ident::new(
"f",
Span::call_site(),
))]),
)),
TokenTree::Group(Group::new(Delimiter::None, quote!(f))),
TokenTree::Punct(Punct::new(':', Spacing::Alone)),
TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
]),
Expand All @@ -142,3 +137,49 @@ fn test_empty_group_vis() {
}
"###);
}

#[test]
fn test_inherited_vis_unnamed_field() {
// mimics `struct S($vis $ty);` where $vis is empty
let tokens = TokenStream::from_iter(vec![
TokenTree::Ident(Ident::new("struct", Span::call_site())),
TokenTree::Ident(Ident::new("S", Span::call_site())),
TokenTree::Group(Group::new(
Delimiter::Parenthesis,
TokenStream::from_iter(vec![
TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())),
TokenTree::Group(Group::new(Delimiter::None, quote!(str))),
]),
)),
TokenTree::Punct(Punct::new(';', Spacing::Alone)),
]);

snapshot!(tokens as DeriveInput, @r###"
DeriveInput {
vis: Visibility::Inherited,
ident: "S",
generics: Generics,
data: Data::Struct {
fields: Fields::Unnamed {
unnamed: [
Field {
vis: Visibility::Inherited,
ty: Type::Group {
elem: Type::Path {
path: Path {
segments: [
PathSegment {
ident: "str",
},
],
},
},
},
},
],
},
semi_token: Some,
},
}
"###);
}