Issue 52918. Report -TYPE_PARAMETER_REFERENCED_BY_STATIC for extension.

Bug: https://github.com/dart-lang/sdk/issues/52918
Change-Id: I2c21239c34bf852a1770296491f30d81dd5eb66c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313520
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-07-13 17:53:38 +00:00 committed by Commit Queue
parent b4cdba66e2
commit 1cf1cf3215
2 changed files with 50 additions and 20 deletions

View file

@ -4666,7 +4666,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
}) {
if (_enclosingExecutable.inStaticMethod || _isInStaticVariableDeclaration) {
if (element is TypeParameterElement &&
element.enclosingElement2 is InterfaceElement) {
element.enclosingElement2 is InstanceElement) {
// The class's type parameters are not in scope for static methods.
// However all other type parameters are legal (e.g. the static method's
// type parameters, or a local function's type parameters).

View file

@ -15,19 +15,7 @@ main() {
@reflectiveTest
class TypeParameterReferencedByStaticTest extends PubPackageResolutionTest {
test_expression_method() async {
await assertErrorsInCode('''
class A<T> {
static foo() {
T;
}
}
''', [
error(CompileTimeErrorCode.TYPE_PARAMETER_REFERENCED_BY_STATIC, 34, 1),
]);
}
test_type_field() async {
test_class_field() async {
await assertErrorsInCode('''
class A<T> {
static T? foo;
@ -37,7 +25,7 @@ class A<T> {
]);
}
test_type_getter() async {
test_class_getter() async {
await assertErrorsInCode('''
class A<T> {
static T? get foo => null;
@ -47,7 +35,7 @@ class A<T> {
]);
}
test_type_method_bodyReference() async {
test_class_method_bodyReference() async {
await assertErrorsInCode('''
class A<T> {
static foo() {
@ -60,7 +48,7 @@ class A<T> {
]);
}
test_type_method_closure() async {
test_class_method_closure() async {
await assertErrorsInCode('''
class A<T> {
static Object foo() {
@ -72,7 +60,7 @@ class A<T> {
]);
}
test_type_method_parameter() async {
test_class_method_parameter() async {
await assertErrorsInCode('''
class A<T> {
static foo(T a) {}
@ -82,7 +70,7 @@ class A<T> {
]);
}
test_type_method_return() async {
test_class_method_return() async {
await assertErrorsInCode('''
class A<T> {
static T foo() {
@ -94,7 +82,7 @@ class A<T> {
]);
}
test_type_setter() async {
test_class_setter() async {
await assertErrorsInCode('''
class A<T> {
static set foo(T _) {}
@ -103,4 +91,46 @@ class A<T> {
error(CompileTimeErrorCode.TYPE_PARAMETER_REFERENCED_BY_STATIC, 30, 1),
]);
}
test_expression_method() async {
await assertErrorsInCode('''
class A<T> {
static foo() {
T;
}
}
''', [
error(CompileTimeErrorCode.TYPE_PARAMETER_REFERENCED_BY_STATIC, 34, 1),
]);
}
test_extension_field() async {
await assertErrorsInCode('''
extension E<T> on int {
static T? foo;
}
''', [
error(CompileTimeErrorCode.TYPE_PARAMETER_REFERENCED_BY_STATIC, 33, 1),
]);
}
test_extension_method_return() async {
await assertErrorsInCode('''
extension E<T> on int {
static T foo() => throw 0;
}
''', [
error(CompileTimeErrorCode.TYPE_PARAMETER_REFERENCED_BY_STATIC, 33, 1),
]);
}
test_mixin_field() async {
await assertErrorsInCode('''
mixin A<T> {
static T? foo;
}
''', [
error(CompileTimeErrorCode.TYPE_PARAMETER_REFERENCED_BY_STATIC, 22, 1),
]);
}
}