[analyzer/ffi] Fix FfiNative Pointer params

TEST=pkg/analyzer/test/src/diagnostics/ffi_native_test.dart
TEST=tests/ffi/regress_49684_test.dart

Closes: https://github.com/dart-lang/sdk/issues/49684
Change-Id: I756635c0a34aa18f3a3a2cbdcc0657b08cb5050e
Cq-Include-Trybots: luci.dart.try:analyzer-linux-release-try,analyzer-mac-release-try,analyzer-win-release-try,analyzer-analysis-server-linux-try,vm-ffi-android-debug-arm64c-try,vm-precomp-ffi-qemu-linux-release-arm-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/261400
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Auto-Submit: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Daco Harkes 2022-09-27 15:58:16 +00:00 committed by Commit Queue
parent 95d559921d
commit 8a78aaf463
3 changed files with 40 additions and 3 deletions

View file

@ -385,11 +385,12 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
}
// Arguments can only be Pointer if the class extends
// NativeFieldWrapperClass1.
// Pointer or NativeFieldWrapperClass1.
for (var i = 0; i < formalParameters.length; i++) {
if (ffiParameterTypes[i].isPointer) {
final type = formalParameters[i].declaredElement!.type;
if (!_extendsNativeFieldWrapperClass1(type as InterfaceType)) {
final type =
formalParameters[i].declaredElement!.type as InterfaceType;
if (!type.isPointer && !_extendsNativeFieldWrapperClass1(type)) {
_errorReporter.reportErrorForNode(
FfiCode
.FFI_NATIVE_ONLY_CLASSES_EXTENDING_NATIVEFIELDWRAPPERCLASS1_CAN_BE_POINTER,

View file

@ -123,6 +123,14 @@ external double nonFfiReturnType(int v);
]);
}
test_FfiNativePointerParameter() async {
await assertNoErrorsInCode(r'''
import 'dart:ffi';
@FfiNative<Void Function(Pointer)>('free')
external void posixFree(Pointer pointer);
''');
}
test_FfiNativeTooFewParameters() async {
await assertErrorsInCode(r'''
import 'dart:ffi';

View file

@ -0,0 +1,28 @@
// Copyright (c) 2022, 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 'dart:ffi';
void main() {
// Does nothing, FfiNative aren't resolved.
}
@FfiNative<Int Function(Pointer<Int>, Int)>('subtract')
external int subtract(
Pointer<Int> a,
int b,
);
@FfiNative<Pointer<Double> Function(Pointer<Float>, Pointer<Float>)>(
'dividePrecision')
external Pointer<Double> dividePrecision(
Pointer<Float> a,
Pointer<Float> b,
);
@FfiNative<Void Function(Pointer)>('free')
external void posixFree(Pointer pointer);
@FfiNative<Void Function(Pointer)>('CoTaskMemFree')
external void winCoTaskMemFree(Pointer pv);