Skip to content

feat: Use macrotest to test the expanded macro code - #501

Merged
martin-g merged 8 commits into
mainfrom
feat/macrotest
Mar 5, 2026
Merged

martin-g merged 8 commits into
mainfrom
feat/macrotest

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

This makes it a lot easier (for both reviewers and programmers) to see the generated code.

I've also moved some tests of error cases to the ui tests.

Generated code can be overwritten using MACROTEST=overwrite (just like TRYBUILD=overwrite), and the tests only run on nightly, as code expansion can change between compiler versions.

We might need to add cargo expand to the CI.

@Kriskras99
Kriskras99 force-pushed the feat/macrotest branch 4 times, most recently from ecbc3bf to 14debf5 Compare March 5, 2026 10:00
@martin-g

martin-g commented Mar 5, 2026

Copy link
Copy Markdown
Member

and the tests only run on nightly, as code expansion can change between compiler versions.

But this means that we may need to regenerate every few days if there are (experimental) changes in nightly.
If we check on stable then we will need to re-generate only when the next stable has a change.

@martin-g martin-g added this to the 0.22.0 milestone Mar 5, 2026
@Kriskras99

Copy link
Copy Markdown
Contributor Author

Unfortunately we can't gate a test on stable, we can gate on a specific version (#[rustversion::stable(1.88)] but if we bump the MSRV and forget to change this version the test will never run.

@martin-g

martin-g commented Mar 5, 2026

Copy link
Copy Markdown
Member

Are you sure ?

https://crates.io/crates/rustversion starts with:

Selectors
#[::stable] —
True on any stable compiler.

#[rustversion::stable(1.34)] —
True on exactly the specified stable compiler.

...

I experimented a bit with:

fn main() {
  print_version();
}

#[rustversion::stable(1.85)]
fn print_version() {
  println!("1.85");
}

#[rustversion::stable]
fn print_version() {
  println!("Stable");
}

#[rustversion::nightly]
fn print_version() {
  println!("Nightly");
}

and clear && cargo run prints Stable, clear && cargo +nightly run prints Nightly.
clear && cargo +1.85 run fails to resolve the function because both ::stable and ::stable(1.85) are detected:

Compiling rustver v0.1.0 (/private/tmp/rustver)
error[E0428]: the name `print_version` is defined multiple times
  --> src/main.rs:12:1
   |
7  | fn print_version() {
   | ------------------ previous definition of the value `print_version` here
...
12 | fn print_version() {
   | ^^^^^^^^^^^^^^^^^^ `print_version` redefined here
   |
   = note: `print_version` must be defined only once in the value namespace of this module

For more information about this error, try `rustc --explain E0428`.
error: could not compile `rustver` (bin "rustver") due to 1 previous error

Since we don't need to both ::stable and ::stable(x.y.z) we should be fine, no ?

Comment thread avro_derive/tests/expand.rs Resolved
Comment thread .github/workflows/test-lang-rust-ci.yml Resolved
@Kriskras99

Copy link
Copy Markdown
Contributor Author

If we change it to #[rustversion::stable] then in the CI it would run both on stable and 1.88 because they are both stable versions. But because they are not the same version generated code can be different (we do the same for the ui tests)

@Kriskras99

Copy link
Copy Markdown
Contributor Author

It all works now. cargo-rdme is only installed for stable-x86_64 while cargo-expand is installed for all nightly targets. On arm targets cargo-expand needs to be compiled, but it is a small wrapper around -Zunpretty=expanded so it doesn't take that long.

Comment thread avro_derive/tests/expand.rs Resolved
Comment thread avro_derive/src/enums/plain.rs Resolved
Comment thread avro_derive/src/lib.rs Resolved
Comment thread avro_derive/src/lib.rs Resolved
Comment thread avro_derive/tests/expand.rs Resolved
Comment thread .github/workflows/test-lang-rust-ci.yml Resolved
@martin-g
martin-g merged commit 285f5c9 into main Mar 5, 2026
15 checks passed
@martin-g
martin-g deleted the feat/macrotest branch March 5, 2026 21:24
@Kriskras99 Kriskras99 mentioned this pull request Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants