rust-lang / rust-lang/rust-bindgen

Problems with static member initialization.

Open
#1,960 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Note: I'm currently writing a fix for this bug, so don't waste time fixing it. I just wanted to create this issue so that I can link to it in the source code.

This code:

struct MyStruct {
    static int weirdvalue;
};
int MyStruct::weirdvalue = 123;

creates these bindings:

extern "C" {
    #[link_name = "\u{1}_ZN8MyStruct10weirdvalueE"]
    pub static mut MyStruct_weirdvalue: ::std::os::raw::c_int;
}
pub const weirdvalue: ::std::os::raw::c_int = 123;

The last line is wrong, there is no global variable just called "weirdvalue", there is only the variable "MyStruct::weirdvalue".

This creates problems with templates, because

template<typename typ>
struct MyStruct {
    static typ weirdvalue;
};
template<typename typ>
typ MyStruct<typ>::weirdvalue = 123;

generates

extern "C" {
    pub static mut weirdvalue: typ;
}

which fails compilation with cannot find type `typ` in this scope

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the two C++ examples and inspect the generated Rust bindings shown in the report. Done means static members retain their class-qualified identity and the templated case no longer refers to the out-of-scope typ.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.