Skip to content

Commit 2c0ba56

Browse files
Clement SkauCommit Bot
Clement Skau
authored and
Commit Bot
committed
[vm/ffi] Add error for FFI annotation with opt. args.
Optional arguments (positional and named) in @FfiNative annotations are not meaningful, and should result in a compile-time error. TEST=tests/ffi/ffi_native_test.dart Bug: #47169 Change-Id: I8896e6a43f9399b537e6ee7c7a0e2857a370203f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/232622 Reviewed-by: Daco Harkes <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Clement Skau <[email protected]>
1 parent eb95543 commit 2c0ba56

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,52 @@ const MessageCode messageCantDisambiguateNotEnoughInformation = const MessageCod
686686
correctionMessage:
687687
r"""Try providing type arguments for the literal explicitly to disambiguate it.""");
688688

689+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
690+
const Template<Message Function(String name)> templateCantHaveNamedParameters =
691+
const Template<Message Function(String name)>(
692+
problemMessageTemplate:
693+
r"""'#name' can't be declared with named parameters.""",
694+
withArguments: _withArgumentsCantHaveNamedParameters);
695+
696+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
697+
const Code<Message Function(String name)> codeCantHaveNamedParameters =
698+
const Code<Message Function(String name)>(
699+
"CantHaveNamedParameters",
700+
);
701+
702+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
703+
Message _withArgumentsCantHaveNamedParameters(String name) {
704+
if (name.isEmpty) throw 'No name provided';
705+
name = demangleMixinApplicationName(name);
706+
return new Message(codeCantHaveNamedParameters,
707+
problemMessage: """'${name}' can't be declared with named parameters.""",
708+
arguments: {'name': name});
709+
}
710+
711+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
712+
const Template<Message Function(String name)>
713+
templateCantHaveOptionalParameters =
714+
const Template<Message Function(String name)>(
715+
problemMessageTemplate:
716+
r"""'#name' can't be declared with optional parameters.""",
717+
withArguments: _withArgumentsCantHaveOptionalParameters);
718+
719+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
720+
const Code<Message Function(String name)> codeCantHaveOptionalParameters =
721+
const Code<Message Function(String name)>(
722+
"CantHaveOptionalParameters",
723+
);
724+
725+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
726+
Message _withArgumentsCantHaveOptionalParameters(String name) {
727+
if (name.isEmpty) throw 'No name provided';
728+
name = demangleMixinApplicationName(name);
729+
return new Message(codeCantHaveOptionalParameters,
730+
problemMessage:
731+
"""'${name}' can't be declared with optional parameters.""",
732+
arguments: {'name': name});
733+
}
734+
689735
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
690736
const Code<Null> codeCantInferPackagesFromManyInputs =
691737
messageCantInferPackagesFromManyInputs;

pkg/front_end/lib/src/api_unstable/vm.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export '../fasta/fasta_codes.dart'
6868
messageFfiPackedAnnotationAlignment,
6969
messageNonPositiveArrayDimensions,
7070
noLength,
71+
templateCantHaveNamedParameters,
72+
templateCantHaveOptionalParameters,
7173
templateFfiDartTypeMismatch,
7274
templateFfiEmptyStruct,
7375
templateFfiExpectedConstantArg,

pkg/front_end/messages.status

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ CannotReadSdkSpecification/example: Fail
5656
CantDetermineConstness/analyzerCode: Fail
5757
CantDisambiguateAmbiguousInformation/analyzerCode: Fail # There's no analyzer code for that error yet.
5858
CantDisambiguateNotEnoughInformation/analyzerCode: Fail # There's no analyzer code for that error yet.
59+
CantHaveNamedParameters/analyzerCode: Fail
60+
CantHaveOptionalParameters/analyzerCode: Fail
5961
CantInferPackagesFromManyInputs/analyzerCode: Fail
6062
CantInferPackagesFromManyInputs/example: Fail
6163
CantInferPackagesFromPackageUri/analyzerCode: Fail

pkg/front_end/messages.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4760,6 +4760,16 @@ SpreadTypeMismatch:
47604760
var b = [...a];
47614761
}
47624762
4763+
CantHaveNamedParameters:
4764+
# Used by dart:ffi
4765+
problemMessage: "'#name' can't be declared with named parameters."
4766+
external: test/ffi_test.dart
4767+
4768+
CantHaveOptionalParameters:
4769+
# Used by dart:ffi
4770+
problemMessage: "'#name' can't be declared with optional parameters."
4771+
external: test/ffi_test.dart
4772+
47634773
SpreadElementTypeMismatch:
47644774
problemMessage: "Can't assign spread elements of type '#type' to collection elements of type '#type2'."
47654775
analyzerCode: LIST_ELEMENT_TYPE_NOT_ASSIGNABLE

pkg/vm/lib/transformations/ffi/native.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'package:front_end/src/api_unstable/vm.dart'
66
show
77
messageFfiNativeMustBeExternal,
88
messageFfiNativeOnlyNativeFieldWrapperClassCanBePointer,
9+
templateCantHaveNamedParameters,
10+
templateCantHaveOptionalParameters,
911
templateFfiNativeUnexpectedNumberOfParameters,
1012
templateFfiNativeUnexpectedNumberOfParametersWithReceiver;
1113

@@ -344,6 +346,25 @@ class FfiNativeTransformer extends FfiTransformer {
344346
// annotation matches.
345347
bool _verifySignatures(Procedure node, FunctionType dartFunctionType,
346348
FunctionType ffiFunctionType, int annotationOffset) {
349+
if (ffiFunctionType.namedParameters.length > 0) {
350+
diagnosticReporter.report(
351+
templateCantHaveNamedParameters.withArguments('FfiNative'),
352+
annotationOffset,
353+
0,
354+
node.location?.file);
355+
return false;
356+
}
357+
358+
if (ffiFunctionType.positionalParameters.length >
359+
ffiFunctionType.requiredParameterCount) {
360+
diagnosticReporter.report(
361+
templateCantHaveOptionalParameters.withArguments('FfiNative'),
362+
annotationOffset,
363+
0,
364+
node.location?.file);
365+
return false;
366+
}
367+
347368
if (dartFunctionType.positionalParameters.length !=
348369
ffiFunctionType.positionalParameters.length) {
349370
final template = (node.isStatic
@@ -384,7 +405,7 @@ class FfiNativeTransformer extends FfiTransformer {
384405
FunctionType ffiFunctionType,
385406
List<Expression> argumentList) {
386407
if (!_verifySignatures(
387-
node, ffiFunctionType, dartFunctionType, annotationOffset)) {
408+
node, dartFunctionType, ffiFunctionType, annotationOffset)) {
388409
return node;
389410
}
390411

tests/ffi/ffi_native_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,12 @@ class DoesNotExtend implements NativeFieldWrapperClass1 {
9595
external static int bad3(DoesNotExtend obj); //# 11: compile-time error
9696
}
9797

98+
// Error: 'FfiNative' can't be declared with optional parameters.
99+
@FfiNative<Void Function([Double])>('doesntmatter') //# 12: compile-time error
100+
external static int badOptParam(); //# 12: compile-time error
101+
102+
// Error: 'FfiNative' can't be declared with named parameters.
103+
@FfiNative<Void Function({Double})>('doesntmatter') //# 13: compile-time error
104+
external static int badNamedParam(); //# 13: compile-time error
105+
98106
void main() {/* Intentionally empty: Compile-time error tests. */}

0 commit comments

Comments
 (0)