Type arguments to a constructor is an error.
Fixes https://github.com/dart-lang/sdk/issues/34159
Change-Id: Ib4cca43f4181f2557560fa45ca41dd059c8b5b06
Reviewed-on: https://dart-review.googlesource.com/71521
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Daniel Hillerström <hillerstrom@google.com>
diff --git a/pkg/analyzer/lib/src/fasta/error_converter.dart b/pkg/analyzer/lib/src/fasta/error_converter.dart
index 4a4f989..aebe4d2 100644
--- a/pkg/analyzer/lib/src/fasta/error_converter.dart
+++ b/pkg/analyzer/lib/src/fasta/error_converter.dart
@@ -630,6 +630,10 @@
errorReporter?.reportErrorForOffset(
ParserErrorCode.TYPEDEF_IN_CLASS, offset, length);
return;
+ case "UNDEFINED_CLASS":
+ errorReporter?.reportErrorForOffset(
+ CompileTimeErrorCode.UNDEFINED_CLASS, offset, length);
+ return;
case "UNDEFINED_GETTER":
errorReporter?.reportErrorForOffset(
StaticTypeWarningCode.UNDEFINED_GETTER, offset, length);
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 bdb31cc..2e438f2 100644
--- a/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart
+++ b/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart
@@ -1449,6 +1449,19 @@
tip: r"""Try removing the return type.""");
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
+const Code<Null> codeConstructorWithTypeArguments =
+ messageConstructorWithTypeArguments;
+
+// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
+const MessageCode messageConstructorWithTypeArguments = const MessageCode(
+ "ConstructorWithTypeArguments",
+ analyzerCode: "UNDEFINED_CLASS",
+ severity: Severity.error,
+ message:
+ r"""A constructor invocation can't have type arguments on the constructor name.""",
+ tip: r"""Try to place the type arguments on the class name.""");
+
+// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeConstructorWithTypeParameters =
messageConstructorWithTypeParameters;
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 d312acf..b6bd338 100644
--- a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
@@ -2830,6 +2830,10 @@
identifier = list[1];
if (prefix is TypeUseGenerator) {
type = prefix;
+ if (typeArguments != null) {
+ addProblem(fasta.messageConstructorWithTypeArguments,
+ identifier.token.charOffset, identifier.name.length);
+ }
} else if (prefix is Generator) {
type = prefix.prefixedLookup(identifier.token);
identifier = null;
diff --git a/pkg/front_end/messages.yaml b/pkg/front_end/messages.yaml
index 98522b3..f1df363 100644
--- a/pkg/front_end/messages.yaml
+++ b/pkg/front_end/messages.yaml
@@ -1160,6 +1160,14 @@
- "class C { factory C<T>() => null; }"
- "class C { factory C.foo<T>() => null; }"
+ConstructorWithTypeArguments:
+ template: "A constructor invocation can't have type arguments on the constructor name."
+ tip: "Try to place the type arguments on the class name."
+ severity: ERROR
+ analyzerCode: UNDEFINED_CLASS
+ script:
+ - "class C<X> { C.foo(); } bar() { new C.foo<int>(); }"
+
ConstructorWithWrongName:
template: "The name of a constructor must match the name of the enclosing class."
severity: ERROR
diff --git a/tests/language_2/constructor_reference_test.dart b/tests/language_2/constructor_reference_test.dart
new file mode 100644
index 0000000..1dae4a2
--- /dev/null
+++ b/tests/language_2/constructor_reference_test.dart
@@ -0,0 +1,41 @@
+// Copyright (c) 2018, 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.
+
+class Foo<X> {
+ const Foo();
+ const Foo.bar();
+ const Foo.baz();
+}
+
+main() {
+ new Foo(); //# 01: ok
+ new Foo.bar(); //# 02: ok
+ new Foo.bar.baz(); //# 03: compile-time error
+ new Foo<int>(); //# 04: ok
+ new Foo<int>.bar(); //# 05: ok
+ new Foo<int>.bar.baz(); //# 06: compile-time error
+ new Foo.bar<int>(); //# 07: compile-time error
+ new Foo.bar<int>.baz(); //# 08: compile-time error
+ new Foo.bar.baz<int>(); //# 09: compile-time error
+
+ const Foo(); //# 11: ok
+ const Foo.bar(); //# 12: ok
+ const Foo.bar.baz(); //# 13: compile-time error
+ const Foo<int>(); //# 14: ok
+ const Foo<int>.bar(); //# 15: ok
+ const Foo<int>.bar.baz(); //# 16: compile-time error
+ const Foo.bar<int>(); //# 17: compile-time error
+ const Foo.bar<int>.baz(); //# 18: compile-time error
+ const Foo.bar.baz<int>(); //# 19: compile-time error
+
+ Foo(); //# 21: ok
+ Foo.bar(); //# 22: ok
+ Foo.bar.baz(); //# 23: compile-time error
+ Foo<int>(); //# 24: ok
+ Foo<int>.bar(); //# 25: ok
+ Foo<int>.bar.baz(); //# 26: compile-time error
+ Foo.bar<int>(); //# 27: compile-time error
+ Foo.bar<int>.baz(); //# 28: compile-time error
+ Foo.bar.baz<int>(); //# 29: compile-time error
+}
diff --git a/tests/language_2/language_2_analyzer.status b/tests/language_2/language_2_analyzer.status
index 432a67f..6805a0a 100644
--- a/tests/language_2/language_2_analyzer.status
+++ b/tests/language_2/language_2_analyzer.status
@@ -23,6 +23,7 @@
conflicting_generic_interfaces_hierarchy_loop_infinite_test: Skip # Crashes or times out
const_cast2_test/01: CompileTimeError
const_cast2_test/none: CompileTimeError
+constructor_reference_test/27: MissingCompileTimeError # Issue 34270
default_implementation2_test: CompileTimeError # Issue 30855
dynamic_prefix_core_test/01: MissingCompileTimeError
emit_const_fields_test: CompileTimeError
diff --git a/tests/language_2/language_2_dartdevc.status b/tests/language_2/language_2_dartdevc.status
index 593a7ce..c4a8c21 100644
--- a/tests/language_2/language_2_dartdevc.status
+++ b/tests/language_2/language_2_dartdevc.status
@@ -36,6 +36,7 @@
const_cast2_test/none: CompileTimeError
const_constructor_mixin3_test/01: MissingCompileTimeError # Issue 33644
const_constructor_mixin_test/01: MissingCompileTimeError # Issue 33644
+constructor_reference_test/27: MissingCompileTimeError
covariance_field_test/03: RuntimeError
covariant_override/tear_off_type_test: RuntimeError # Issue 28395
default_implementation2_test: CompileTimeError # Issue 30855
diff --git a/tests/language_2/language_2_kernel.status b/tests/language_2/language_2_kernel.status
index 3925076..b3f8360 100644
--- a/tests/language_2/language_2_kernel.status
+++ b/tests/language_2/language_2_kernel.status
@@ -209,6 +209,7 @@
const_constructor_mixin_test: CompileTimeError # Issue 33644.
const_types_test/34: MissingCompileTimeError # Issue 31590
const_types_test/39: MissingCompileTimeError # Issue 31590
+constructor_reference_test/27: MissingCompileTimeError # Issue 32972 (parsed as method call)
cyclic_type_variable_test/01: MissingCompileTimeError # Issue 32989 (missing cycle check in bounds)
cyclic_type_variable_test/02: MissingCompileTimeError # Issue 32989 (missing cycle check in bounds)
cyclic_type_variable_test/03: MissingCompileTimeError # Issue 32989 (missing cycle check in bounds)
@@ -268,7 +269,6 @@
mixin_invalid_bound_test/10: MissingCompileTimeError # Issue 33308
mixin_super_bound_test/01: MissingCompileTimeError # Issue 33308
mixin_super_bound_test/02: MissingCompileTimeError # Issue 33308
-named_constructor_test/01: MissingCompileTimeError # Issue 34159
named_parameters_default_eq_test/02: MissingCompileTimeError # Issue 34160
redirecting_factory_default_values_test/01: MissingCompileTimeError # Issue 34160
redirecting_factory_default_values_test/02: MissingCompileTimeError # Issue 34160
@@ -1518,7 +1518,6 @@
method_override6_test/03: MissingCompileTimeError
mixin_illegal_super_use_test: Skip # Issues 24478 and 23773
mixin_illegal_superclass_test: Skip # Issues 24478 and 23773
-named_constructor_test/01: MissingRuntimeError # Fasta bug: Bad compilation of constructor reference.
named_parameters_default_eq_test/none: RuntimeError
nested_generic_closure_test: RuntimeError
no_main_test/01: Skip