Skip to content

ir: Use the completed type of incomplete arrays redeclared with a bound - #3553

Open
mmporong wants to merge 1 commit into
rust-lang:mainfrom
mmporong:fix-incomplete-array-redeclaration
Open

mmporong wants to merge 1 commit into
rust-lang:mainfrom
mmporong:fix-incomplete-array-redeclaration

Conversation

@mmporong

Copy link
Copy Markdown

Fixes #3499.

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 (int[3]) to the later declaration; the first one keeps int[]. Each VarDecl becomes its own Var item, and <Var as CodeGenerator>::codegen emits the first one it sees (seen_var), so the binding was generated as [c_int; 0usize].

With this change, when a VarDecl has an incomplete array type, Var::parse looks 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:

// before
pub static mut array: [::std::os::raw::c_int; 0usize];
// after
pub static mut array: [::std::os::raw::c_int; 3usize];

The same applies when the completing declaration is a definition (extern int t[]; int t[6]; now gives 6usize instead of 0usize).

Tests

Added two test headers:

  • extern-incomplete-array-completed.h
    • extern int array[]; extern int array[3]; (the issue)
    • a multidimensional array whose first bound is completed later (matrix[][2] then matrix[4][2])
    • an incomplete declaration completed by a tentative definition
    • a complete declaration followed by one without a bound (already worked, kept as is)
    • an incomplete array with no completing declaration (stays 0usize)
  • extern-incomplete-array-completed-cxx.hpp: the same pattern inside a namespace, inside an extern "C" { ... } block, and completed by a definition with an initializer.

Without the change in var.rs, both tests fail: the C header on array, matrix and tentative (0usize instead of 3usize, 4usize and 6usize), the C++ header on all three variables (0usize instead of 3usize, 4usize and 5usize). With it, they pass.

Run in a Linux container with clang 19.1.7:

  • cargo test: all 645 tests in bindgen-tests pass, plus the other default members.
  • cargo test -p tests_expectations --test extern-incomplete-array-completed and --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 to main.

Not covered by this change, since the redeclarations have different semantic parents (these still give 0usize, as before):

  • redeclarations in two separate blocks of a reopened namespace, or in two separate extern "C" blocks / extern "C" declarations;
  • a static data member declared as static int a[]; and defined out of line as int S::a[3]; (S_a stays 0usize).

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.
@rustbot rustbot added the A-C++ label Nov 19, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong size for incomplete arrays that are later completed

2 participants