-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
area-native-interopUsed for native interop related issues, including FFI.Used for native interop related issues, including FFI.legacy-area-analyzerUse area-devexp instead.Use area-devexp instead.library-ffitriage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)Incorrect behavior (everything from a crash to more subtle misbehavior)
Description
Consider this code:
import 'dart:ffi';
@Native<Void Function(Pointer)>()
external void free(Pointer p);
@Native<Int32>()
external int x;
void main() {
Native.addressOf<NativeFunction>(free); // No analyzer error but CFE reports error
// ^
// Error: Expected type 'NativeFunction<Function>' to be a valid and instantiated subtype of 'NativeType'.
// - 'NativeFunction' is from 'dart:ffi'.
// - 'Function' is from 'dart:core'.
Native.addressOf<NativeFunction<Void Function(Pointer)>>(free); // No CFE error as expected
Native.addressOf(x); // No analyzer error but CFE reports error
// ^
// Error: Expected type 'NativeType' to be a valid and instantiated subtype of 'NativeType'.
// - 'NativeType' is from 'dart:ffi'.
Native.addressOf<Int32>(x); // No CFE error as expected
}- For native functions: If
NativeFunctionis used as a type argument forNative.addressOf, the analyzer doesn’t report any errors, but the CFE does. - For native fields: If no type argument is provided, the analyzer again doesn’t flag it, but the CFE does.
I looked into the relevant code in ffi_verifier.dart, and the analyzer only checks if the type argument is assignable to the native type. This doesn’t seem right – it should be checking if the types are exactly equal.
Here’s the code that handles these checks:
- Native functions:
if (!typeSystem.isAssignableTo(nativeType, targetFunctionType, - Native fields:
if (!typeSystem.isAssignableTo(nativeType, targetType)) {
If this sounds correct, I’d be happy to create a CL to fix it.
Metadata
Metadata
Assignees
Labels
area-native-interopUsed for native interop related issues, including FFI.Used for native interop related issues, including FFI.legacy-area-analyzerUse area-devexp instead.Use area-devexp instead.library-ffitriage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)Incorrect behavior (everything from a crash to more subtle misbehavior)