Skip to content

codegen: Don't add a const to the pointee of const pointers in static wrappers - #3543

Open
thelastbackspace wants to merge 1 commit into
rust-lang:mainfrom
thelastbackspace:fix/3251-wrap-static-fns-const-ptr
Open

thelastbackspace wants to merge 1 commit into
rust-lang:mainfrom
thelastbackspace:fix/3251-wrap-static-fns-const-ptr

Conversation

@thelastbackspace

Copy link
Copy Markdown

With --wrap-static-fns, a parameter like uint32_t *const p was emitted in the C wrapper as const uint32_t *const p, so the wrapper took a pointer-to-const and passing it to the original function triggered -Wdiscarded-qualifiers. If the pointee was already const (const int *const p) it got worse and produced const const int *const p.

The parameter type is a const ResolvedTypeRef wrapping a pointer that is itself const. In CSerialize for Type the ResolvedTypeRef arm wrote a const prefix and then serialized the pointer, which wrote its own *const. The fix only writes the prefix when the referenced type isn't already const, so the qualifier is emitted once and in the right place. Non-pointer cases like const uint32_t x or const enum foo f still get the prefix, since their wrapped type isn't const.

Testing:

  • Added takes_const_ptr(int *const arg, const int *const carg) to tests/headers/wrap-static-fns.h and updated the generated .c expectations (both the default and aarch64-linux ones) plus the .rs expectation. Without the change test_wrap_static_fns fails on that line.
  • cargo test -p bindgen-tests (643 passed), cd bindgen-integration && cargo test, cargo +nightly fmt --check, cargo clippy -p bindgen --all-targets.

Fixes #3251

… wrappers

A `T *const` parameter is represented as a const ResolvedTypeRef
wrapping a const pointer. The wrapper wrote its own "const " prefix and
then the pointer wrote "*const", so the C wrapper ended up taking
`const T *const` (and `const const T *const` when the pointee was also
const).

Fixes rust-lang#3251

This branch has not been deployed

No deployments
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.

wrap_static_fns adds extra incorrect 'const' to pointer parameters

1 participant