Skip to content

ctest: add foreign static test #4601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2025
Merged

Conversation

mbyx
Copy link
Contributor

@mbyx mbyx commented Jul 30, 2025

Description

Adds support for testing extern statics.

Also adds support for parsing any dimensional array in make_cdecl, and makes it easier to extract ffi safe types from Option.

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 Jul 30, 2025
@mbyx mbyx force-pushed the ctest-static-test branch 2 times, most recently from 6df4113 to 4455b42 Compare August 2, 2025 13:27
@mbyx mbyx force-pushed the ctest-static-test branch from 4455b42 to aa88d90 Compare August 6, 2025 12:31
@mbyx mbyx force-pushed the ctest-static-test branch from aa88d90 to c0fa242 Compare August 6, 2025 12:35
@mbyx mbyx requested a review from tgross35 August 7, 2025 07:01
Comment on lines 125 to 147
{%- if static_.rust_ty.contains("extern") +%}
typedef {{ static_.return_type }};
ctest_static_ty__{{ static_.id }} ctest_static__{{ static_.id }}(void) {
return {{ static_.c_val }};
}
{%- else +%}
typedef {{ static_.volatile_keyword }}{{ static_.c_mutable }}{{ static_.return_type }};
ctest_static_ty__{{ static_.id }} ctest_static__{{ static_.id }}(void) {
return &{{ static_.c_val }};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since these return pointers, can they cast them on the C side and return void *? Rather than needing the more complex definition

Copy link
Contributor

Choose a reason for hiding this comment

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

@mbyx this one still needs to be changed. It's also checking for an extern substring still.

The change should look something like #4594 (comment) for the function pointer part.

Comment on lines 112 to 114
/* A simple helper to find any nested bare fn types, such as `Option<unsafe extern "C" fn()>` */

struct BareFnFinder {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be a doc comment on the struct

Comment on lines 530 to 531
pub mutable: BoxStr,
pub c_mutable: BoxStr,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: these can be &'static str

Comment on lines 125 to 147
{%- if static_.rust_ty.contains("extern") +%}
typedef {{ static_.return_type }};
ctest_static_ty__{{ static_.id }} ctest_static__{{ static_.id }}(void) {
return {{ static_.c_val }};
}
{%- else +%}
typedef {{ static_.volatile_keyword }}{{ static_.c_mutable }}{{ static_.return_type }};
ctest_static_ty__{{ static_.id }} ctest_static__{{ static_.id }}(void) {
return &{{ static_.c_val }};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@mbyx this one still needs to be changed. It's also checking for an extern substring still.

The change should look something like #4594 (comment) for the function pointer part.

Comment on lines 326 to 330
let actual = unsafe {
std::mem::transmute::<_, *const ()>(*(&raw const {{ static_.id }})).addr()
};
let expected = unsafe {
std::mem::transmute::<_, *const ()>(ctest_static__{{ static_.id }}()).addr()
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines 326 to 330
let actual = unsafe {
std::mem::transmute::<_, *const ()>(*(&raw const {{ static_.id }})).addr()
};
let expected = unsafe {
std::mem::transmute::<_, *const ()>(ctest_static__{{ static_.id }}()).addr()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add comments, this is tricky. Also, make sure this branch is covered in one of the tests.

@mbyx mbyx force-pushed the ctest-static-test branch from 494e5e5 to 148bb13 Compare August 11, 2025 08:15
@mbyx mbyx requested a review from tgross35 August 11, 2025 08:19
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.

LGTM! What happened part with special handling for functions?

@mbyx mbyx force-pushed the ctest-static-test branch from 148bb13 to dcfb81a Compare August 11, 2025 12:21
@mbyx
Copy link
Contributor Author

mbyx commented Aug 11, 2025

Now it always returns a pointer to something, whether its a data item or a function pointer/array, etc. So the Rust side just checks for equality of addresses. I'm not sure if that's perfectly correct though.

@tgross35
Copy link
Contributor

Ah, I didn't catch that - it will work but it technically isn't correct because of the function->data pointer cast. I don't think we need to deal with it now, could you make that a FIXME?

We should enable warnings in the C builds at some point, at least during testing, -Wpedantic catches that.

@mbyx mbyx force-pushed the ctest-static-test branch 2 times, most recently from 19af932 to 2d8d548 Compare August 11, 2025 14:41
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.

LGTM! Unfortunately CI is still blocked

@tgross35 tgross35 enabled auto-merge August 11, 2025 16:12
@tgross35 tgross35 added this pull request to the merge queue Aug 11, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Aug 11, 2025
@mbyx mbyx force-pushed the ctest-static-test branch from c0b5f23 to 1685c18 Compare August 11, 2025 18:38
@mbyx mbyx requested a review from tgross35 August 11, 2025 18:39
@tgross35 tgross35 enabled auto-merge August 11, 2025 20:54
@tgross35 tgross35 added this pull request to the merge queue Aug 11, 2025
auto-merge was automatically disabled August 11, 2025 21:36

Pull Request is not mergeable

@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 11, 2025
@tgross35 tgross35 added this pull request to the merge queue Aug 11, 2025
Merged via the queue into rust-lang:main with commit 3060925 Aug 11, 2025
77 of 100 checks passed
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants