#3315. Add more primary constructors tests (#3320)
Add more primary constructors tests
diff --git a/LanguageFeatures/primary-constructors/grammar_A02_t02.dart b/LanguageFeatures/primary-constructors/grammar_A02_t02.dart
index 31f6435..3aa0af7 100644
--- a/LanguageFeatures/primary-constructors/grammar_A02_t02.dart
+++ b/LanguageFeatures/primary-constructors/grammar_A02_t02.dart
@@ -23,8 +23,8 @@
// [cfe] unspecified
}
-extension type ET2() {
- this();
+extension type ET2(int v) {
+ this(final int v);
// ^
// [analyzer] unspecified
// [cfe] unspecified
diff --git a/LanguageFeatures/primary-constructors/grammar_A04_t04.dart b/LanguageFeatures/primary-constructors/grammar_A04_t04.dart
new file mode 100644
index 0000000..5d0e3fa
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/grammar_A04_t04.dart
@@ -0,0 +1,74 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion The keyword `const` can be specified in the class header when it
+/// contains a primary constructor, and in this case `const` can not be
+/// specified in the part of the primary constructor that occurs in the body
+/// (that is, the declaration that starts with this and contains an initializer
+/// list and/or a constructor body, if any). The rationale is that when the
+/// class header contains any parts of a declaring constructor, the class header
+/// must be the location where all parts of the signature of that primary
+/// constructor are specified.
+///
+/// @description Check that it is a compile-time error if the keyword `const` is
+/// specified in the class/extension type/enum header when it doesn't contain a
+/// primary constructor.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+class const C1 {
+// ^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+ this(final int v);
+}
+
+class const C2 {
+// ^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+ this.someName();
+}
+
+extension type const ET1 {
+// ^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+ this(final int v);
+}
+
+extension type const ET2 {
+// ^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+ this.someName(final int v);
+}
+
+enum const E1 {
+// ^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+ e0(1);
+
+ const this(final int v);
+}
+
+enum const E2 {
+// ^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+ e0;
+
+ const this.someName();
+}
+
+main() {
+ print(C1);
+ print(C2);
+ print(ET1);
+ print(ET2);
+ print(E1);
+ print(E2);
+}
diff --git a/LanguageFeatures/primary-constructors/static_processing_A04_t01.dart b/LanguageFeatures/primary-constructors/static_processing_A04_t01.dart
new file mode 100644
index 0000000..13f6f50
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/static_processing_A04_t01.dart
@@ -0,0 +1,64 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion Consider a class, enum, or extension type declaration `D` with a
+/// declaring header constructor, also known as a primary constructor.
+/// ...
+/// If `D` is an extension type, it is a compile-time error if `D`
+/// does not contain a declaring constructor that has exactly one declaring
+/// parameter which is `final`.
+///
+/// @description Check that it is a compile-time error if an extension type does
+/// not contain a declaring constructor that has exactly one declaring parameter
+/// which is `final`.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+extension type const ET1(var int v);
+// ^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+
+extension type ET2.someName(var int v);
+// ^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+
+extension type ET3 {
+ this(var int v);
+// ^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type ET4 {
+ const this.someName(var int v);
+// ^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type ET5 {
+ const this(int v);
+// ^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type ET6 {
+ this(int v);
+// ^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+main() {
+ print(ET1);
+ print(ET2);
+ print(ET3);
+ print(ET4);
+ print(ET5);
+ print(ET6);
+}
diff --git a/LanguageFeatures/primary-constructors/static_processing_A04_t02.dart b/LanguageFeatures/primary-constructors/static_processing_A04_t02.dart
new file mode 100644
index 0000000..6eddade
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/static_processing_A04_t02.dart
@@ -0,0 +1,51 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion Consider a class, enum, or extension type declaration `D` with a
+/// declaring header constructor, also known as a primary constructor.
+/// ...
+/// If `D` is an extension type, it is a compile-time error if `D`
+/// does not contain a declaring constructor that has exactly one declaring
+/// parameter which is `final`.
+///
+/// @description Check that it is not an error if an extension type contains
+/// a declaring constructor that has exactly one optional declaring parameter.
+/// Test optional parameters.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+import '../../Utils/expect.dart';
+
+extension type const ET1({final int v = 0});
+
+extension type const ET2 {
+ this({final int v = 0});
+}
+
+// Representation variable below is `final` by default
+extension type ET3.someName([int v = 0]);
+
+extension type ET4 {
+ this.someName([final int v = 0]);
+}
+
+extension type const ET5._({required int v}); // It is allowed to omit `final` here
+
+extension type const ET6 {
+ this._({required final int v});
+}
+
+main() {
+ Expect.equals(0, ET1().v);
+ Expect.equals(1, ET1(v: 1).v);
+ Expect.equals(0, ET2().v);
+ Expect.equals(2, ET2(v: 2).v);
+ Expect.equals(0, ET3.someName().v);
+ Expect.equals(3, ET3.someName(3).v);
+ Expect.equals(0, ET4.someName().v);
+ Expect.equals(4, ET4.someName(4).v);
+ Expect.equals(5, ET5._(v: 5).v);
+ Expect.equals(6, ET5._(v: 6).v);
+}
diff --git a/LanguageFeatures/primary-constructors/static_processing_A04_t03.dart b/LanguageFeatures/primary-constructors/static_processing_A04_t03.dart
new file mode 100644
index 0000000..3db4fad
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/static_processing_A04_t03.dart
@@ -0,0 +1,42 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion Consider a class, enum, or extension type declaration `D` with a
+/// declaring header constructor, also known as a primary constructor.
+/// ...
+/// If `D` is an extension type, it is a compile-time error if `D`
+/// does not contain a declaring constructor that has exactly one declaring
+/// parameter which is `final`.
+///
+/// @description Check that it is not an error if an extension type contains
+/// a declaring constructor that has exactly one declaring parameter which is
+/// `final`. Test required parameters.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+import '../../Utils/expect.dart';
+
+// Representation variable of a primary (in-header) constructor is `final` by
+// default
+extension type ET1(int v);
+
+extension type const ET2 {
+ this(final int v, int x, [int y = 0]);
+}
+
+extension type const ET3.someName(final int v);
+
+extension type ET4 {
+ const this.someName(final int v, {int x = 0, required int y});
+}
+
+main() {
+ Expect.equals(1, ET1(1).v);
+ Expect.equals(2, const ET2(2, 3, 4).v);
+ Expect.equals(2, const ET2(2, 3).v);
+ Expect.equals(3, const ET3(3).v);
+ Expect.equals(4, const ET4(4, y: 0).v);
+ Expect.equals(4, const ET4(4, x: 1, y: 2).v);
+}
diff --git a/LanguageFeatures/primary-constructors/static_processing_A04_t04.dart b/LanguageFeatures/primary-constructors/static_processing_A04_t04.dart
new file mode 100644
index 0000000..e79bb58
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/static_processing_A04_t04.dart
@@ -0,0 +1,45 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion Consider a class, enum, or extension type declaration `D` with a
+/// declaring header constructor, also known as a primary constructor.
+/// ...
+/// If `D` is an extension type, it is a compile-time error if `D`
+/// does not contain a declaring constructor that has exactly one declaring
+/// parameter which is `final`.
+///
+/// @description Check that it is not an error if an extension type contains
+/// a declaring in-body constructor that has one optional declaring parameter
+/// and also non-declaring ones.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+import '../../Utils/expect.dart';
+
+extension type ET1 {
+ const this(final int x, int y, [String z = ""]);
+}
+
+extension type ET2 {
+ const this.someName(int x, {String y = "", final int z = 0});
+}
+
+extension type ET3 {
+ const this(int x, [String y = "", final int z = 0]);
+}
+
+extension type ET4 {
+ this.someName(int x, {required final String z});
+}
+
+main() {
+ Expect.equals("3", ET1(1, 2, "3").z);
+ Expect.equals("", ET1(1, 2).z);
+ Expect.equals(3, ET2.someName(1, "2", 3).z);
+ Expect.equals(0, ET2.someName(1, "2").z);
+ Expect.equals(0, ET3(1).z);
+ Expect.equals(3, ET3(1, "2", 3).z);
+ Expect.equals("2", ET4.someName(1, "2").z);
+}
diff --git a/LanguageFeatures/primary-constructors/static_processing_A05_t01.dart b/LanguageFeatures/primary-constructors/static_processing_A05_t01.dart
new file mode 100644
index 0000000..2103ce6
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/static_processing_A05_t01.dart
@@ -0,0 +1,70 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion A compile-time error occurs if the name of the primary
+/// constructor is the same as the name of a constructor (declaring or not) in
+/// the body.
+///
+/// @description Check that it is a compile-time error if the name of the
+/// primary constructor is the same as the name of some constructor declared in
+/// the body. Test classes.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+class C1(var int v) {
+ C1.someName(this.v);
+ C1(int v) : this.someName(v);
+//^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+class C2(int v1) {
+ this(var int v2);
+//^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+class const C3.someName(final int v) {
+ const C3(this.v);
+ const C3.someName(int v) : this(v);
+// ^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+class C4.someName(int v1) {
+ this.someName(var int v2);
+//^^^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+class C5(var int v) {
+ C5.someName(int v) : this(v);
+ factory C5(int v) = C5.someName;
+// ^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+class C6 {
+ this.someName(final int v);
+ C6(this.v);
+ factory C6.someName(int v) => C6(v);
+// ^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+main() {
+ print(C1);
+ print(C2);
+ print(C3);
+ print(C4);
+ print(C5);
+ print(C6);
+}
diff --git a/LanguageFeatures/primary-constructors/static_processing_A05_t02.dart b/LanguageFeatures/primary-constructors/static_processing_A05_t02.dart
new file mode 100644
index 0000000..5d49d82
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/static_processing_A05_t02.dart
@@ -0,0 +1,70 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion A compile-time error occurs if the name of the primary
+/// constructor is the same as the name of a constructor (declaring or not) in
+/// the body.
+///
+/// @description Check that it is a compile-time error if the name of the
+/// primary constructor is the same as the name of some constructor declared in
+/// the body. Test extension types.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+extension type ET1(int v) {
+ ET1(this.v);
+//^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type ET2 {
+ this(final int v);
+ ET2(this.v);
+//^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type const ET3.someName(final int v) {
+ const ET3.someName(this.v);
+// ^^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type ET4 {
+ this.someName(final int v);
+ ET4.someName(this.v);
+//^^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type ET5(final int v) {
+ ET5.someName(int v) : this(v);
+ factory ET5(int v) = ET5.someName;
+// ^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+extension type ET6 {
+ this.someName(final int v);
+ ET6(this.v);
+ factory ET6.someName(int v) => ET6(v);
+// ^^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+main() {
+ print(ET1);
+ print(ET2);
+ print(ET3);
+ print(ET4);
+ print(ET5);
+ print(ET6);
+}
diff --git a/LanguageFeatures/primary-constructors/static_processing_A05_t03.dart b/LanguageFeatures/primary-constructors/static_processing_A05_t03.dart
new file mode 100644
index 0000000..aae1c01
--- /dev/null
+++ b/LanguageFeatures/primary-constructors/static_processing_A05_t03.dart
@@ -0,0 +1,56 @@
+// Copyright (c) 2025, 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.
+
+/// @assertion A compile-time error occurs if the name of the primary
+/// constructor is the same as the name of a constructor (declaring or not) in
+/// the body.
+///
+/// @description Check that it is a compile-time error if the name of the
+/// primary constructor is the same as the name of some constructor declared in
+/// the body. Test enums.
+/// @author sgrekhov22@gmail.com
+
+// TODO (sgrekhov) Add `declaring-constructors` experimental flag
+
+enum const E1(final int v) {
+ e0(1);
+
+ const E1.someName(this.v);
+ const E1(int v) : E1.someName(v);
+// ^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+enum const E2(final int v) {
+ e0(2);
+ const this(final int v);
+// ^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+enum const E3.someName(final int v) {
+ e0(3);
+ const E3.someName(this.v);
+// ^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+enum const E4.someName(final int v) {
+ e0(4);
+ const E4(this.v);
+ const E4.someName(int v) : this(v);
+// ^^^^^^^^^^^
+// [analyzer] unspecified
+// [cfe] unspecified
+}
+
+main() {
+ print(E1);
+ print(E2);
+ print(E3);
+ print(E4);
+}