Skip to content

Conversation

@mbyx
Copy link
Contributor

@mbyx mbyx commented Jun 10, 2025

Description

This PR adds a function to expand a crate (consisting of either a single file or a folder) using rustc, a stub entrypoint for test generation, and a simple test that checks whether it can expand both types of crates.

Sources

Checklist

  • Relevant tests in libc-test/semver have been updated
  • No placeholder or unstable values like *LAST or *MAX are
    included (see #3131)
  • Tested locally (cd libc-test && cargo test --target mytarget);
    especially relevant for platforms that may not be checked in CI

@rustbot rustbot added ctest Issues relating to the ctest crate S-waiting-on-review labels Jun 10, 2025
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, some small requests. Please also add a test file next to simple.rs that puts some code in a macro.


use crate::expand;

pub struct TestGenerator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this #[non_exhaustive] struct TestGenerator {} so building this has to go through the constructor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the module nesting and just move src/generation/generator.rs to src/generator.rs? I don't think we're going to have more than one file here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enforce some rules from the get go

#![warn(missing_docs)]
#![warn(unreachable_pub)]

Comment on lines 9 to 10
generator.generate("./tests/sources/hierarchy/lib.rs", "hierarchy_out.rs")?;
generator.generate("./tests/sources/simple.rs", "simple_out.rs")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? -> .unwrap() so we get the information about which line failed. The test can drop the -> Result<...> return type.

Could you also split these test sources into two different #[test] functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//! Ensure that our crate is able to handle definitions spread across many files

&self,
crate_path: P,
_output_file_path: P,
) -> Result<(), Box<dyn Error>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add

type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;

And then import those? Rather than retyping the definitions everywhere.

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few remaining nits then please squash and this should be good to merge

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything tiny like this can exist in the parent module, no need for a separate file. It can move if we wind up with more error types but we might not.

Comment on lines 1 to 13
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}

pub fn write_buffer() {
let buf = String::new();
t!(writeln!(buf, "#define A 1"));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the macro generate structs or functions that will be visible in the C interface? Since that is what we'll want to test.

@@ -0,0 +1,11 @@
pub type Byte = u8;

pub struct Person {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[repr(C)] since we'll want to have it exported

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it some, could you rename the sources directory to input? Probably makes things a bit more obvious.

@tgross35
Copy link
Contributor

CI also needs to be fixed, probably an update here

libc/ci/run.sh

Lines 97 to 99 in 2d96246

case "$target" in
*wasm*) cmd="$cmd --exclude ctest --exclude ctest-test"
esac

@mbyx mbyx requested a review from tgross35 June 11, 2025 12:44
@mbyx
Copy link
Contributor Author

mbyx commented Jun 11, 2025

The expand function seemed to not fail even if the source file had incorrect syntax or could only be partially expanded due to an error, so I fixed that.

@tgross35
Copy link
Contributor

The expand function seemed to not fail even if the source file had incorrect syntax or could only be partially expanded due to an error, so I fixed that.

Could you add a test with invalid syntax in tests/input and assert that it fails in basic.rs?

Please squash the commits once this is all set, everything else lgtm

@mbyx mbyx force-pushed the ctest_macro_expansion branch from d029284 to 7b1b6c3 Compare June 12, 2025 11:10
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tgross35 tgross35 added this pull request to the merge queue Jun 12, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jun 12, 2025
@tgross35 tgross35 added this pull request to the merge queue Jun 12, 2025
Merged via the queue into rust-lang:main with commit aa5bbcc Jun 12, 2025
49 of 51 checks passed
@tgross35 tgross35 changed the title Add macro expansion and ctest entrypoint. ctest: Add macro expansion and ctest entrypoint. Aug 11, 2025
@tgross35 tgross35 added the stable-unneeded This PR is applied to main but already exists on libc-0.2 label Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ctest Issues relating to the ctest crate S-waiting-on-review stable-unneeded This PR is applied to main but already exists on libc-0.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants