Skip to content

Commit c0ad7b7

Browse files
halildurmusCommit Queue
authored andcommitted
[vm/ffi] Report error for imprecise type argument in Native.addressOf
Closes: #59675 Change-Id: Idf7b35f2a2aad6da1204ecc21e84cd04f0de57b8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399560 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Daco Harkes <[email protected]>
1 parent e200a3a commit c0ad7b7

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

pkg/analyzer/lib/src/generated/ffi_verifier.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,8 +1706,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
17061706
} else {
17071707
var targetFunctionType =
17081708
(targetType as InterfaceType).typeArguments[0];
1709-
if (!typeSystem.isAssignableTo(nativeType, targetFunctionType,
1710-
strictCasts: strictCasts)) {
1709+
if (!typeSystem.isEqualTo(nativeType, targetFunctionType)) {
17111710
_errorReporter.atNode(
17121711
node,
17131712
FfiCode.MUST_BE_A_SUBTYPE,
@@ -1731,7 +1730,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
17311730
}
17321731
}
17331732

1734-
if (!typeSystem.isAssignableTo(nativeType, targetType)) {
1733+
if (!typeSystem.isEqualTo(nativeType, targetType)) {
17351734
_errorReporter.atNode(
17361735
node,
17371736
FfiCode.MUST_BE_A_SUBTYPE,

pkg/analyzer/test/src/diagnostics/ffi_native_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ void main() {
7373
]);
7474
}
7575

76+
test_invalid_MissingType2() async {
77+
await assertErrorsInCode(r'''
78+
import 'dart:ffi';
79+
80+
@Native()
81+
external Pointer<IntPtr> global;
82+
83+
void main() => print(Native.addressOf(global));
84+
''', [
85+
error(FfiCode.MUST_BE_A_SUBTYPE, 85, 24),
86+
]);
87+
}
88+
7689
test_invalid_NotAConstant() async {
7790
await assertErrorsInCode(r'''
7891
import 'dart:ffi';
@@ -90,6 +103,19 @@ void entry(bool condition) {
90103
]);
91104
}
92105

106+
test_invalid_NotAPreciseType() async {
107+
await assertErrorsInCode(r'''
108+
import 'dart:ffi';
109+
110+
@Native<Void Function()>()
111+
external void foo();
112+
113+
void main() => print(Native.addressOf<NativeFunction>(foo));
114+
''', [
115+
error(FfiCode.MUST_BE_A_SUBTYPE, 90, 37),
116+
]);
117+
}
118+
93119
test_invalid_String() async {
94120
await assertErrorsInCode(r'''
95121
import 'dart:ffi';

tests/ffi/static_checks/vmspecific_static_checks_native_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ void addressOf() {
171171
// ^
172172
// [cfe] Expected type 'NativeType' to be a valid and instantiated subtype of 'NativeType'.
173173

174+
Native.addressOf<NativeFunction>(_valid);
175+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
176+
// [analyzer] COMPILE_TIME_ERROR.MUST_BE_A_SUBTYPE
177+
// ^
178+
// [cfe] Expected type 'NativeFunction<Function>' to be a valid and instantiated subtype of 'NativeType'.
179+
174180
Native.addressOf<NativeFunction<Void Function(Int)>>(_valid);
175181
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
176182
// [analyzer] COMPILE_TIME_ERROR.MUST_BE_A_SUBTYPE
@@ -179,6 +185,12 @@ void addressOf() {
179185

180186
Native.addressOf<NativeFunction<ComplexNativeFunction>>(validNative);
181187

188+
Native.addressOf(myStruct0);
189+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^
190+
// [analyzer] COMPILE_TIME_ERROR.MUST_BE_A_SUBTYPE
191+
// ^
192+
// [cfe] Expected type 'NativeType' to be a valid and instantiated subtype of 'NativeType'.
193+
182194
Native.addressOf<MyStruct>(myStruct0);
183195
Native.addressOf<MyStruct>(myStruct1);
184196
Native.addressOf<Pointer<MyStruct>>(myStructPtr0);

0 commit comments

Comments
 (0)