Added fix for member name and type name collision in structs/unions. (#256)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9cdf028..1c36c42 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 4.1.1
+- Added fix for errors due to name collision between member name
+and type name used internally in structs/unions.
+
# 4.1.0
- Add config key `functions -> leaf` for specifying `isLeaf:true` for functions.
diff --git a/lib/src/code_generator/compound.dart b/lib/src/code_generator/compound.dart
index 887f523..8bdee9b 100644
--- a/lib/src/code_generator/compound.dart
+++ b/lib/src/code_generator/compound.dart
@@ -115,6 +115,12 @@
/// to have the same name as the class.
final localUniqueNamer = UniqueNamer({enclosingClassName});
+ /// Marking type names because dart doesn't allow class member to have the
+ /// same name as a type name used internally.
+ for (final m in members) {
+ localUniqueNamer.markUsed(m.type.getDartType(w));
+ }
+
/// Write @Packed(X) annotation if struct is packed.
if (isStruct && pack != null) {
s.write('@${w.ffiLibraryPrefix}.Packed($pack)\n');
diff --git a/pubspec.yaml b/pubspec.yaml
index f4e7135..4f902ab 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.
name: ffigen
-version: 4.1.0
+version: 4.1.1
homepage: https://github.com/dart-lang/ffigen
description: Generator for FFI bindings, using LibClang to parse C header files.
diff --git a/test/collision_tests/decl_type_name_collision.h b/test/collision_tests/decl_type_name_collision.h
new file mode 100644
index 0000000..9d25f38
--- /dev/null
+++ b/test/collision_tests/decl_type_name_collision.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+struct A {
+ int a;
+};
+
+struct B {
+ int B;
+ int A;
+};
+
+struct C {
+ struct A A;
+ struct B B;
+};
+
+struct D {
+ struct B A;
+ struct A B;
+};
diff --git a/test/collision_tests/decl_type_name_collision_test.dart b/test/collision_tests/decl_type_name_collision_test.dart
new file mode 100644
index 0000000..5bb8f82
--- /dev/null
+++ b/test/collision_tests/decl_type_name_collision_test.dart
@@ -0,0 +1,47 @@
+// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:ffigen/src/code_generator.dart';
+import 'package:ffigen/src/config_provider.dart';
+import 'package:ffigen/src/header_parser.dart' as parser;
+import 'package:ffigen/src/strings.dart' as strings;
+import 'package:logging/logging.dart';
+import 'package:test/test.dart';
+import 'package:yaml/yaml.dart' as yaml;
+
+import '../test_utils.dart';
+
+late Library actual;
+void main() {
+ group('decl_type_name_collision test', () {
+ setUpAll(() {
+ logWarnings(Level.SEVERE);
+ actual = parser.parse(
+ Config.fromYaml(yaml.loadYaml('''
+${strings.name}: 'NativeLibrary'
+${strings.description}: 'Decl type name collision test'
+${strings.output}: 'unused'
+${strings.headers}:
+ ${strings.entryPoints}:
+ - 'test/collision_tests/decl_type_name_collision.h'
+${strings.preamble}: |
+ // ignore_for_file: non_constant_identifier_names,
+ ''') as yaml.YamlMap),
+ );
+ });
+
+ test('Expected bindings', () {
+ matchLibraryWithExpected(actual, [
+ 'test',
+ 'debug_generated',
+ 'decl_type_name_collision_test_output.dart'
+ ], [
+ 'test',
+ 'collision_tests',
+ 'expected_bindings',
+ '_expected_decl_type_name_collision_bindings.dart'
+ ]);
+ });
+ });
+}
diff --git a/test/collision_tests/expected_bindings/_expected_decl_type_name_collision_bindings.dart b/test/collision_tests/expected_bindings/_expected_decl_type_name_collision_bindings.dart
new file mode 100644
index 0000000..5dab4a2
--- /dev/null
+++ b/test/collision_tests/expected_bindings/_expected_decl_type_name_collision_bindings.dart
@@ -0,0 +1,31 @@
+// ignore_for_file: non_constant_identifier_names,
+
+// AUTO GENERATED FILE, DO NOT EDIT.
+//
+// Generated by `package:ffigen`.
+import 'dart:ffi' as ffi;
+
+class A extends ffi.Struct {
+ @ffi.Int32()
+ external int a;
+}
+
+class B extends ffi.Struct {
+ @ffi.Int32()
+ external int B1;
+
+ @ffi.Int32()
+ external int A;
+}
+
+class C extends ffi.Struct {
+ external A A1;
+
+ external B B1;
+}
+
+class D extends ffi.Struct {
+ external B A1;
+
+ external A B1;
+}