Skip to content

Commit 7f45c84

Browse files
authored
[ffigen] Fix block helper naming bug (dart-lang#2963)
1 parent 9c9b31c commit 7f45c84

8 files changed

Lines changed: 1495 additions & 1146 deletions

File tree

‎pkgs/ffigen/CHANGELOG.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
supports integers, doubles, and string literals. Including the variable name
1616
in the globals -> symbol-address configuration will still generate symbol
1717
lookups.
18-
18+
- Fix [a bug](https://github.com/dart-lang/native/issues/2952) where block
19+
helpers were occasionally given unexpected names. Technically a breaking
20+
change because if you were affected by the bug, the block helper's name will
21+
change to something more sensible.
1922

2023
## 20.1.1
2124

‎pkgs/ffigen/lib/src/code_generator/objc_block.dart‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,30 @@ class ObjCBlock extends BindingType with HasLocalScope {
4141

4242
final usr = _getBlockUsr(returnType, renamedParams, returnsRetained);
4343

44+
final newBlockName = _getBlockName(
45+
returnType,
46+
renamedParams.map((a) => a.type),
47+
reduced: false,
48+
);
4449
final oldBlock = context.bindingsIndex.getSeenObjCBlock(usr);
4550
if (oldBlock != null) {
51+
if (oldBlock.symbol.oldName != newBlockName) {
52+
// Block with matching signature, but a different name. This is usually
53+
// due to type aliases. Replace the name with the reduced name, so that
54+
// it makes sense as a name for all blocks sharing this signature.
55+
oldBlock.symbol.oldName = _getBlockName(
56+
returnType,
57+
renamedParams.map((a) => a.type),
58+
reduced: true,
59+
);
60+
}
4661
return oldBlock;
4762
}
4863

4964
final block = ObjCBlock._(
5065
context,
5166
usr: usr,
52-
name: _getBlockName(returnType, renamedParams.map((a) => a.type)),
67+
name: newBlockName,
5368
returnType: returnType,
5469
params: renamedParams,
5570
returnsRetained: returnsRetained,
@@ -96,11 +111,26 @@ class ObjCBlock extends BindingType with HasLocalScope {
96111
// type. These names will be pretty verbose and unweildy, but they're at least
97112
// sensible and stable. Users can always add their own typedef with a simpler
98113
// name if necessary.
99-
static String _getBlockName(Type returnType, Iterable<Type> argTypes) =>
100-
'ObjCBlock_${[returnType, ...argTypes].map(_typeName).join('_')}';
101-
static String _typeName(Type type) =>
102-
type.toString().replaceAll(_illegalNameChar, '');
114+
static String _getBlockName(
115+
Type returnType,
116+
Iterable<Type> argTypes, {
117+
required bool reduced,
118+
}) {
119+
final types = [returnType, ...argTypes].map((t) => _typeName(t, reduced));
120+
return 'ObjCBlock_${types.join('_')}';
121+
}
122+
123+
static String _typeName(Type type, bool reduced) =>
124+
(reduced ? _reducedType(type) : type).toString().replaceAll(
125+
_illegalNameChar,
126+
'',
127+
);
103128
static final _illegalNameChar = RegExp(r'[^0-9a-zA-Z]');
129+
static Type _reducedType(Type type) {
130+
if (type.baseType != type) return _reducedType(type.baseType);
131+
if (type.typealiasType != type) return _reducedType(type.typealiasType);
132+
return type;
133+
}
104134

105135
static String _getBlockUsr(
106136
Type returnType,

‎pkgs/ffigen/lib/src/code_generator/scope.dart‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,23 @@ class Namer {
161161
/// Add the [Symbol] to a [Scope], and it will be assigned a name during the
162162
/// transformation phase.
163163
class Symbol extends AstNode {
164-
final String oldName;
164+
String _oldName;
165165
final SymbolKind kind;
166166
bool isImported = false;
167167
String? _name;
168168

169+
String get oldName => _oldName;
170+
171+
/// Only valid if [Scope.fillNames] has not been called yet.
172+
set oldName(String n) {
173+
assert(!isFilled);
174+
_oldName = n;
175+
}
176+
169177
/// Only valid if [Scope.fillNames] has been called already.
170178
String get name => _name!;
171179

172-
Symbol(this.oldName, this.kind);
180+
Symbol(this._oldName, this.kind);
173181

174182
bool get isFilled => _name != null;
175183

0 commit comments

Comments
 (0)