rust-lang / rust-lang/rust-bindgen

C Atomics are not converted to equivalent atomics in Rust

Open
#2,151 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Input C/C++ Header
struct globals {
  _Atomic _Bool interrupted;
};
Bindgen Invocation
$ bindgen input.h
Actual Results

In general, these get translated into u8 e.g.

/* automatically generated by rust-bindgen 0.59.2 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct globals {
    pub interrupted: u8,
}

However, while trying to make an even smaller reproduce case:

extern _Atomic _Bool interrupted;

I got a panic:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `177`,
 right: `118`: Couldn't resolve constant type, and it wasn't an nondeductible auto type!', /Users/levi.morrison/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.59.2/src/ir/var.rs:318:25
stack backtrace:
   0: rust_begin_unwind
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:517:5
   1: core::panicking::panic_fmt
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:100:14
   2: core::panicking::assert_failed_inner
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:175:23
   3: core::panicking::assert_failed
   4: <bindgen::ir::var::Var as bindgen::parse::ClangSubItemParser>::parse
   5: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::parse
   6: bindgen::parse_one
   7: bindgen::clang::visit_children
   8: __ZN5clang8cxcursor13CursorVisitor5VisitE8CXCursorb
   9: __ZN5clang8cxcursor13CursorVisitor23handleDeclForVisitationEPKNS_4DeclE
  10: __ZN5clang8cxcursor13CursorVisitor16VisitDeclContextEPNS_11DeclContextE
  11: __ZN5clang8cxcursor13CursorVisitor13VisitChildrenE8CXCursor
  12: _clang_visitChildren
  13: clang_sys::clang_visitChildren
  14: bindgen::Builder::generate
  15: std::panicking::try
  16: bindgen::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Expected Results

I would expect something more like this:

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct globals {
    pub interrupted: std::sync::atomic::AtomicBool,
}

Since std::sync::atomic::AtomicBool has the same in-memory representation as a bool does, I think that might be exactly what I would expect as output.

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 supplied input.h and bindgen invocation, then start at ir/var.rs:318 where the standalone atomic variable panics. Trace how _Atomic _Bool is parsed for both struct fields and extern variables. Done means equivalent Rust atomic output is generated and the reproducer no longer panics.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.