Skip to content

Commit b5e8422

Browse files
dcharkesCommit Queue
authored and
Commit Queue
committed
[beta] Revert "[vm/ffi] Unwrap typed data in FFI calls"
This reverts: https://dart-review.googlesource.com/c/sdk/+/338620 We'd like to support this use case with a different API. See the discussion in #54739. Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/349340 Cherry-pick-request: #54771 Change-Id: I23ced1bace50a110f590f85a8a62e7f2ca80e304 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/349320 Commit-Queue: Daco Harkes <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent c383330 commit b5e8422

File tree

23 files changed

+2
-4235
lines changed

23 files changed

+2
-4235
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5005,29 +5005,6 @@ const MessageCode messageFfiAddressOfMustBeNative = const MessageCode(
50055005
problemMessage:
50065006
r"""Argument to 'Native.addressOf' must be annotated with @Native.""");
50075007

5008-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5009-
const Code<Null> codeFfiCallMustNotReturnTypedData =
5010-
messageFfiCallMustNotReturnTypedData;
5011-
5012-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5013-
const MessageCode messageFfiCallMustNotReturnTypedData = const MessageCode(
5014-
"FfiCallMustNotReturnTypedData",
5015-
analyzerCodes: <String>["CALL_MUST_NOT_RETURN_TYPED_DATA"],
5016-
problemMessage: r"""FFI calls can't return typed data.""",
5017-
correctionMessage: r"""Try changing using `Pointer`s.""");
5018-
5019-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5020-
const Code<Null> codeFfiCallbackMustNotUseTypedData =
5021-
messageFfiCallbackMustNotUseTypedData;
5022-
5023-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5024-
const MessageCode messageFfiCallbackMustNotUseTypedData = const MessageCode(
5025-
"FfiCallbackMustNotUseTypedData",
5026-
analyzerCodes: <String>["CALLBACK_MUST_NOT_USE_TYPED_DATA"],
5027-
problemMessage:
5028-
r"""FFI callbacks can't take typed data arguments or return value.""",
5029-
correctionMessage: r"""Try changing using `Pointer`s.""");
5030-
50315008
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
50325009
const Template<Message Function(String string, String name)>
50335010
templateFfiCompoundImplementsFinalizable =
@@ -5430,17 +5407,6 @@ Message _withArgumentsFfiNativeUnexpectedNumberOfParametersWithReceiver(
54305407
arguments: {'count': count, 'count2': count2});
54315408
}
54325409

5433-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5434-
const Code<Null> codeFfiNonLeafCallMustNotTakeTypedData =
5435-
messageFfiNonLeafCallMustNotTakeTypedData;
5436-
5437-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5438-
const MessageCode messageFfiNonLeafCallMustNotTakeTypedData = const MessageCode(
5439-
"FfiNonLeafCallMustNotTakeTypedData",
5440-
analyzerCodes: <String>["NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA"],
5441-
problemMessage: r"""FFI non-leaf calls can't take typed data arguments.""",
5442-
correctionMessage: r"""Try changing the call to a leaf call.""");
5443-
54445410
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
54455411
const Template<
54465412
Message Function(String name)> templateFfiNotStatic = const Template<

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,12 +1776,6 @@ FfiCode.LEAF_CALL_MUST_NOT_RETURN_HANDLE:
17761776
status: noFix
17771777
FfiCode.LEAF_CALL_MUST_NOT_TAKE_HANDLE:
17781778
status: noFix
1779-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA:
1780-
status: noFix
1781-
FfiCode.CALL_MUST_NOT_RETURN_TYPED_DATA:
1782-
status: noFix
1783-
FfiCode.NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA:
1784-
status: noFix
17851779
FfiCode.MISMATCHED_ANNOTATION_ON_STRUCT_FIELD:
17861780
status: noFix
17871781
FfiCode.MISSING_ANNOTATION_ON_STRUCT_FIELD:

pkg/analyzer/lib/src/dart/error/ffi_code.g.dart

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,6 @@ class FfiCode extends AnalyzerErrorCode {
9191
"Try passing a static function or field annotated with '@Native'",
9292
);
9393

94-
/// No parameters.
95-
static const FfiCode CALLBACK_MUST_NOT_USE_TYPED_DATA = FfiCode(
96-
'CALLBACK_MUST_NOT_USE_TYPED_DATA',
97-
"FFI callbacks can't take typed data arguments or return value.",
98-
correctionMessage: "Try changing using `Pointer`s.",
99-
hasPublishedDocs: true,
100-
);
101-
102-
/// No parameters.
103-
static const FfiCode CALL_MUST_NOT_RETURN_TYPED_DATA = FfiCode(
104-
'CALL_MUST_NOT_RETURN_TYPED_DATA',
105-
"FFI calls can't return typed data.",
106-
correctionMessage: "Try changing using `Pointer`s.",
107-
hasPublishedDocs: true,
108-
);
109-
11094
/// Parameters:
11195
/// 0: the name of the struct or union class
11296
static const FfiCode COMPOUND_IMPLEMENTS_FINALIZABLE = FfiCode(
@@ -410,14 +394,6 @@ class FfiCode extends AnalyzerErrorCode {
410394
hasPublishedDocs: true,
411395
);
412396

413-
/// No parameters.
414-
static const FfiCode NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA = FfiCode(
415-
'NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA',
416-
"FFI non-leaf calls can't take typed data arguments.",
417-
correctionMessage: "Try changing the call to a leaf call.",
418-
hasPublishedDocs: true,
419-
);
420-
421397
/// Parameters:
422398
/// 0: the type that should be a valid dart:ffi native type.
423399
static const FfiCode NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER = FfiCode(

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,6 @@ const List<ErrorCode> errorCodeValues = [
581581
FfiCode.ANNOTATION_ON_POINTER_FIELD,
582582
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT,
583583
FfiCode.ARGUMENT_MUST_BE_NATIVE,
584-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA,
585-
FfiCode.CALL_MUST_NOT_RETURN_TYPED_DATA,
586584
FfiCode.COMPOUND_IMPLEMENTS_FINALIZABLE,
587585
FfiCode.CREATION_OF_STRUCT_OR_UNION,
588586
FfiCode.EMPTY_STRUCT,
@@ -615,7 +613,6 @@ const List<ErrorCode> errorCodeValues = [
615613
FfiCode.NATIVE_FIELD_MISSING_TYPE,
616614
FfiCode.NATIVE_FIELD_NOT_STATIC,
617615
FfiCode.NON_CONSTANT_TYPE_ARGUMENT,
618-
FfiCode.NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA,
619616
FfiCode.NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER,
620617
FfiCode.NON_POSITIVE_ARRAY_DIMENSION,
621618
FfiCode.NON_SIZED_TYPE_ARGUMENT,

pkg/analyzer/lib/src/generated/ffi_verifier.dart

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
601601
[nativeType, dartType, 'Native']);
602602
return;
603603
}
604-
605-
_validateFfiTypedDataUnwrapping(
606-
dartType,
607-
nativeType,
608-
errorNode,
609-
isLeaf: isLeaf,
610-
isCall: true,
611-
);
612604
}
613605

614606
bool _extendsNativeFieldWrapperClass1(InterfaceType? type) {
@@ -1041,13 +1033,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
10411033
if (isLeaf) {
10421034
_validateFfiLeafCallUsesNoHandles(TPrime, node);
10431035
}
1044-
_validateFfiTypedDataUnwrapping(
1045-
F,
1046-
TPrime,
1047-
errorNode,
1048-
isLeaf: isLeaf,
1049-
isCall: true,
1050-
);
10511036
}
10521037
_validateIsLeafIsConst(node);
10531038
}
@@ -1207,46 +1192,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
12071192
}
12081193
}
12091194

1210-
void _validateFfiTypedDataUnwrapping(
1211-
DartType dartType,
1212-
DartType nativeType,
1213-
AstNode errorNode, {
1214-
required bool isLeaf,
1215-
required bool isCall,
1216-
}) {
1217-
if (dartType is FunctionType && nativeType is FunctionType) {
1218-
if (dartType.returnType.isTypedData && nativeType.returnType.isPointer) {
1219-
if (!isCall) {
1220-
_errorReporter.reportErrorForNode(
1221-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA, errorNode);
1222-
} else {
1223-
_errorReporter.reportErrorForNode(
1224-
FfiCode.CALL_MUST_NOT_RETURN_TYPED_DATA, errorNode);
1225-
}
1226-
}
1227-
int i = 0;
1228-
final nativeParamTypes = nativeType.normalParameterTypes.flattenVarArgs();
1229-
for (final dartParam in dartType.normalParameterTypes) {
1230-
if (i >= nativeParamTypes.length) {
1231-
// Cascading error as not the same amount of arguments.
1232-
// Already results in an error earlier.
1233-
return;
1234-
}
1235-
final nativeParam = nativeParamTypes[i];
1236-
i++;
1237-
if (dartParam.isTypedData && nativeParam.isPointer) {
1238-
if (!isCall) {
1239-
_errorReporter.reportErrorForNode(
1240-
FfiCode.CALLBACK_MUST_NOT_USE_TYPED_DATA, errorNode);
1241-
} else if (!isLeaf) {
1242-
_errorReporter.reportErrorForNode(
1243-
FfiCode.NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA, errorNode);
1244-
}
1245-
}
1246-
}
1247-
}
1248-
}
1249-
12501195
/// Validate that the fields declared by the given [node] meet the
12511196
/// requirements for fields within a struct or union class.
12521197
void _validateFieldsInCompound(FieldDeclaration node) {
@@ -1379,7 +1324,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
13791324
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT, e, ['exceptionalReturn']);
13801325
}
13811326
}
1382-
_validateFfiTypedDataUnwrapping(FT, T, f, isLeaf: false, isCall: false);
13831327
}
13841328

13851329
/// Ensure `isLeaf` is const as we need the value at compile time to know
@@ -1431,9 +1375,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
14311375
if (isLeaf) {
14321376
_validateFfiLeafCallUsesNoHandles(S, typeArguments[0]);
14331377
}
1434-
final AstNode errorNode = typeArguments[1];
1435-
_validateFfiTypedDataUnwrapping(F, S, errorNode,
1436-
isLeaf: isLeaf, isCall: true);
14371378
}
14381379

14391380
/// Validate the invocation of `Native.addressOf`.
@@ -1576,13 +1517,6 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
15761517
_errorReporter.reportErrorForNode(
15771518
FfiCode.MUST_BE_A_SUBTYPE, e, [eType, natRetType, name]);
15781519
}
1579-
_validateFfiTypedDataUnwrapping(
1580-
funcType,
1581-
typeArg,
1582-
e,
1583-
isLeaf: false,
1584-
isCall: false,
1585-
);
15861520
if (!_isConst(e)) {
15871521
_errorReporter.reportErrorForNode(
15881522
FfiCode.ARGUMENT_MUST_BE_A_CONSTANT, e, ['exceptionalReturn']);

pkg/analyzer/messages.yaml

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -20001,139 +20001,6 @@ FfiCode:
2000120001
}
2000220002
}
2000320003
```
20004-
NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA:
20005-
problemMessage: "FFI non-leaf calls can't take typed data arguments."
20006-
correctionMessage: Try changing the call to a leaf call.
20007-
comment: No parameters.
20008-
hasPublishedDocs: true
20009-
documentation: |-
20010-
#### Description
20011-
20012-
The analyzer produces this diagnostic when the value of the `isLeaf`
20013-
argument of `Pointer.asFunction`, `DynamicLibrary.lookupFunction`, or
20014-
`@Native` is `false` and the Dart function signature contains a typed
20015-
data parameter.
20016-
20017-
Typed data unwrapping is only supported on arguments for leaf FFI calls.
20018-
20019-
For more information about FFI, see [C interop using dart:ffi][ffi].
20020-
20021-
#### Example
20022-
20023-
The following code produces this diagnostic because the dart function
20024-
signature contains a typed data, but the `isLeaf` argument is `false`:
20025-
20026-
```dart
20027-
import 'dart:ffi';
20028-
import 'dart:typed_data';
20029-
20030-
void f(Pointer<NativeFunction<Void Function(Pointer<Uint8>)>> p) {
20031-
p.asFunction<[!void Function(Uint8List)!]>();
20032-
}
20033-
```
20034-
20035-
#### Common fixes
20036-
20037-
If the function has at least one typed data parameter, then add
20038-
the `isLeaf` argument:
20039-
20040-
```dart
20041-
import 'dart:ffi';
20042-
import 'dart:typed_data';
20043-
20044-
void f(Pointer<NativeFunction<Void Function(Pointer<Uint8>)>> p) {
20045-
p.asFunction<void Function(Uint8List)>(isLeaf: true);
20046-
}
20047-
```
20048-
20049-
If the function also uses `Handle`s, then it must be non-leaf. In That
20050-
case use `Pointer`s instead of typed data.
20051-
CALL_MUST_NOT_RETURN_TYPED_DATA:
20052-
problemMessage: "FFI calls can't return typed data."
20053-
correctionMessage: Try changing using `Pointer`s.
20054-
comment: No parameters.
20055-
hasPublishedDocs: true
20056-
documentation: |-
20057-
#### Description
20058-
20059-
The analyzer produces this diagnostic when the return type of
20060-
`Pointer.asFunction`, `DynamicLibrary.lookupFunction`, or
20061-
`@Native` is a typed data.
20062-
20063-
Typed data unwrapping is only supported on arguments for leaf FFI calls.
20064-
20065-
For more information about FFI, see [C interop using dart:ffi][ffi].
20066-
20067-
#### Example
20068-
20069-
The following code produces this diagnostic because the dart function
20070-
signature contains a typed data, but the `isLeaf` argument is `false`:
20071-
20072-
```dart
20073-
import 'dart:ffi';
20074-
import 'dart:typed_data';
20075-
20076-
void f(Pointer<NativeFunction<Pointer<Uint8> Function()>> p) {
20077-
p.asFunction<[!Uint8List Function()!]>();
20078-
}
20079-
```
20080-
20081-
#### Common fixes
20082-
20083-
Use the `Pointer` type instead:
20084-
20085-
```dart
20086-
import 'dart:ffi';
20087-
20088-
void f(Pointer<NativeFunction<Pointer<Uint8> Function()>> p) {
20089-
p.asFunction<Pointer<Uint8> Function()>();
20090-
}
20091-
```
20092-
CALLBACK_MUST_NOT_USE_TYPED_DATA:
20093-
problemMessage: "FFI callbacks can't take typed data arguments or return value."
20094-
correctionMessage: Try changing using `Pointer`s.
20095-
comment: No parameters.
20096-
hasPublishedDocs: true
20097-
documentation: |-
20098-
#### Description
20099-
20100-
The analyzer produces this diagnostic when an invocation of
20101-
`Pointer.fromFunction`, one of`NativeCallable`'s constructors has a
20102-
typed data argument or return value."
20103-
20104-
Typed data unwrapping is only supported on arguments for leaf FFI calls.
20105-
20106-
For more information about FFI, see [C interop using dart:ffi][ffi].
20107-
20108-
#### Example
20109-
20110-
The following code produces this diagnostic because the parameter type
20111-
of `g` is a typed data.
20112-
20113-
```dart
20114-
import 'dart:ffi';
20115-
import 'dart:typed_data';
20116-
20117-
void f(Uint8List i) {}
20118-
20119-
void g() {
20120-
Pointer.fromFunction<Void Function(Pointer<Uint8>)>([!f!]);
20121-
}
20122-
```
20123-
20124-
#### Common fixes
20125-
20126-
Use the `Pointer` type instead:
20127-
20128-
```dart
20129-
import 'dart:ffi';
20130-
20131-
void f(Pointer<Uint8> i) {}
20132-
20133-
void g() {
20134-
Pointer.fromFunction<Void Function(Pointer<Uint8>)>(f);
20135-
}
20136-
```
2013720004
NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER:
2013820005
problemMessage: "Can't invoke 'asFunction' because the function signature '{0}' for the pointer isn't a valid C function signature."
2013920006
correctionMessage: "Try changing the function argument in 'NativeFunction' to only use NativeTypes."

0 commit comments

Comments
 (0)