From abf59764017ab159171fa2d8bc7f84113bb7aa58 Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Tue, 7 Jan 2025 13:47:01 +0200 Subject: [PATCH] Fixes #3030. Add additional test checking non const in a const constructor initializer list --- ...t_a_constant_in_initializer_list_t01.dart} | 6 +-- ...t_a_constant_in_initializer_list_t02.dart} | 24 +++++++--- ...t_a_constant_in_initializer_list_t03.dart} | 19 ++++---- ...ot_a_constant_in_initializer_list_t04.dart | 44 +++++++++++++++++++ 4 files changed, 74 insertions(+), 19 deletions(-) rename Language/Classes/Constructors/Constant_Constructors/{potentially_constant_expression_t01.dart => not_a_constant_in_initializer_list_t01.dart} (95%) rename Language/Classes/Constructors/Constant_Constructors/{potentially_constant_expression_t03.dart => not_a_constant_in_initializer_list_t02.dart} (73%) rename Language/Classes/Constructors/Constant_Constructors/{potentially_constant_expression_t02.dart => not_a_constant_in_initializer_list_t03.dart} (84%) create mode 100644 Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t04.dart diff --git a/Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t01.dart b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t01.dart similarity index 95% rename from Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t01.dart rename to Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t01.dart index 5bf36260f6..8ece7088d1 100644 --- a/Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t01.dart +++ b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t01.dart @@ -11,11 +11,11 @@ /// constant constructor were treated as compile-time constants that were /// guaranteed to evaluate to an integer, boolean or string value as required /// by their immediately enclosing superexpression. +/// /// @description Checks that it is a compile-time error when a constant /// constructor's initializer list contains non-constant list literal. /// @author iefremov - class A { final x; const A(var p) : x = [p]; @@ -25,7 +25,5 @@ class A { } main() { - const A(1); -// ^ -// [analyzer] unspecified + print(A); } diff --git a/Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t03.dart b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t02.dart similarity index 73% rename from Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t03.dart rename to Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t02.dart index 162be562f3..d7c0481e23 100644 --- a/Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t03.dart +++ b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t02.dart @@ -11,21 +11,33 @@ /// constant constructor were treated as compile-time constants that were /// guaranteed to evaluate to an integer, boolean or string value as required /// by their immediately enclosing superexpression. +/// /// @description Checks that it is a compile-time error when a constant -/// constructor's initializer list contains an instance creation expression. +/// constructor's initializer list contains a function call. /// @author iefremov +f1() {} +int f2() => 2; +int get f3 => 3; class A { final x; - const A() : x = new List.from([]); -// ^^^ + const A() : x = f1(); +// ^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + const A.foo() : x = f2(); +// ^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + const A.bar() : x = f3; +// ^^ // [analyzer] unspecified // [cfe] unspecified } main() { - const A(); -// ^ -// [analyzer] unspecified + print(A); } diff --git a/Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t02.dart b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t03.dart similarity index 84% rename from Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t02.dart rename to Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t03.dart index 7750983d14..b06bf83a98 100644 --- a/Language/Classes/Constructors/Constant_Constructors/potentially_constant_expression_t02.dart +++ b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t03.dart @@ -11,23 +11,24 @@ /// constant constructor were treated as compile-time constants that were /// guaranteed to evaluate to an integer, boolean or string value as required /// by their immediately enclosing superexpression. +/// /// @description Checks that it is a compile-time error when a constant -/// constructor's initializer list contains a function call. +/// constructor's initializer list contains an non-constant instance creation +/// expression. /// @author iefremov - -f() {} - class A { + const A(); +} + +class C { final x; - const A() : x = f(); -// ^^^ + const C() : x = new A(); +// ^^^^^^^ // [analyzer] unspecified // [cfe] unspecified } main() { - const A(); -// ^ -// [analyzer] unspecified + print(C); } diff --git a/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t04.dart b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t04.dart new file mode 100644 index 0000000000..4f12ead254 --- /dev/null +++ b/Language/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t04.dart @@ -0,0 +1,44 @@ +// 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 Any expression that appears within the initializer list of a +/// constant constructor must be a potentially constant expression, or a +/// compile-time error occurs. +/// +/// A potentially constant expression is an expression E that would be a valid +/// constant expression if all formal parameters of E's immediately enclosing +/// constant constructor were treated as compile-time constants that were +/// guaranteed to evaluate to an integer, boolean or string value as required +/// by their immediately enclosing superexpression. +/// +/// @description Checks that it is a compile-time error when a constant +/// constructor's initializer list contains a use of a non-constant. +/// @author sgrekhov22@gmail.com +/// @issue 59804 + +class A { + final int id; + const A(this.id); + static A answer = const A(42); +} + +final A zero = const A(0); + +class C { + final A a; + const C(this.a); + const C.fromAnswer() : a = A.answer; +// ^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + const C.fromGlobal() : a = zero; +// ^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(C); +}