Skip to content
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

Fix name collision between C enum and typedef #2326

Merged
merged 2 commits into from Nov 28, 2022

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Oct 27, 2022

Fixes #2008.

Example:

enum Enum { Variant };
typedef int16_t Enum;

This is valid and idiomatic C (though not valid C++). cbindgen uses this idiom as the default C translation of Rust enums, the equivalent of what would be enum Enum : int16_t { Variant }; in C++.

bindgen header.h before:

pub const Enum_Variant: Enum = 0;
pub type Enum = ::std::os::raw::c_uint;
pub type Enum = i16;
error[E0428]: the name `Enum` is defined multiple times
 --> generated.rs:3:1
  |
2 | pub type Enum = ::std::os::raw::c_uint;
  | --------------------------------------- previous definition of the type `Enum` here
3 | pub type Enum = i16;
  | ^^^^^^^^^^^^^^^^^^^^ `Enum` redefined here
  |
  = note: `Enum` must be defined only once in the type namespace of this module

After:

pub const Enum_Variant: Enum = 0;
pub type Enum = i16;

@bors-servo
Copy link

☔ The latest upstream changes (presumably f160d11) made this pull request unmergeable. Please resolve the merge conflicts.

    error[E0428]: the name `Enum` is defined multiple times
      --> bindgen-tests/tests/expectations/tests/enum-typedef.rs:10:1
       |
    9  | pub type Enum = ::std::os::raw::c_uint;
       | --------------------------------------- previous definition of the type `Enum` here
    10 | pub type Enum = i16;
       | ^^^^^^^^^^^^^^^^^^^^ `Enum` redefined here
       |
       = note: `Enum` must be defined only once in the type namespace of this module

    error[E0428]: the name `TypedefFirst` is defined multiple times
      --> bindgen-tests/tests/expectations/tests/enum-typedef.rs:13:1
       |
    11 | pub type TypedefFirst = i16;
       | ---------------------------- previous definition of the type `TypedefFirst` here
    12 | pub const TypedefFirst_Variant2: TypedefFirst = 0;
    13 | pub type TypedefFirst = ::std::os::raw::c_uint;
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `TypedefFirst` redefined here
       |
       = note: `TypedefFirst` must be defined only once in the type namespace of this module
@dtolnay
Copy link
Member Author

dtolnay commented Nov 25, 2022

@pvdrz pvdrz merged commit 0a78cde into rust-lang:master Nov 28, 2022
@dtolnay dtolnay deleted the enum branch November 28, 2022 15:45
facebook-github-bot pushed a commit to facebookexperimental/rust-shed that referenced this pull request Mar 22, 2023
Summary:
Both waiting-in PRs have been merged and released:

- rust-lang/rust-bindgen#2319
- rust-lang/rust-bindgen#2326

Adding `artifact = "bin"` removes the need for the `lib.rs` patch:

- fbsource/bindgen@3579276

Reviewed By: jsgf

Differential Revision: D44301594

fbshipit-source-id: d5ac1b54b099655e8e87974e42439bcd32aa5aed
facebook-github-bot pushed a commit to facebook/fb303 that referenced this pull request Mar 22, 2023
Summary:
Both waiting-in PRs have been merged and released:

- rust-lang/rust-bindgen#2319
- rust-lang/rust-bindgen#2326

Adding `artifact = "bin"` removes the need for the `lib.rs` patch:

- fbsource/bindgen@3579276

Reviewed By: jsgf

Differential Revision: D44301594

fbshipit-source-id: d5ac1b54b099655e8e87974e42439bcd32aa5aed
facebook-github-bot pushed a commit to facebook/sapling that referenced this pull request Mar 22, 2023
Summary:
Both waiting-in PRs have been merged and released:

- rust-lang/rust-bindgen#2319
- rust-lang/rust-bindgen#2326

Adding `artifact = "bin"` removes the need for the `lib.rs` patch:

- fbsource/bindgen@3579276

Reviewed By: jsgf

Differential Revision: D44301594

fbshipit-source-id: d5ac1b54b099655e8e87974e42439bcd32aa5aed
LoganBarnett pushed a commit to LoganBarnett/rust-bindgen that referenced this pull request Dec 2, 2023
Fixes rust-lang#2008.

Example:

```c
enum Enum { Variant };
typedef int16_t Enum;
```

This is valid and idiomatic C (though not valid C++). `cbindgen` uses this idiom as the default C translation of Rust enums, the equivalent of what would be `enum Enum : int16_t { Variant };` in C++.

`bindgen header.h` before:

```rust
pub const Enum_Variant: Enum = 0;
pub type Enum = ::std::os::raw::c_uint;
pub type Enum = i16;
```

```console
error[E0428]: the name `Enum` is defined multiple times
 --> generated.rs:3:1
  |
2 | pub type Enum = ::std::os::raw::c_uint;
  | --------------------------------------- previous definition of the type `Enum` here
3 | pub type Enum = i16;
  | ^^^^^^^^^^^^^^^^^^^^ `Enum` redefined here
  |
  = note: `Enum` must be defined only once in the type namespace of this module
```

After:

```rust

pub const Enum_Variant: Enum = 0;
pub type Enum = i16;
```
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.

the name <> is defined multiple times caused by enum and integer definition
3 participants