[analyzer/ffi] Error on sizeOf<Handle>()

Test covers both analyzer and CFE throwing an error. (But does not
test what error.)

Closes: https://github.com/dart-lang/sdk/issues/45100

Change-Id: I8b820347945d88304d3f03d7d03f156b7111db42
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193405
Auto-Submit: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Daco Harkes 2021-03-30 00:20:52 +00:00 committed by commit-bot@chromium.org
parent 38d5c5e444
commit 93a187e6c8
3 changed files with 28 additions and 18 deletions

View file

@ -269,13 +269,13 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
return false;
}
if (!_isValidFfiNativeType(nativeType.returnType,
allowVoid: true, allowEmptyStruct: false)) {
allowVoid: true, allowEmptyStruct: false, allowHandle: true)) {
return false;
}
for (final DartType typeArg in nativeType.normalParameterTypes) {
if (!_isValidFfiNativeType(typeArg,
allowVoid: false, allowEmptyStruct: false)) {
allowVoid: false, allowEmptyStruct: false, allowHandle: true)) {
return false;
}
}
@ -288,13 +288,21 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
bool _isValidFfiNativeType(DartType? nativeType,
{bool allowVoid = false,
bool allowEmptyStruct = false,
bool allowArray = false}) {
bool allowArray = false,
bool allowHandle = false}) {
if (nativeType is InterfaceType) {
// Is it a primitive integer/double type (or ffi.Void if we allow it).
final primitiveType = _primitiveNativeType(nativeType);
if (primitiveType != _PrimitiveDartType.none &&
(primitiveType != _PrimitiveDartType.void_ || allowVoid)) {
return true;
switch (primitiveType) {
case _PrimitiveDartType.void_:
return allowVoid;
case _PrimitiveDartType.handle:
return allowHandle;
case _PrimitiveDartType.double:
case _PrimitiveDartType.int:
return true;
case _PrimitiveDartType.none:
// These are the cases below.
break;
}
if (nativeType.isNativeFunction) {
return _isValidFfiNativeFunctionType(nativeType.typeArguments.single);
@ -302,7 +310,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
if (nativeType.isPointer) {
final nativeArgumentType = nativeType.typeArguments.single;
return _isValidFfiNativeType(nativeArgumentType,
allowVoid: true, allowEmptyStruct: true) ||
allowVoid: true, allowEmptyStruct: true, allowHandle: true) ||
nativeArgumentType.isStructSubtype ||
nativeArgumentType.isNativeType;
}

View file

@ -58,6 +58,7 @@ void main() {
testRefStruct();
testSizeOfGeneric();
testSizeOfNativeType();
testSizeOfHandle();
testElementAtGeneric();
testElementAtNativeType();
}
@ -636,11 +637,11 @@ void testSizeOfGeneric() {
}
void testSizeOfNativeType() {
try {
sizeOf(); //# 1301: compile-time error
} catch (e) {
print(e);
}
sizeOf(); //# 1301: compile-time error
}
void testSizeOfHandle() {
sizeOf<Handle>(); //# 1302: compile-time error
}
void testElementAtGeneric() {

View file

@ -58,6 +58,7 @@ void main() {
testRefStruct();
testSizeOfGeneric();
testSizeOfNativeType();
testSizeOfHandle();
testElementAtGeneric();
testElementAtNativeType();
}
@ -634,11 +635,11 @@ void testSizeOfGeneric() {
}
void testSizeOfNativeType() {
try {
sizeOf(); //# 1301: compile-time error
} catch (e) {
print(e);
}
sizeOf(); //# 1301: compile-time error
}
void testSizeOfHandle() {
sizeOf<Handle>(); //# 1302: compile-time error
}
void testElementAtGeneric() {