Skip to content

Analyzer and CFE disagree on FFI @Native annotations on extensions #60250

@simolus3

Description

@simolus3

Consider this example program:

import 'dart:ffi';
import 'dart:isolate';

extension type NativeSendPort(int id) {
  @Native<Bool Function(Int64, Int64)>(symbol: 'Dart_PostInteger')
  external bool postInteger(int message);
}

extension on int {
  @Native<Bool Function(Int64, Int64)>(symbol: 'Dart_PostInteger')
  external bool postInteger(int message);
}

extension type InvalidNativeSendPort._(double id) {
  @Native<Bool Function(Int64, Int64)>(symbol: 'Dart_PostInteger')
  external bool postInteger(int message);
}

extension on double {
  @Native<Bool Function(Int64, Int64)>(symbol: 'Dart_PostInteger')
  external bool postInteger(int message);
}

void main() {
  final port = ReceivePort();
  port.listen((data) => print('Received $data'));

  final nativePort = NativeSendPort(port.sendPort.nativePort);
  nativePort.postInteger(1234);
  nativePort.id.postInteger(4321);
  1.234.postInteger(1234);
}

Running dart analyze on that program reports no errors, but the VM transformation finds a few:

example/repro.dart:10:17: Error: Expected type 'bool Function(NativeSendPort, int)' to be 'bool Function(int, int)', which is the Dart type corresponding to 'NativeFunction<Bool Function(Int64, Int64)>'.
 - 'NativeFunction' is from 'dart:ffi'.
 - 'Bool' is from 'dart:ffi'.
 - 'Int64' is from 'dart:ffi'.
  external bool postInteger(int message);
                ^
example/repro.dart:20:17: Error: Expected type 'bool Function(InvalidNativeSendPort, int)' to be 'bool Function(int, int)', which is the Dart type corresponding to 'NativeFunction<Bool Function(Int64, Int64)>'.
 - 'NativeFunction' is from 'dart:ffi'.
 - 'Bool' is from 'dart:ffi'.
 - 'Int64' is from 'dart:ffi'.
  external bool postInteger(int message);
                ^
example/repro.dart:25:17: Error: Expected type 'bool Function(double, int)' to be 'bool Function(int, int)', which is the Dart type corresponding to 'NativeFunction<Bool Function(Int64, Int64)>'.
 - 'NativeFunction' is from 'dart:ffi'.
 - 'Bool' is from 'dart:ffi'.
 - 'Int64' is from 'dart:ffi'.
  external bool postInteger(int message);
                ^

For the extension type and method on double, the analyzer is clearly in the wrong and should prevent that method from being defined with the mismatching type.
I'm not sure if the first extension type on int is supposed to be forbidden too, it would be nice if that definition could work. But in either case, CFE and analyzer should probably agree on the errors.

Tested with Dart SDK version: 3.8.0-edge.388f22a88bc809b5938f3f1a70bb760ab3c65e4a (main) (Tue Mar 4 07:50:28 2025 -0800) on "macos_arm64"

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.area-native-interopUsed for native interop related issues, including FFI.contributions-welcomeContributions welcome to help resolve this (the resolution is expected to be clear from the issue)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