From 721ff350c0288f7cf57b2df58c3d6ff59f495433 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 12 Aug 2019 20:12:05 +0000 Subject: [PATCH] Extract TYPE_ARGUMENT_NOT_MATCHING_BOUNDS tests. R=brianwilkerson@google.com Change-Id: Icea3c1d158d54a5d5d08e8727618f3558760615c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112752 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../generated/compile_time_error_code.dart | 13 - .../static_type_warning_code_test.dart | 275 ---------------- .../test/src/diagnostics/test_all.dart | 3 + ...ype_argument_not_matching_bounds_test.dart | 305 ++++++++++++++++++ 4 files changed, 308 insertions(+), 288 deletions(-) create mode 100644 pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart diff --git a/pkg/analyzer/test/generated/compile_time_error_code.dart b/pkg/analyzer/test/generated/compile_time_error_code.dart index a7d99b705493..e0116146b31c 100644 --- a/pkg/analyzer/test/generated/compile_time_error_code.dart +++ b/pkg/analyzer/test/generated/compile_time_error_code.dart @@ -4881,19 +4881,6 @@ typedef A>(); ]); } - test_typeArgumentNotMatchingBounds_const() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G { - const G(); -} -f() { return const G(); } -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 81, 1), - ]); - } - test_typedef_infiniteParameterBoundCycle() async { await assertErrorsInCode(r''' typedef F = F Function(); diff --git a/pkg/analyzer/test/generated/static_type_warning_code_test.dart b/pkg/analyzer/test/generated/static_type_warning_code_test.dart index 6b2cfd515613..b9c570da8e92 100644 --- a/pkg/analyzer/test/generated/static_type_warning_code_test.dart +++ b/pkg/analyzer/test/generated/static_type_warning_code_test.dart @@ -1063,281 +1063,6 @@ void main() { } ]); } - test_typeArgumentNotMatchingBounds_classTypeAlias() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class C {} -class G {} -class D = G with C; -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 69, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_extends() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -class C extends G{} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 64, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_extends_regressionInIssue18468Fix() async { - // https://code.google.com/p/dart/issues/detail?id=18628 - await assertErrorsInCode(r''' -class X {} -class Y extends X {} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 48, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_fieldFormalParameter() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -class C { - var f; - C(G this.f) {} -} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 71, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_functionReturnType() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -G f() { return null; } -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 48, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_functionTypeAlias() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -typedef G f(); -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 56, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_functionTypedFormalParameter() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -f(G h()) {} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 50, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_implements() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -class C implements G{} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 67, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_is() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -var b = 1 is G; -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 61, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_methodInvocation_localFunction() async { - await assertErrorsInCode(r''' -class Point { - Point(T x, T y); -} - -main() { - Point f(T x, T y) { - return new Point(x, y); - } - print(f('hello', 'world')); -} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 145, 6), - ]); - } - - test_typeArgumentNotMatchingBounds_methodInvocation_method() async { - await assertErrorsInCode(r''' -class Point { - Point(T x, T y); -} - -class PointFactory { - Point point(T x, T y) { - return new Point(x, y); - } -} - -f(PointFactory factory) { - print(factory.point('hello', 'world')); -} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 202, 6), - ]); - } - - test_typeArgumentNotMatchingBounds_methodInvocation_topLevelFunction() async { - await assertErrorsInCode(r''' -class Point { - Point(T x, T y); -} - -Point f(T x, T y) { - return new Point(x, y); -} - -main() { - print(f('hello', 'world')); -} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 140, 6), - ]); - } - - test_typeArgumentNotMatchingBounds_methodReturnType() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -class C { - G m() { return null; } -} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 60, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_new() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -f() { return new G(); } -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 65, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_new_superTypeOfUpperBound() async { - await assertErrorsInCode(r''' -class A {} -class B extends A {} -class C extends B {} -class G {} -f() { return new G(); } -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 96, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_ofFunctionTypeAlias() async { - await assertErrorsInCode(r''' -class A {} -class B {} -typedef F(); -F fff; -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 50, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_parameter() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -f(G g) {} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 50, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_redirectingConstructor() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class X { - X(int x, int y) {} - factory X.name(int x, int y) = X; -} -''', [ - error(StaticWarningCode.REDIRECT_TO_INVALID_RETURN_TYPE, 99, 4), - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 101, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_typeArgumentList() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class C {} -class D {} -C> Var; -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 64, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_typeParameter() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class C {} -class G {} -class D> {} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 77, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_variableDeclaration() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -G g; -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 48, 1), - ]); - } - - test_typeArgumentNotMatchingBounds_with() async { - await assertErrorsInCode(r''' -class A {} -class B {} -class G {} -class C extends Object with G{} -''', [ - error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 76, 1), - ]); - } - test_typeParameterSupertypeOfItsBound() async { await assertErrorsInCode(r''' class A { diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart index 8873b0b000f7..e5a012868b95 100644 --- a/pkg/analyzer/test/src/diagnostics/test_all.dart +++ b/pkg/analyzer/test/src/diagnostics/test_all.dart @@ -253,6 +253,8 @@ import 'subtype_of_sealed_class_test.dart' as subtype_of_sealed_class; import 'super_in_extension_test.dart' as super_in_extension; import 'top_level_instance_getter_test.dart' as top_level_instance_getter; import 'top_level_instance_method_test.dart' as top_level_instance_method; +import 'type_argument_not_matching_bounds_test.dart' + as type_argument_not_matching_bounds; import 'type_check_is_not_null_test.dart' as type_check_is_not_null; import 'type_check_is_null_test.dart' as type_check_is_null; import 'unchecked_use_of_nullable_value_test.dart' @@ -464,6 +466,7 @@ main() { super_in_extension.main(); top_level_instance_getter.main(); top_level_instance_method.main(); + type_argument_not_matching_bounds.main(); type_check_is_not_null.main(); type_check_is_null.main(); unchecked_use_of_nullable_value.main(); diff --git a/pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart b/pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart new file mode 100644 index 000000000000..d32a0124b768 --- /dev/null +++ b/pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart @@ -0,0 +1,305 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:analyzer/src/error/codes.dart'; +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../dart/resolution/driver_resolution.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(TypeArgumentNotMatchingBoundsTest); + }); +} + +@reflectiveTest +class TypeArgumentNotMatchingBoundsTest extends DriverResolutionTest { + test_classTypeAlias() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class C {} +class G {} +class D = G with C; +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 69, 1), + ]); + } + + test_const() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G { + const G(); +} +f() { return const G(); } +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 81, 1), + ]); + } + + test_extends() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +class C extends G{} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 64, 1), + ]); + } + + test_extends_regressionInIssue18468Fix() async { + // https://code.google.com/p/dart/issues/detail?id=18628 + await assertErrorsInCode(r''' +class X {} +class Y extends X {} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 48, 1), + ]); + } + + test_fieldFormalParameter() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +class C { + var f; + C(G this.f) {} +} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 71, 1), + ]); + } + + test_functionReturnType() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +G f() { return null; } +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 48, 1), + ]); + } + + test_functionTypeAlias() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +typedef G f(); +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 56, 1), + ]); + } + + test_functionTypedFormalParameter() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +f(G h()) {} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 50, 1), + ]); + } + + test_implements() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +class C implements G{} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 67, 1), + ]); + } + + test_is() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +var b = 1 is G; +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 61, 1), + ]); + } + + test_methodInvocation_localFunction() async { + await assertErrorsInCode(r''' +class Point { + Point(T x, T y); +} + +main() { + Point f(T x, T y) { + return new Point(x, y); + } + print(f('hello', 'world')); +} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 145, 6), + ]); + } + + test_methodInvocation_method() async { + await assertErrorsInCode(r''' +class Point { + Point(T x, T y); +} + +class PointFactory { + Point point(T x, T y) { + return new Point(x, y); + } +} + +f(PointFactory factory) { + print(factory.point('hello', 'world')); +} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 202, 6), + ]); + } + + test_methodInvocation_topLevelFunction() async { + await assertErrorsInCode(r''' +class Point { + Point(T x, T y); +} + +Point f(T x, T y) { + return new Point(x, y); +} + +main() { + print(f('hello', 'world')); +} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 140, 6), + ]); + } + + test_methodReturnType() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +class C { + G m() { return null; } +} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 60, 1), + ]); + } + + test_new() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +f() { return new G(); } +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 65, 1), + ]); + } + + test_new_superTypeOfUpperBound() async { + await assertErrorsInCode(r''' +class A {} +class B extends A {} +class C extends B {} +class G {} +f() { return new G(); } +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 96, 1), + ]); + } + + test_ofFunctionTypeAlias() async { + await assertErrorsInCode(r''' +class A {} +class B {} +typedef F(); +F fff; +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 50, 1), + ]); + } + + test_parameter() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +f(G g) {} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 50, 1), + ]); + } + + test_redirectingConstructor() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class X { + X(int x, int y) {} + factory X.name(int x, int y) = X; +} +''', [ + error(StaticWarningCode.REDIRECT_TO_INVALID_RETURN_TYPE, 99, 4), + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 101, 1), + ]); + } + + test_typeArgumentList() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class C {} +class D {} +C> Var; +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 64, 1), + ]); + } + + test_typeParameter() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class C {} +class G {} +class D> {} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 77, 1), + ]); + } + + test_variableDeclaration() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +G g; +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 48, 1), + ]); + } + + test_with() async { + await assertErrorsInCode(r''' +class A {} +class B {} +class G {} +class C extends Object with G{} +''', [ + error(CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 76, 1), + ]); + } +}