dart-sdk/tests/ffi/vmspecific_static_checks_ffinative_test.dart
Daco Harkes e364fda064 Reland "[cfe/ffi] Improve FFI call mismatched types compile errors"
This is a reland of commit 206fdf148c

Original change's description:
> [cfe/ffi] Improve FFI call mismatched types compile errors
>
> This CL fixes two issues.
>
> 1. `FfiNative`s now check the Dart and native type for compatiblity.
> 2. Both `FfiNative`, `asFunction`, and `lookupFunction` check the type
>    correspondence between native and Dart type with a subtype check of
>    the expected Dart type and the provided Dart type. For functions,
>    any return type is a subtype of a void type. This is fine for Dart,
>    but not for native calls. This CL manually checks the return type
>    for void.
>
> This CL does not fix the inconsistency between `asFunction` and
> `FfiNative` with regard to allowing more strict return types than
> `Object` for `Handle`s
> Issue: https://github.com/dart-lang/sdk/issues/49518
>
> Analyzer fixes in follow up CL.
>
> TEST=tests/ffi/vmspecific_static_checks_ffinative_test.dart
>
> Closes: https://github.com/dart-lang/sdk/issues/49471
> Change-Id: Ibc7bd6a1a0db59cc5fa5d755d76999fd7e9a06a4
> Cq-Include-Trybots: luci.dart.try:analyzer-linux-release-try,analyzer-mac-release-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64c-try
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252601
> Reviewed-by: Martin Kustermann <kustermann@google.com>
> Commit-Queue: Daco Harkes <dacoharkes@google.com>

TEST=tests/ffi/vmspecific_static_checks_ffinative_test.dart

Change-Id: Ic1efba45ae8ff2585fc67fdf63c653ce090d0337
Cq-Include-Trybots: luci.dart.try:analyzer-linux-release-try,analyzer-mac-release-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64c-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252663
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2022-08-01 16:09:03 +00:00

97 lines
4.4 KiB
Dart

// 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.
// NOTE: There is no `test/ffi_2/...` version of this test since annotations
// with type arguments isn't supported in that version of Dart.
import 'dart:ffi';
import 'dart:nativewrappers';
void main() {
/* Intentionally empty: Compile-time error tests. */
}
// Error: FFI leaf call must not have Handle return type.
@FfiNative<Handle Function()>("foo", isLeaf: true) //# 01: compile-time error
external Object foo(); //# 01: compile-time error
// Error: FFI leaf call must not have Handle argument types.
@FfiNative<Void Function(Handle)>("bar", //# 02: compile-time error
isLeaf: true) //# 02: compile-time error
external void bar(Object); //# 02: compile-time error
class Classy {
// Error: Missing receiver in FfiNative annotation.
@FfiNative<Void Function(IntPtr)>('doesntmatter') //# 03: compile-time error
external void badMissingReceiver(int v); //# 03: compile-time error
// Error: Class doesn't extend NativeFieldWrapperClass1 - can't be converted
// to Pointer.
@FfiNative<Void Function(Pointer<Void>, IntPtr)>(//# 04: compile-time error
'doesntmatter') //# 04: compile-time error
external void badHasReceiverPointer(int v); //# 04: compile-time error
}
class NativeClassy extends NativeFieldWrapperClass1 {
// Error: Missing receiver in FfiNative annotation.
@FfiNative<Void Function(IntPtr)>('doesntmatter') //# 05: compile-time error
external void badMissingReceiver(int v); //# 05: compile-time error
// Error: wrong return type.
@FfiNative<Handle Function(Pointer<Void>, Uint32, Uint32, Handle)>('doesntmatter') //# 49471: compile-time error
external void toImageSync(int width, int height, Object outImage); //# 49471: compile-time error
}
// Error: Too many FfiNative parameters.
@FfiNative<Handle Function(IntPtr, IntPtr)>(//# 06: compile-time error
'doesntmatter') //# 06: compile-time error
external Object badTooManyFfiParameter(int v); //# 06: compile-time error
// Error: Too few FfiNative parameters.
@FfiNative<Handle Function(IntPtr)>('doesntmatter') //# 07: compile-time error
external Object badTooFewFfiParameter(int v, int v2); //# 07: compile-time error
// Error: FfiNatives must be marked external (and by extension have no body).
@FfiNative<Void Function()>('doesntmatter') //# 08: compile-time error
void mustBeMarkedExternal() {} //# 08: compile-time error
class DoesNotExtend implements NativeFieldWrapperClass1 {
// Error: Receiver type can't be converted to Pointer since it doesn't extend
// NativeFieldWrapperClass1.
@FfiNative<IntPtr Function(Pointer<Void>, Handle)>(//# 09: compile-time error
'doesntmatter') //# 09: compile-time error
external int bad1(DoesNotExtend obj); //# 09: compile-time error
// Error: Parameter type can't be converted to Pointer since it doesn't extend
// NativeFieldWrapperClass1.
@FfiNative<IntPtr Function(Handle, Pointer<Void>)>(//# 10: compile-time error
'doesntmatter') //# 10: compile-time error
external int bad2(DoesNotExtend obj); //# 10: compile-time error
// Error: Parameter type can't be converted to Pointer since it doesn't extend
// NativeFieldWrapperClass1.
@FfiNative<IntPtr Function(Pointer<Void>)>(//# 11: compile-time error
'doesntmatter') //# 11: compile-time error
external static int bad3(DoesNotExtend obj); //# 11: compile-time error
}
// Error: 'FfiNative' can't be declared with optional parameters.
@FfiNative<Void Function([Double])>('doesntmatter') //# 12: compile-time error
external static int badOptParam(); //# 12: compile-time error
// Error: 'FfiNative' can't be declared with named parameters.
@FfiNative<Void Function({Double})>('doesntmatter') //# 13: compile-time error
external static int badNamedParam(); //# 13: compile-time error
@FfiNative<IntPtr Function(Double)>('doesntmatter') //# 14: compile-time error
external int wrongFfiParameter(int v); //# 14: compile-time error
@FfiNative<IntPtr Function(IntPtr)>('doesntmatter') //# 15: compile-time error
external double wrongFfiReturnType(int v); //# 15: compile-time error
@FfiNative<IntPtr Function(int)>('doesntmatter') //# 16: compile-time error
external int nonFfiParameter(int v); //# 16: compile-time error
@FfiNative<double Function(IntPtr)>('doesntmatter') //# 17: compile-time error
external double nonFfiReturnType(int v); //# 17: compile-time error