Skip to content

Commit

Permalink
[analyzer/ffi] Error on missing type argument to @Native
Browse files Browse the repository at this point in the history
Also, update error messages to mention `Native` rather than
`FfiNative`, as `FfiNative` syntax is deprecated.

TEST=tests/ffi/regress_51913_test.dart

Closes: #51913
Change-Id: I0830a360bfdd9faffb04761d1ce2da33b380341d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293687
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
dcharkes authored and Commit Queue committed Apr 6, 2023
1 parent ef7d8d3 commit 6275efd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/analyzer/lib/src/generated/ffi_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ class FfiVerifier extends RecursiveAstVisitor<void> {

final typeArguments = annotation.typeArguments?.arguments;
final arguments = annotation.arguments?.arguments;
if (typeArguments == null || arguments == null) {
continue;
if (typeArguments == null) {
_errorReporter.reportErrorForNode(
FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE, errorNode, ['T', 'Native']);
return;
}

final ffiSignature = typeArguments[0].type! as FunctionType;
Expand Down Expand Up @@ -400,13 +402,13 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
_errorReporter.reportErrorForNode(
FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE,
errorNode,
[nativeType, 'FfiNative']);
[nativeType, 'Native']);
return;
}
if (!_validateCompatibleFunctionTypes(dartType, nativeType,
nativeFieldWrappersAsPointer: true, allowStricterReturn: true)) {
_errorReporter.reportErrorForNode(FfiCode.MUST_BE_A_SUBTYPE, errorNode,
[nativeType, dartType, 'FfiNative']);
[nativeType, dartType, 'Native']);
return;
}
}
Expand Down Expand Up @@ -926,8 +928,8 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
}

void _validateFfiLeafCallUsesNoHandles(
NodeList<Expression> args, DartType nativeType, AstNode errorNode) {
if (args.isEmpty) {
NodeList<Expression>? args, DartType nativeType, AstNode errorNode) {
if (args == null || args.isEmpty) {
return;
}
for (final arg in args) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/test/src/diagnostics/ffi_native_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import 'dart:ffi';
external int foo();
''', [
error(CompileTimeErrorCode.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS, 20, 10),
error(FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE, 20, 30),
]);
}

Expand All @@ -51,6 +52,7 @@ import 'dart:ffi';
@FfiNative()
external int foo();
''', [
error(FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE, 20, 32),
error(CompileTimeErrorCode.NOT_ENOUGH_POSITIONAL_ARGUMENTS_NAME_SINGULAR,
31, 1),
]);
Expand Down Expand Up @@ -210,6 +212,7 @@ import 'dart:ffi';
external int foo();
''', [
error(CompileTimeErrorCode.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS, 20, 7),
error(FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE, 20, 27),
]);
}

Expand Down

0 comments on commit 6275efd

Please sign in to comment.