From 321a4ec048008be951c93eff86f024dfd991f2c2 Mon Sep 17 00:00:00 2001 From: Sigmund Cherem Date: Fri, 15 Sep 2017 20:37:41 +0000 Subject: [PATCH] Check whether type-arguments and type-parameters length match from the body builder. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: Change-Id: I395365393c9032ff179d675f42ca56b7981531e0 Reviewed-on: https://dart-review.googlesource.com/5620 Reviewed-by: Peter von der Ahé --- .../lib/src/fasta/fasta_codes_generated.dart | 22 ++++++++++++++++ .../lib/src/fasta/kernel/body_builder.dart | 7 ++++++ .../lib/src/fasta/kernel/fasta_accessors.dart | 24 ++++++++++++++++++ pkg/front_end/messages.yaml | 4 +++ pkg/front_end/testcases/compile.status | 1 - .../regress/issue_29981.dart.direct.expect | 13 ++++++++++ .../regress/issue_29981.dart.strong.expect | 13 ++++++++++ pkg/front_end/testcases/strong.status | 1 - tests/language/language_dart2js.status | 25 +++---------------- tests/language_2/language_2_dart2js.status | 2 -- 10 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 pkg/front_end/testcases/regress/issue_29981.dart.direct.expect create mode 100644 pkg/front_end/testcases/regress/issue_29981.dart.strong.expect diff --git a/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart b/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart index e0e03943578..1d52b387c15 100644 --- a/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart +++ b/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart @@ -2909,6 +2909,28 @@ const MessageCode messageTypeAfterVar = const MessageCode("TypeAfterVar", message: r"""Can't have both a type and 'var'.""", tip: r"""Try removing 'var.'"""); +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +const Template + templateTypeArgumentMismatch = + const Template( + messageTemplate: r"""'#name' expects #string type arguments.""", + withArguments: _withArgumentsTypeArgumentMismatch); + +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +const Code + codeTypeArgumentMismatch = + const Code( + "TypeArgumentMismatch", + templateTypeArgumentMismatch, +); + +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +Message _withArgumentsTypeArgumentMismatch(String name, String string) { + return new Message(codeTypeArgumentMismatch, + message: """'$name' expects $string type arguments.""", + arguments: {'name': name, 'string': string}); +} + // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. const Template templateTypeArgumentsOnTypeVariable = diff --git a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart index 4fb67d51b46..b56cf0b76b1 100644 --- a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart +++ b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart @@ -1016,6 +1016,13 @@ class BodyBuilder extends ScopeListener implements BuilderHelper { charOffset); } + @override + void warnTypeArgumentsMismatch(String name, int expected, int charOffset) { + warning( + fasta.templateTypeArgumentMismatch.withArguments(name, '${expected}'), + charOffset); + } + @override Member lookupSuperMember(Name name, {bool isSetter: false}) { Class superclass = classBuilder.cls.superclass; diff --git a/pkg/front_end/lib/src/fasta/kernel/fasta_accessors.dart b/pkg/front_end/lib/src/fasta/kernel/fasta_accessors.dart index 498a7036524..19b0d08fe75 100644 --- a/pkg/front_end/lib/src/fasta/kernel/fasta_accessors.dart +++ b/pkg/front_end/lib/src/fasta/kernel/fasta_accessors.dart @@ -37,6 +37,7 @@ import 'frontend_accessors.dart' show Accessor; import 'kernel_builder.dart' show Builder, + FunctionTypeAliasBuilder, KernelClassBuilder, KernelInvalidTypeBuilder, LibraryBuilder, @@ -130,6 +131,8 @@ abstract class BuilderHelper { void warnUnresolvedSuperSet(Name name, int charOffset); void warnUnresolvedSuperMethod(Name name, int charOffset); + + void warnTypeArgumentsMismatch(String name, int expected, int charOffset); } abstract class FastaAccessor implements Accessor { @@ -1025,6 +1028,27 @@ class TypeDeclarationAccessor extends ReadOnlyAccessor { DartType buildType(List arguments, {bool nonInstanceAccessIsError: false}) { + if (arguments != null) { + int expected = 0; + if (declaration is KernelClassBuilder) { + expected = declaration.target.typeParameters.length; + } else if (declaration is FunctionTypeAliasBuilder) { + expected = declaration.target.typeParameters.length; + } else { + return unhandled( + "${declaration.runtimeType}", + "TypeDeclarationAccessor.buildType", + offsetForToken(token), + helper.uri); + } + if (arguments.length != expected) { + helper.warnTypeArgumentsMismatch( + declaration.name, expected, offsetForToken(token)); + // TODO(sigmund): include more details in InvalidType about why the type + // is invalid (see #29840). + return const InvalidType(); + } + } DartType type = declaration.buildTypesWithBuiltArguments(helper.library, arguments); if (type is TypeParameterType) { diff --git a/pkg/front_end/messages.yaml b/pkg/front_end/messages.yaml index 6bae1ead1c1..3d018eb45a9 100644 --- a/pkg/front_end/messages.yaml +++ b/pkg/front_end/messages.yaml @@ -541,6 +541,10 @@ ListLiteralTooManyTypeArguments: ListLiteralTypeArgumentMismatch: template: "Map literal requires two type arguments." +TypeArgumentMismatch: + # TODO(sigmund): #string should be a number instead. + template: "'#name' expects #string type arguments." + NotAType: template: "'#name' isn't a type." diff --git a/pkg/front_end/testcases/compile.status b/pkg/front_end/testcases/compile.status index 2e3ffe6ac03..fe95d033321 100644 --- a/pkg/front_end/testcases/compile.status +++ b/pkg/front_end/testcases/compile.status @@ -96,7 +96,6 @@ regress/issue_29944: Crash # Issue 29944. regress/issue_29975: Fail # Issue 29975. regress/issue_29976: RuntimeError regress/issue_29977: Crash # Issue 29977. -regress/issue_29981: Crash # Issue 29981. regress/issue_29982: RuntimeError regress/issue_29983: Crash # Issue 29983. regress/issue_29984: Crash # Issue 29984. diff --git a/pkg/front_end/testcases/regress/issue_29981.dart.direct.expect b/pkg/front_end/testcases/regress/issue_29981.dart.direct.expect new file mode 100644 index 00000000000..bfc03c48734 --- /dev/null +++ b/pkg/front_end/testcases/regress/issue_29981.dart.direct.expect @@ -0,0 +1,13 @@ +library; +import self as self; +import "dart:core" as core; + +class C extends core::Object { + field self::C field = null; + default constructor •() → void + : super core::Object::•() + ; +} +static method main() → dynamic { + core::print(new self::C::•()); +} diff --git a/pkg/front_end/testcases/regress/issue_29981.dart.strong.expect b/pkg/front_end/testcases/regress/issue_29981.dart.strong.expect new file mode 100644 index 00000000000..bfc03c48734 --- /dev/null +++ b/pkg/front_end/testcases/regress/issue_29981.dart.strong.expect @@ -0,0 +1,13 @@ +library; +import self as self; +import "dart:core" as core; + +class C extends core::Object { + field self::C field = null; + default constructor •() → void + : super core::Object::•() + ; +} +static method main() → dynamic { + core::print(new self::C::•()); +} diff --git a/pkg/front_end/testcases/strong.status b/pkg/front_end/testcases/strong.status index d63789e3383..1100b8a77cf 100644 --- a/pkg/front_end/testcases/strong.status +++ b/pkg/front_end/testcases/strong.status @@ -156,7 +156,6 @@ regress/issue_29944: Crash # Issue 29944. regress/issue_29975: Fail # Issue 29975. regress/issue_29976: RuntimeError # Issue 29976. regress/issue_29977: Crash # Issue 29977. -regress/issue_29981: Crash # Issue 29981. regress/issue_29982: RuntimeError # Issue 29982. regress/issue_29983: Crash # Issue 29983. regress/issue_29984: Crash # Issue 29984. diff --git a/tests/language/language_dart2js.status b/tests/language/language_dart2js.status index b2218064c70..4cd2d65612f 100644 --- a/tests/language/language_dart2js.status +++ b/tests/language/language_dart2js.status @@ -647,7 +647,6 @@ for_variable_capture_test: RuntimeError full_stacktrace1_test: RuntimeError full_stacktrace2_test: RuntimeError full_stacktrace3_test: RuntimeError -function_malformed_result_type_test: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. function_subtype_cast2_test: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(Class.test#)) for j:closure_call(Class_test_closure.call). function_subtype_cast3_test: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(Class.test#local)) for j:closure_call(Class_test_closure.call). function_subtype_closure0_test: Crash # NoSuchMethodError: The method 'hasSubclass' was called on null. @@ -704,12 +703,8 @@ main_test/42: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Faile main_test/43: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. main_test/44: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. main_test/45: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. -malformed_test/01: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -malformed_test/02: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -malformed_test/03: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -malformed_test/04: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -malformed_test/06: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -malformed_test/none: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. +malformed_test/06: CompileTimeError +malformed_test/none: Crash # Internal Error: Non-empty instruction stack many_named_arguments_test: RuntimeError map_literal4_test: RuntimeError method_name_test: CompileTimeError @@ -968,22 +963,13 @@ syncstar_less_than_test: RuntimeError syncstar_yield_test/copyParameters: RuntimeError syncstar_yield_test/none: RuntimeError syncstar_yieldstar_test: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. -syntax_test/02: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/03: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/27: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. syntax_test/28: Crash # type 'KernelConstructorBuilder' is not a subtype of type 'KernelProcedureBuilder' of 'method' where syntax_test/29: Crash # type 'KernelConstructorBuilder' is not a subtype of type 'KernelProcedureBuilder' of 'method' where syntax_test/30: Crash # type 'KernelConstructorBuilder' is not a subtype of type 'KernelProcedureBuilder' of 'method' where syntax_test/31: Crash # type 'KernelConstructorBuilder' is not a subtype of type 'KernelProcedureBuilder' of 'method' where syntax_test/32: Crash # type 'KernelConstructorBuilder' is not a subtype of type 'KernelProcedureBuilder' of 'method' where syntax_test/33: Crash # type 'KernelConstructorBuilder' is not a subtype of type 'KernelProcedureBuilder' of 'method' where -syntax_test/49: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/54: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/59: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/60: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/61: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/62: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -syntax_test/none: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. +syntax_test/none: CompileTimeError temp_mangling_test: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. top_level_getter_no_setter1_test/01: RuntimeError top_level_getter_no_setter2_test/01: RuntimeError @@ -997,7 +983,6 @@ type_parameter_test/04: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.da type_parameter_test/05: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. type_parameter_test/06: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. type_parameter_test/none: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. -type_variable_bounds_test/10: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. type_variable_closure2_test: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(C.list#)) for j:closure_call(C_list_closure.call). type_variable_closure3_test: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(C.a#)) for j:closure_call(C_a_closure.call). type_variable_closure4_test: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(C.map#)) for j:closure_call(C_map_closure.call). @@ -1009,8 +994,6 @@ type_variable_conflict2_test/09: RuntimeError type_variable_field_initializer_closure2_test: Crash # Assertion failure: Runtime type information not available for type_variable_local(A.T) in j:closure_call(A_closure.call). type_variable_field_initializer_closure_test: Crash # Assertion failure: Runtime type information not available for type_variable_local(A.T) in j:closure_call(A_closure.call). type_variable_function_type_test: RuntimeError -wrong_number_type_arguments_test/00: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -wrong_number_type_arguments_test/02: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. yieldstar_pause_test: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. [ $compiler == dart2js && $dart2js_with_kernel && $minified ] @@ -1256,7 +1239,7 @@ main_test/43: Crash # Assertion failure: Cannot find value Instance of 'ThisLoca main_test/44: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(_LinkedCustomHashSet.#x), local(_LinkedCustomHashSet.#)) for j:closure_call(_LinkedCustomHashSet__LinkedCustomHashSet_closure.call). main_test/45: Crash # Assertion failure: Cannot find value Instance of 'ThisLocal' in (local(_LinkedCustomHashSet.#x), local(_LinkedCustomHashSet.#)) for j:closure_call(_LinkedCustomHashSet__LinkedCustomHashSet_closure.call). malformed_test/06: CompileTimeError -malformed_test/none: RuntimeError +malformed_test/none: Crash # Internal Error: Non-empty instruction stack many_named_arguments_test: RuntimeError map_literal4_test: RuntimeError method_name_test: CompileTimeError diff --git a/tests/language_2/language_2_dart2js.status b/tests/language_2/language_2_dart2js.status index fa3bd7f30f0..1930ae6df5f 100644 --- a/tests/language_2/language_2_dart2js.status +++ b/tests/language_2/language_2_dart2js.status @@ -751,8 +751,6 @@ named_parameters_with_dollars_test: RuntimeError regress_30339_test: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. typevariable_substitution2_test/02: RuntimeError unused_overridden_async_test: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. -wrong_number_type_arguments_test/00: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. -wrong_number_type_arguments_test/02: Crash # 'package:front_end/src/fasta/kernel/kernel_class_builder.dart': Failed assertion: line 83 pos 12: 'arguments == null || cls.typeParameters.length == arguments.length': is not true. yieldstar_pause_test: Crash # 'file:*/pkg/compiler/lib/src/ssa/type_builder.dart': Failed assertion: line 141 pos 12: 'member.isInstanceMember': is not true. [ $compiler == dart2js && $dart2js_with_kernel && $minified ]