Repository navigation
Conversation
A variable can be declared with an incomplete array type and completed by
a later redeclaration:
extern int array[];
extern int array[3];
clang only gives the complete type to the later declaration. Each
declaration becomes its own Var item, and codegen emits the first one it
sees, so the binding was generated as a zero-sized array.
When a VarDecl has an incomplete array type, look for a redeclaration in
the same semantic parent with a constant array type and use that type.
Fixes rust-lang#3499.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3499.
A variable can be declared with an incomplete array type and completed by a later redeclaration:
clang only gives the complete type (
int[3]) to the later declaration; the first one keepsint[]. EachVarDeclbecomes its ownVaritem, and<Var as CodeGenerator>::codegenemits the first one it sees (seen_var), so the binding was generated as[c_int; 0usize].With this change, when a
VarDeclhas an incomplete array type,Var::parselooks for a redeclaration in the same semantic parent that has a constant array type and uses that type. The lookup only runs for incomplete array types and stops at the first match.Generated bindings for the issue's header:
The same applies when the completing declaration is a definition (
extern int t[]; int t[6];now gives6usizeinstead of0usize).Tests
Added two test headers:
extern-incomplete-array-completed.hextern int array[]; extern int array[3];(the issue)matrix[][2]thenmatrix[4][2])0usize)extern-incomplete-array-completed-cxx.hpp: the same pattern inside a namespace, inside anextern "C" { ... }block, and completed by a definition with an initializer.Without the change in
var.rs, both tests fail: the C header onarray,matrixandtentative(0usizeinstead of3usize,4usizeand6usize), the C++ header on all three variables (0usizeinstead of3usize,4usizeand5usize). With it, they pass.Run in a Linux container with clang 19.1.7:
cargo test: all 645 tests inbindgen-testspass, plus the other default members.cargo test -p tests_expectations --test extern-incomplete-array-completedand--test extern-incomplete-array-completed-cxx: the new expectations compile.cargo +nightly fmt --check: clean.cargo clippy -p bindgen -p bindgen-tests --all-targets: no new warnings compared tomain.Not covered by this change, since the redeclarations have different semantic parents (these still give
0usize, as before):extern "C"blocks /extern "C"declarations;static int a[];and defined out of line asint S::a[3];(S_astays0usize).