Report error on function tearoff w/ type args on dynamic

From the spec:

> We do not allow dynamic explicit instantiation. If an
expression _e_ has type `dynamic` (or `Never`), then
`e.foo<int>` is a compile-time error for any name `foo`.

Bug: https://github.com/dart-lang/sdk/issues/46020
Change-Id: I041c3fcac77fe8edf64633a8d23dbaa89cf42f77
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/209623
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Sam Rawlins 2021-08-10 18:36:11 +00:00 committed by commit-bot@chromium.org
parent 799b39ad42
commit 2c32ff9331
4 changed files with 33 additions and 2 deletions

View file

@ -200,6 +200,7 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.FOR_IN_WITH_CONST_VARIABLE,
CompileTimeErrorCode.GENERIC_FUNCTION_TYPE_CANNOT_BE_BOUND,
CompileTimeErrorCode.GENERIC_FUNCTION_TYPE_CANNOT_BE_TYPE_ARGUMENT,
CompileTimeErrorCode.GENERIC_METHOD_TYPE_INSTANTIATION_ON_DYNAMIC,
CompileTimeErrorCode.GETTER_NOT_ASSIGNABLE_SETTER_TYPES,
CompileTimeErrorCode.GETTER_NOT_SUBTYPE_SETTER_TYPES,
CompileTimeErrorCode.IF_ELEMENT_CONDITION_FROM_DEFERRED_LIBRARY,

View file

@ -278,8 +278,11 @@ class FunctionReferenceResolver {
function.prefix.staticType = prefixType;
if (prefixType != null && prefixType.isDynamic) {
// TODO(srawlins): Report error. See spec text: "We do not allow dynamic
// explicit instantiation."
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.GENERIC_METHOD_TYPE_INSTANTIATION_ON_DYNAMIC,
function,
[],
);
node.staticType = DynamicTypeImpl.instance;
return;
}

View file

@ -5207,6 +5207,19 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
correction: "Try removing type parameters from the generic function "
"type, or using 'dynamic' as the type argument here.");
/**
* No parameters.
*/
static const CompileTimeErrorCode
GENERIC_METHOD_TYPE_INSTANTIATION_ON_DYNAMIC = CompileTimeErrorCode(
'GENERIC_METHOD_TYPE_INSTANTIATION_ON_DYNAMIC',
"A method tearoff on a target whose type is 'dynamic' can't have type "
"arguments.",
correction:
"Specify the type of the target, or remove the type arguments from the "
"method tearoff.",
);
/**
* 10.3 Setters: It is a compile-time error if a class has a setter named
* `v=` with argument type `T` and a getter named `v` with return type `S`,

View file

@ -582,6 +582,20 @@ void f(void Function<T>(T a) foo, void Function<T>(T a) bar) {
assertType(reference, 'void Function(dynamic)');
}
test_receiverIsDynamic() async {
await assertErrorsInCode('''
bar(dynamic a) {
a.foo<int>;
}
''', [
error(CompileTimeErrorCode.GENERIC_METHOD_TYPE_INSTANTIATION_ON_DYNAMIC,
19, 5),
]);
var reference = findNode.functionReference('a.foo<int>;');
assertType(reference, 'dynamic');
}
test_staticMethod() async {
await assertNoErrorsInCode('''
class A {