Skip to content

Commit b7b501f

Browse files
dlrobertsonemilio
authored andcommitted
Do not use the canonical declaration for enum type building
Allow forward declarations to be used by avoiding the use of the canonical declaration when building the underlying representation.
1 parent 5ae4b31 commit b7b501f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

bindgen-tests/tests/expectations/tests/enum-forward-ref.rs

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// bindgen-flags: --default-enum-style=consts --constified-enum-module=Neg
2+
3+
// A forward reference to an enum should work..
4+
enum Foo;
5+
enum Foo {
6+
A
7+
};
8+
9+
// A reference to an enum after the definition should work.
10+
enum Bar { B };
11+
enum Bar;
12+
13+
// A forward reference to an enum in a function definition should work.
14+
enum Baz f(void);
15+
enum Baz { C };
16+
17+
// A forward reference to an enum with no later definition should result in
18+
// a guess of the underlying type (i32).
19+
enum Qux;

bindgen/ir/enum_ty.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ impl Enum {
6565
return Err(ParseError::Continue);
6666
}
6767

68-
let declaration = ty.declaration().canonical();
68+
// Use the enum decl instead of the canonical destination. The
69+
// cursor location of the first declaration may be the forward
70+
// declaration.
71+
let declaration = ty.declaration();
6972
let repr = declaration
7073
.enum_type()
7174
.and_then(|et| Item::from_ty(&et, declaration, None, ctx).ok());

0 commit comments

Comments
 (0)