[analyzer/ffi] Non const type argument error on receiver

Bug: https://github.com/dart-lang/sdk/issues/36780

Change-Id: I2dd98d58f0045d25afcfb92a43f858e5f2bcc1fd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217009
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/dart/error/ffi_code.g.dart b/pkg/analyzer/lib/src/dart/error/ffi_code.g.dart
index c2b7d56..4264f45 100644
--- a/pkg/analyzer/lib/src/dart/error/ffi_code.g.dart
+++ b/pkg/analyzer/lib/src/dart/error/ffi_code.g.dart
@@ -282,9 +282,9 @@
    */
   static const FfiCode NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER = FfiCode(
     'NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER',
-    "The type argument for the pointer '{0}' must be a 'NativeFunction' in order to use 'asFunction'.",
+    "The type argument for the pointer '{0}' must be a valid 'NativeFunction' in order to use 'asFunction'.",
     correctionMessage:
-        "Try changing the type argument to be a 'NativeFunction'.",
+        "Try changing the function argument in 'NativeFunction' to only use NativeTypes.",
   );
 
   /**
diff --git a/pkg/analyzer/lib/src/generated/ffi_verifier.dart b/pkg/analyzer/lib/src/generated/ffi_verifier.dart
index df2a706..033f4c4 100644
--- a/pkg/analyzer/lib/src/generated/ffi_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/ffi_verifier.dart
@@ -651,9 +651,16 @@
     var targetType = target.staticType;
     if (targetType is InterfaceType && targetType.isPointer) {
       final DartType T = targetType.typeArguments[0];
-      if (!T.isNativeFunction ||
-          !_isValidFfiNativeFunctionType(
-              (T as InterfaceType).typeArguments.single)) {
+      if (!T.isNativeFunction) {
+        return;
+      }
+      final DartType pointerTypeArg = (T as InterfaceType).typeArguments.single;
+      if (pointerTypeArg is TypeParameterType) {
+        _errorReporter.reportErrorForNode(
+            FfiCode.NON_CONSTANT_TYPE_ARGUMENT, target, ['asFunction']);
+        return;
+      }
+      if (!_isValidFfiNativeFunctionType(pointerTypeArg)) {
         final AstNode errorNode =
             typeArguments != null ? typeArguments[0] : node;
         _errorReporter.reportErrorForNode(
diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml
index 079cc69..2f331e2 100644
--- a/pkg/analyzer/messages.yaml
+++ b/pkg/analyzer/messages.yaml
@@ -13887,8 +13887,8 @@
       Parameters:
       0: the name of the function, method, or constructor having type arguments
   NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER:
-    problemMessage: "The type argument for the pointer '{0}' must be a 'NativeFunction' in order to use 'asFunction'."
-    correctionMessage: "Try changing the type argument to be a 'NativeFunction'."
+    problemMessage: "The type argument for the pointer '{0}' must be a valid 'NativeFunction' in order to use 'asFunction'."
+    correctionMessage: "Try changing the function argument in 'NativeFunction' to only use NativeTypes."
     comment: |-
       Parameters:
       0: the type that should be a valid dart:ffi native type.
diff --git a/pkg/analyzer/test/src/diagnostics/non_native_function_type_argument_to_pointer_test.dart b/pkg/analyzer/test/src/diagnostics/non_native_function_type_argument_to_pointer_test.dart
index 323c81b..40cedd5 100644
--- a/pkg/analyzer/test/src/diagnostics/non_native_function_type_argument_to_pointer_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/non_native_function_type_argument_to_pointer_test.dart
@@ -58,7 +58,7 @@
   }
 }
 ''', [
-      error(FfiCode.NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER, 138, 1),
+      error(FfiCode.NON_CONSTANT_TYPE_ARGUMENT, 125, 1),
     ]);
   }
 }