Skip to content

Commit

Permalink
Support parsing 'unsafe mod'
Browse files Browse the repository at this point in the history
This too was added in Rust 1.48 by
rust-lang/rust#75857
  • Loading branch information
adetaylor committed Dec 5, 2020
1 parent 2308ad8 commit c407c83
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/gen/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ impl Clone for ItemMod {
ItemMod {
attrs: self.attrs.clone(),
vis: self.vis.clone(),
unsafety: self.unsafety.clone(),
mod_token: self.mod_token.clone(),
ident: self.ident.clone(),
content: self.content.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/gen/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,7 @@ impl Debug for ItemMod {
let mut formatter = formatter.debug_struct("ItemMod");
formatter.field("attrs", &self.attrs);
formatter.field("vis", &self.vis);
formatter.field("unsafety", &self.unsafety);
formatter.field("mod_token", &self.mod_token);
formatter.field("ident", &self.ident);
formatter.field("content", &self.content);
Expand Down
1 change: 1 addition & 0 deletions src/gen/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ impl PartialEq for ItemMod {
fn eq(&self, other: &Self) -> bool {
self.attrs == other.attrs
&& self.vis == other.vis
&& self.unsafety == other.unsafety
&& self.ident == other.ident
&& self.content == other.content
&& self.semi == other.semi
Expand Down
1 change: 1 addition & 0 deletions src/gen/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,7 @@ where
ItemMod {
attrs: FoldHelper::lift(node.attrs, |it| f.fold_attribute(it)),
vis: f.fold_visibility(node.vis),
unsafety: (node.unsafety).map(|it| Token![unsafe](tokens_helper(f, &it.span))),
mod_token: Token![mod](tokens_helper(f, &node.mod_token.span)),
ident: f.fold_ident(node.ident),
content: (node.content).map(|it| {
Expand Down
1 change: 1 addition & 0 deletions src/gen/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,7 @@ impl Hash for ItemMod {
{
self.attrs.hash(state);
self.vis.hash(state);
self.unsafety.hash(state);
self.ident.hash(state);
self.content.hash(state);
self.semi.hash(state);
Expand Down
5 changes: 5 additions & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ ast_struct! {
pub struct ItemMod {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub unsafety: Option<Token![unsafe]>,
pub mod_token: Token![mod],
pub ident: Ident,
pub content: Option<(token::Brace, Vec<Item>)>,
Expand Down Expand Up @@ -1580,6 +1581,7 @@ pub mod parsing {
fn parse(input: ParseStream) -> Result<Self> {
let outer_attrs = input.call(Attribute::parse_outer)?;
let vis: Visibility = input.parse()?;
let unsafety = input.parse()?;
let mod_token: Token![mod] = input.parse()?;
let ident: Ident = input.parse()?;

Expand All @@ -1588,6 +1590,7 @@ pub mod parsing {
Ok(ItemMod {
attrs: outer_attrs,
vis,
unsafety,
mod_token,
ident,
content: None,
Expand All @@ -1606,6 +1609,7 @@ pub mod parsing {
Ok(ItemMod {
attrs: private::attrs(outer_attrs, inner_attrs),
vis,
unsafety,
mod_token,
ident,
content: Some((brace_token, items)),
Expand Down Expand Up @@ -2761,6 +2765,7 @@ mod printing {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
self.vis.to_tokens(tokens);
self.unsafety.to_tokens(tokens);
self.mod_token.to_tokens(tokens);
self.ident.to_tokens(tokens);
if let Some((brace, items)) = &self.content {
Expand Down
12 changes: 12 additions & 0 deletions tests/debug/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3183,6 +3183,18 @@ impl Debug for Lite<syn::ItemMod> {
formatter.field("attrs", Lite(&_val.attrs));
}
formatter.field("vis", Lite(&_val.vis));
if let Some(val) = &_val.unsafety {
#[derive(RefCast)]
#[repr(transparent)]
struct Print(syn::token::Unsafe);
impl Debug for Print {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Some")?;
Ok(())
}
}
formatter.field("unsafety", Print::ref_cast(val));
}
formatter.field("ident", Lite(&_val.ident));
if let Some(val) = &_val.content {
#[derive(RefCast)]
Expand Down

0 comments on commit c407c83

Please sign in to comment.