Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ on:

jobs:
test:
name: Test (${{ matrix.toolchain }})
name: Test (${{ matrix.name }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
toolchain:
- stable
- nightly
include:
- name: stable
toolchain: 1.98.0
- name: nightly
toolchain: nightly-2026-08-19
steps:
- name: Checkout
uses: actions/checkout@v5
Expand All @@ -53,7 +55,7 @@ jobs:
uses: actions/checkout@v5

- name: Install Rust
uses: dtolnay/rust-toolchain@stable
uses: dtolnay/rust-toolchain@1.98.0
with:
components: clippy

Expand All @@ -71,7 +73,7 @@ jobs:
uses: actions/checkout@v5

- name: Install Rust
uses: dtolnay/rust-toolchain@stable
uses: dtolnay/rust-toolchain@1.98.0

- name: Cache Cargo
uses: Swatinem/rust-cache@v2
Expand Down
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ version = "0.5.6"
edition = "2024"
license = "MIT"
documentation = "https://docs.rs/moxy"
homepage = "https://www.github.com/aacebo/moxy"
repository = "https://www.github.com/aacebo/moxy"
keywords = ["macro", "framework", "proc", "rust"]
categories = ["development-tools::procedural-macro-helpers", "rust-patterns", "no-std"]
Expand All @@ -30,7 +29,6 @@ large_enum_variant = "allow"
suspicious_else_formatting = "allow"

[workspace.dependencies]
moxy = { path = ".", version = "0.5.6" }
moxy-ast = { path = "crates/moxy-ast", version = "0.5.6" }
moxy-build = { path = "crates/moxy-build", version = "0.5.6" }
moxy-macros = { path = "crates/moxy-macros", version = "0.5.6" }
Expand All @@ -53,7 +51,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/moxy-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
12 changes: 8 additions & 4 deletions crates/moxy-ast/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Parse for GenericArgument {
|| token.is_punct_not();

if is_const {
return Ok(Self::Const(<_ as Parse>::parse(parser)?));
return Ok(Self::Const(expr::parse::const_generic_default(parser)?));
}

if token.is_ident() {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Parse for GenericArgument {
};

if is_const {
let expr = <_ as Parse>::parse(parser)?;
let expr = expr::parse::const_generic_default(parser)?;

return Ok(AssocConstArgument {
ident,
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Parse for GenericArgument {
};

if is_const {
return Expr::skip(cursor);
return expr::skip::const_generic_default(cursor);
}

if Ident::peek(cursor) {
Expand Down Expand Up @@ -193,7 +193,11 @@ impl Parse for GenericArgument {
_ => false,
};

return if is_const { Expr::skip(cursor) } else { Type::skip(cursor) };
return if is_const {
expr::skip::const_generic_default(cursor)
} else {
Type::skip(cursor)
};
}

cursor = Path::skip_rest(cursor)?;
Expand Down
1 change: 1 addition & 0 deletions crates/moxy-ast/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ impl ExprContext {
|| <Token![,]>::peek(cursor.offset(1))
|| <Token![;]>::peek(cursor.offset(1))
|| <Token![where]>::peek(cursor.offset(1))
|| cursor.offset(1).is_delimited(Delim::Paren)
|| cursor.offset(1).is_delimited(Delim::Brace))
}
}
1 change: 0 additions & 1 deletion crates/moxy-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/moxy-diagnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/moxy-fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions crates/moxy-fmt/src/ast/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ impl Format for ItemImpl {

self.self_ty.format(f)?;

if let Some(where_clause) = &self.generics.where_clause {
where_clause.format(f)?;
}

f.text(" {")?;
f.indent(|f| {
for (i, item) in self.items.inner.iter().enumerate() {
Expand Down
2 changes: 1 addition & 1 deletion crates/moxy-macro-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ doctest = false
workspace = true

[dependencies]
moxy = { workspace = true, features = ["macros", "proc-macro2", "template"] }
moxy = { path = "../..", version = "0.5.6", features = ["macros", "proc-macro2", "template"] }
proc-macro2.workspace = true
syn.workspace = true

Expand Down
1 change: 0 additions & 1 deletion crates/moxy-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
7 changes: 6 additions & 1 deletion crates/moxy-macros/src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ pub fn expand(tokens: TokenStream) -> TokenStream {
moxy::ast::Declaration::Struct(target) => (&target.ident, &target.generics, structs::expand(target)),
moxy::ast::Declaration::Enum(target) => (&target.ident, &target.generics, enums::expand(target)),
moxy::ast::Declaration::Union(target) => {
return target.span().error("`Meta` cannot be derived for unions").emit();
return target
.fields
.fields
.close()
.error("`Meta` cannot be derived for unions")
.emit();
}
};

Expand Down
15 changes: 11 additions & 4 deletions crates/moxy-macros/src/meta/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ pub struct Variant {

impl Variant {
pub fn parse(variant: &moxy::ast::Variant) -> Result<Self, moxy::ast::ParseError> {
if variant.discriminant.is_some() {
if let Some(discriminant) = &variant.discriminant {
let span = match discriminant {
moxy::ast::Expr::Lit(expr) => expr.lit.span(),
_ => discriminant.span(),
};

return Err(moxy::ast::ParseError::new(
variant.span(),
span,
"`Meta` enum variants cannot have discriminants",
));
}
Expand Down Expand Up @@ -54,8 +59,10 @@ impl Variant {
key = name;
}
MetaRule::Default(value) => {
let span = value.as_ref().map(Spanner::span).unwrap_or_else(|| group.span.close());

return Err(moxy::ast::ParseError::new(
value.as_ref().map(Spanner::span).unwrap_or(attr.span()),
span,
"`default` is not supported on enum variants",
));
}
Expand All @@ -77,7 +84,7 @@ impl Variant {

if values.len() != 1 {
return Err(moxy::ast::ParseError::new(
variant.fields.span(),
fields.fields.close(),
"`Meta` tuple variants must contain exactly one field",
));
}
Expand Down
1 change: 0 additions & 1 deletion crates/moxy-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/moxy-token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version.workspace = true
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
3 changes: 1 addition & 2 deletions examples/builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ version = "0.0.0"
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand All @@ -19,4 +18,4 @@ doctest = false
workspace = true

[dependencies]
moxy = { workspace = true, features = ["macros", "template", "diagnostic"] }
moxy = { path = "../..", version = "0.5.6", features = ["macros", "template", "diagnostic"] }
3 changes: 1 addition & 2 deletions examples/enumerate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ version = "0.0.0"
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand All @@ -19,4 +18,4 @@ doctest = false
workspace = true

[dependencies]
moxy = { workspace = true, features = ["macros", "template", "diagnostic"] }
moxy = { path = "../..", version = "0.5.6", features = ["macros", "template", "diagnostic"] }
3 changes: 1 addition & 2 deletions examples/trace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ version = "0.0.0"
edition.workspace = true
license.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
Expand All @@ -19,4 +18,4 @@ doctest = false
workspace = true

[dependencies]
moxy = { workspace = true, features = ["macros", "template", "diagnostic"] }
moxy = { path = "../..", version = "0.5.6", features = ["macros", "template", "diagnostic"] }
11 changes: 10 additions & 1 deletion fixtures/grammar/generics/const-default/pass.rs
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
struct S<const N: usize = 1>;
pub struct Unit<const N: usize = 0>;

struct Tuple<P, const N: usize = 0>(P);

struct Named<'a, T: 'a, const N: usize = 0, const ENABLED: bool = true> {
value: &'a T,
bytes: [u8; N],
}

struct Multiple<const A: u8 = 0, const B: u8 = { 1 + 1 }>;
2 changes: 1 addition & 1 deletion fixtures/grammar/matrix.json
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@
"tests/ui/parser/"
]
},
"parse_root": "item",
"parse_root": "crate",
"support_expectation": "moxy_accepts",
"fixture_directory": "fixtures/grammar/generics/const-default"
},
Expand Down
6 changes: 6 additions & 0 deletions fixtures/trybuild/derive/fail/meta_discriminant_expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(moxy::FromMeta)]
enum Invalid {
Value = 1 + 1,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `Meta` enum variants cannot have discriminants
--> fixtures/trybuild/derive/fail/meta_discriminant_expression.rs:3:17
|
3 | Value = 1 + 1,
| ^
4 changes: 2 additions & 2 deletions fixtures/trybuild/derive/fail/meta_variant_default.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `default` is not supported on enum variants
--> fixtures/trybuild/derive/fail/meta_variant_default.rs:3:20
--> fixtures/trybuild/derive/fail/meta_variant_default.rs:3:19
|
3 | #[meta(default)]
| ^
| ^
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "stable"
channel = "1.98.0"
components = ["rustfmt", "clippy", "llvm-tools-preview"]
36 changes: 35 additions & 1 deletion tests/generics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use moxy::ast::{GenericParam, Item, WherePredicate};
use moxy::ast::{GenericArgument, GenericParam, Item, PathArguments, Type, WherePredicate};
use moxy::token::{Spanner, ToTokenStream};

#[test]
Expand Down Expand Up @@ -45,3 +45,37 @@ fn higher_ranked_where_predicates_render_on_functions() {
"fn borrow<T>(value: T)\nwhere\nfor<'a> &'a T: IntoIterator,\nT: 'static {}"
);
}

#[test]
fn literal_const_generic_arguments_parse_in_implementations() {
let item: Item = moxy::parse!("impl<P> Builder<P, 0> { fn end(self) -> P { self.0 } }").unwrap();
let implementation = item.as_impl().unwrap();
let Type::Path(self_ty) = &implementation.self_ty else {
panic!("expected a path type");
};
let PathArguments::AngleBracketed(arguments) = &self_ty.path.last().unwrap().args else {
panic!("expected angle-bracketed arguments");
};

assert_eq!(implementation.items.inner.len(), 1);
assert!(matches!(arguments.args[1], GenericArgument::Const(_)));
}

#[test]
fn associated_const_generic_arguments_parse() {
let item: Item = moxy::parse!("impl<P> Builder<P, COUNT = 0> {}").unwrap();

assert_eq!(moxy::fmt!(&item).unwrap(), "impl<P> Builder<P, COUNT = 0> {}");
}

#[test]
fn const_generic_default_permutations_parse() {
for source in [
"pub struct Unit<const N: usize = 0>;",
"struct Tuple<P, const N: usize = 0>(P);",
"struct Named<'a, T: 'a, const N: usize = 0, const ENABLED: bool = true> { value: &'a T, bytes: [u8; N] }",
"struct Multiple<const A: u8 = 0, const B: u8 = { 1 + 1 }>;",
] {
let _: Item = moxy::parse!(source as Item).unwrap_or_else(|err| panic!("failed to parse {source:?}: {err}"));
}
}
12 changes: 12 additions & 0 deletions tests/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ fn unsafe_negative_trait_implementations_expose_polarity() {
assert!(implementation.trait_ref.as_ref().unwrap().polarity.is_some());
assert_eq!(moxy::fmt!(&item).unwrap(), "unsafe impl !Send for Record {}");
}

#[test]
fn implementations_preserve_where_clauses_when_formatted() {
let item: Item = moxy::parse!("impl<T> Example<T> where T: Copy { fn value(&self) -> T { self.0 } }").unwrap();
let implementation = item.as_impl().unwrap();

assert!(implementation.generics.where_clause.is_some());
assert_eq!(
moxy::fmt!(&item).unwrap(),
"impl<T> Example<T>\nwhere\nT: Copy {\n\tfn value(&self) -> T {\n\t\tself.0\n\t}\n}"
);
}