diff --git a/crates/moxy-ast/src/item/item_extern_crate.rs b/crates/moxy-ast/src/item/item_extern_crate.rs index 1bfe42fd..1444b450 100644 --- a/crates/moxy-ast/src/item/item_extern_crate.rs +++ b/crates/moxy-ast/src/item/item_extern_crate.rs @@ -35,7 +35,11 @@ impl Parse for ItemExternCrate { let vis = <_ as Parse>::parse(parser)?; let extern_keyword = <_ as Parse>::parse(parser)?; let crate_keyword = <_ as Parse>::parse(parser)?; - let ident = <_ as Parse>::parse(parser)?; + let ident = if ::peek(parser.cursor()) { + parser.parse_ident_any()? + } else { + <_ as Parse>::parse(parser)? + }; let (as_keyword, rename) = if ::peek(parser.cursor()) { let as_keyword = <_ as Parse>::parse(parser)?; let rename = <_ as Parse>::parse(parser)?; @@ -63,7 +67,11 @@ impl Parse for ItemExternCrate { cursor = Visibility::skip(cursor)?; cursor = ::skip(cursor)?; cursor = ::skip(cursor)?; - cursor = Ident::skip(cursor)?; + cursor = if ::peek(cursor) { + ::skip(cursor)? + } else { + Ident::skip(cursor)? + }; if ::peek(cursor) { cursor = ::skip(cursor)?; diff --git a/crates/moxy-ast/src/item/item_impl.rs b/crates/moxy-ast/src/item/item_impl.rs index eea1a3af..f9b0a502 100644 --- a/crates/moxy-ast/src/item/item_impl.rs +++ b/crates/moxy-ast/src/item/item_impl.rs @@ -51,7 +51,7 @@ impl Parse for ItemImpl { let unsafety = <_ as Parse>::parse(parser)?; let impl_keyword = <_ as Parse>::parse(parser)?; let mut generics: Generics = <_ as Parse>::parse(parser)?; - let polarity = <_ as Parse>::parse(parser)?; + let polarity: Option = <_ as Parse>::parse(parser)?; let first = <_ as Parse>::parse(parser)?; let (for_keyword, trait_ref, self_ty) = if ::peek(parser.cursor()) { @@ -59,6 +59,10 @@ impl Parse for ItemImpl { let self_ty = <_ as Parse>::parse(parser)?; (Some(for_keyword), Some(Self::type_to_trait_ref(first, polarity)?), self_ty) } else { + if let Some(bang) = polarity { + return Err(ParseError::new(bang.span(), "an inherent impl cannot be negative")); + } + (None, None, first) }; @@ -84,12 +88,15 @@ impl Parse for ItemImpl { cursor = Option::::skip(cursor)?; cursor = ::skip(cursor)?; cursor = Generics::skip(cursor)?; + let negative = ::peek(cursor); cursor = Option::::skip(cursor)?; cursor = Type::skip(cursor)?; if ::peek(cursor) { cursor = ::skip(cursor)?; cursor = Type::skip(cursor)?; + } else if negative { + return None; } cursor = Option::::skip(cursor)?; diff --git a/crates/moxy-ast/src/item/item_trait_alias.rs b/crates/moxy-ast/src/item/item_trait_alias.rs index 4b1b1cf9..a9160b4b 100644 --- a/crates/moxy-ast/src/item/item_trait_alias.rs +++ b/crates/moxy-ast/src/item/item_trait_alias.rs @@ -27,8 +27,6 @@ impl Parse for ItemTraitAlias { fn peek(cursor: crate::Cursor<'_>) -> bool { let cursor = Attributes::skip(cursor).unwrap_or(cursor); let cursor = Visibility::skip(cursor).unwrap_or(cursor); - let cursor = Option::::skip(cursor).unwrap_or(cursor); - let cursor = Option::::skip(cursor).unwrap_or(cursor); let Some(cursor) = ::skip(cursor) else { return false; }; @@ -47,13 +45,6 @@ impl Parse for ItemTraitAlias { fn parse(parser: &Parser) -> Result { let attrs = <_ as Parse>::parse(parser)?; let vis = <_ as Parse>::parse(parser)?; - let _unsafety: Option = <_ as Parse>::parse(parser)?; - - // skip optional `auto` - if ::peek(parser.cursor()) { - let _: Token![auto] = <_ as Parse>::parse(parser)?; - } - let trait_keyword = <_ as Parse>::parse(parser)?; let ident = <_ as Parse>::parse(parser)?; let generics = <_ as Parse>::parse(parser)?; @@ -76,8 +67,6 @@ impl Parse for ItemTraitAlias { fn skip(mut cursor: crate::Cursor<'_>) -> Option> { cursor = Attributes::skip(cursor)?; cursor = Visibility::skip(cursor)?; - cursor = Option::::skip(cursor)?; - cursor = Option::::skip(cursor)?; cursor = ::skip(cursor)?; cursor = Ident::skip(cursor)?; cursor = Generics::skip(cursor)?; diff --git a/tests/externs.rs b/tests/externs.rs index 7950e4dc..df1a2652 100644 --- a/tests/externs.rs +++ b/tests/externs.rs @@ -1,4 +1,5 @@ -use moxy::ast::Item; +use moxy::ast::{Item, Parse, Parser}; +use moxy::token::TokenStream; #[test] fn extern_crate_aliases_are_parsed_and_rendered() { @@ -9,6 +10,32 @@ fn extern_crate_aliases_are_parsed_and_rendered() { assert_eq!(moxy::fmt!(&item).unwrap(), "extern crate core as rust_core;"); } +#[test] +fn extern_crate_self_can_be_renamed() { + let source = "extern crate self as local;"; + let tokens: TokenStream = source.parse().unwrap(); + let parser = Parser::from_tokens(&tokens); + + let item: Item = moxy::parse!(source).unwrap(); + let external = item.as_extern_crate().unwrap(); + + assert_eq!(external.ident.text(), "self"); + assert_eq!(external.rename.as_ref().unwrap().text(), "local"); + assert_eq!(moxy::fmt!(&item).unwrap(), source); + assert!(Item::skip(parser.cursor()).unwrap().is_empty()); +} + +#[test] +fn extern_crate_rejects_other_keywords() { + for source in ["extern crate fn;", "extern crate super;"] { + let tokens: TokenStream = source.parse().unwrap(); + let parser = Parser::from_tokens(&tokens); + + assert!(moxy::parse!(source as Item).is_err(), "{source}"); + assert!(Item::skip(parser.cursor()).is_none(), "{source}"); + } +} + #[test] fn foreign_modules_preserve_functions_statics_types_and_macros() { let item: Item = diff --git a/tests/implementations.rs b/tests/implementations.rs index fd56e7c3..461cb918 100644 --- a/tests/implementations.rs +++ b/tests/implementations.rs @@ -1,4 +1,5 @@ -use moxy::ast::Item; +use moxy::ast::{Item, Parse, Parser}; +use moxy::token::TokenStream; #[test] fn inherent_implementations_preserve_members_and_render_layout() { @@ -32,3 +33,14 @@ fn implementations_preserve_where_clauses_when_formatted() { "impl Example\nwhere\nT: Copy {\n\tfn value(&self) -> T {\n\t\tself.0\n\t}\n}" ); } + +#[test] +fn inherent_impls_reject_polarity() { + let source = "impl !Record {}"; + let tokens: TokenStream = source.parse().unwrap(); + let parser = Parser::from_tokens(&tokens); + + let error = moxy::parse!(source as Item).err().unwrap(); + assert_eq!(error.message(), "an inherent impl cannot be negative"); + assert!(Item::skip(parser.cursor()).is_none()); +} diff --git a/tests/traits.rs b/tests/traits.rs index d185ce99..22b3c004 100644 --- a/tests/traits.rs +++ b/tests/traits.rs @@ -1,4 +1,5 @@ -use moxy::ast::Item; +use moxy::ast::{Item, Parse, Parser}; +use moxy::token::TokenStream; #[test] fn traits_preserve_supertraits_and_every_member_family() { @@ -39,3 +40,14 @@ fn trait_aliases_render_bounds_with_canonical_spacing() { assert_eq!(alias.bounds.len(), 3); assert_eq!(moxy::fmt!(&item).unwrap(), "pub trait Portable = Clone + Send + 'static;"); } + +#[test] +fn trait_aliases_reject_unsafe_and_auto() { + for source in ["unsafe trait A = B;", "auto trait A = B;"] { + let tokens: TokenStream = source.parse().unwrap(); + let parser = Parser::from_tokens(&tokens); + + assert!(moxy::parse!(source as Item).is_err(), "{source}"); + assert!(Item::skip(parser.cursor()).is_none(), "{source}"); + } +}