Skip to content

[vm/ffi] Missing analyzer error for type argument in Native.addressOf #59675

@halildurmus

Description

@halildurmus

@dcharkes

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 NativeFunction is used as a type argument for Native.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:

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.legacy-area-analyzerUse area-devexp instead.library-ffitriage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions