Skip to content

Commit

Permalink
Support parsing 'unsafe extern "C++"'
Browse files Browse the repository at this point in the history
This was added in Rust 1.48 by
rust-lang/rust#75857
  • Loading branch information
adetaylor committed Dec 5, 2020
1 parent da1818e commit 2308ad8
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/gen/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ impl Clone for ItemForeignMod {
fn clone(&self) -> Self {
ItemForeignMod {
attrs: self.attrs.clone(),
unsafety: self.unsafety.clone(),
abi: self.abi.clone(),
brace_token: self.brace_token.clone(),
items: self.items.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 @@ -1599,6 +1599,7 @@ impl Debug for ItemForeignMod {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let mut formatter = formatter.debug_struct("ItemForeignMod");
formatter.field("attrs", &self.attrs);
formatter.field("unsafety", &self.unsafety);
formatter.field("abi", &self.abi);
formatter.field("brace_token", &self.brace_token);
formatter.field("items", &self.items);
Expand Down
2 changes: 1 addition & 1 deletion src/gen/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ impl Eq for ItemForeignMod {}
#[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
impl PartialEq for ItemForeignMod {
fn eq(&self, other: &Self) -> bool {
self.attrs == other.attrs && self.abi == other.abi && self.items == other.items
self.attrs == other.attrs && self.unsafety == other.unsafety && self.abi == other.abi && self.items == other.items
}
}
#[cfg(feature = "full")]
Expand Down
1 change: 1 addition & 0 deletions src/gen/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,7 @@ where
{
ItemForeignMod {
attrs: FoldHelper::lift(node.attrs, |it| f.fold_attribute(it)),
unsafety: (node.unsafety).map(|it| Token![unsafe](tokens_helper(f, &it.span))),
abi: f.fold_abi(node.abi),
brace_token: Brace(tokens_helper(f, &node.brace_token.span)),
items: FoldHelper::lift(node.items, |it| f.fold_foreign_item(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 @@ -1482,6 +1482,7 @@ impl Hash for ItemForeignMod {
H: Hasher,
{
self.attrs.hash(state);
self.unsafety.hash(state);
self.abi.hash(state);
self.items.hash(state);
}
Expand Down
4 changes: 4 additions & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ ast_struct! {
#[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
pub struct ItemForeignMod {
pub attrs: Vec<Attribute>,
pub unsafety: Option<Token![unsafe]>,
pub abi: Abi,
pub brace_token: token::Brace,
pub items: Vec<ForeignItem>,
Expand Down Expand Up @@ -1620,6 +1621,7 @@ pub mod parsing {
impl Parse for ItemForeignMod {
fn parse(input: ParseStream) -> Result<Self> {
let outer_attrs = input.call(Attribute::parse_outer)?;
let unsafety = input.parse()?;
let abi: Abi = input.parse()?;

let content;
Expand All @@ -1632,6 +1634,7 @@ pub mod parsing {

Ok(ItemForeignMod {
attrs: private::attrs(outer_attrs, inner_attrs),
unsafety,
abi,
brace_token,
items,
Expand Down Expand Up @@ -2775,6 +2778,7 @@ mod printing {
impl ToTokens for ItemForeignMod {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
self.unsafety.to_tokens(tokens);
self.abi.to_tokens(tokens);
self.brace_token.surround(tokens, |tokens| {
tokens.append_all(self.attrs.inner());
Expand Down
12 changes: 12 additions & 0 deletions tests/debug/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3027,6 +3027,18 @@ impl Debug for Lite<syn::ItemForeignMod> {
if !_val.attrs.is_empty() {
formatter.field("attrs", Lite(&_val.attrs));
}
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("abi", Lite(&_val.abi));
if !_val.items.is_empty() {
formatter.field("items", Lite(&_val.items));
Expand Down

0 comments on commit 2308ad8

Please sign in to comment.