From 330ed8a5eec6e7c791cde07f6364bceba273ca91 Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Wed, 14 Feb 2024 13:34:52 +0200 Subject: [PATCH] #1400. Add cascade operator test and more constants tests --- ...amic_semantics_extension_type_A05_t04.dart | 33 ++++++++++++ .../Extension-types/syntax_A08_t05.dart | 42 +++++++++++++++ .../Extension-types/syntax_A10_t03.dart | 51 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 LanguageFeatures/Extension-types/dynamic_semantics_extension_type_A05_t04.dart create mode 100644 LanguageFeatures/Extension-types/syntax_A08_t05.dart create mode 100644 LanguageFeatures/Extension-types/syntax_A10_t03.dart diff --git a/LanguageFeatures/Extension-types/dynamic_semantics_extension_type_A05_t04.dart b/LanguageFeatures/Extension-types/dynamic_semantics_extension_type_A05_t04.dart new file mode 100644 index 0000000000..bcef497f3c --- /dev/null +++ b/LanguageFeatures/Extension-types/dynamic_semantics_extension_type_A05_t04.dart @@ -0,0 +1,33 @@ +// Copyright (c) 2023, 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 an extension type declaration DV named Name with +/// representation name id and representation type R. Invocation of a +/// non-redirecting generative extension type constructor proceeds as follows: +/// A fresh, non-late, final variable v is created. An initializing formal +/// this.id has the side-effect that it initializes v to the actual argument +/// passed to this formal. An initializer list element of the form id = e or +/// this.id = e is evaluated by evaluating e to an object o and binding v to o. +/// During the execution of the constructor body, this and id are bound to the +/// value of v. The value of the instance creation expression that gave rise to +/// this constructor execution is the value of this. +/// +/// @description Check evaluation of an initializer list with a cascade +/// expressions +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import '../../Utils/expect.dart'; + +extension type ET1(List id) { + ET1.pair(int v1, int v2) : this.id = [] + ..add(v1) + ..add(v2); +} + +main() { + var et = ET1.pair(1, 2); + Expect.listEquals([1, 2], et); +} diff --git a/LanguageFeatures/Extension-types/syntax_A08_t05.dart b/LanguageFeatures/Extension-types/syntax_A08_t05.dart new file mode 100644 index 0000000000..9ffc5fa9a7 --- /dev/null +++ b/LanguageFeatures/Extension-types/syntax_A08_t05.dart @@ -0,0 +1,42 @@ +// Copyright (c) 2023, 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 rule for is added to the grammar, +/// along with some rules for elements used in extension type declarations: +/// +/// ::= +/// 'extension' 'type' 'const'? ? +/// ? +/// '{' +/// ( )* +/// '}' +/// +/// ::= +/// ('.' )? '(' ')' +/// +/// ::= | 'new' +/// +/// ::= +/// ... +/// Some errors can be detected immediately from the syntax: +/// +/// A compile-time error occurs if the extension type declaration declares any +/// instance variables, unless they are external +/// +/// @description Checks that it is a compile-time error if an extension type +/// declaration declares a non-static constant. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +extension type ET(int _) { + const int x = 0; +//^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(ET); +} diff --git a/LanguageFeatures/Extension-types/syntax_A10_t03.dart b/LanguageFeatures/Extension-types/syntax_A10_t03.dart new file mode 100644 index 0000000000..ccc85cd9bb --- /dev/null +++ b/LanguageFeatures/Extension-types/syntax_A10_t03.dart @@ -0,0 +1,51 @@ +// Copyright (c) 2023, 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 rule for is added to the grammar, +/// along with some rules for elements used in extension type declarations: +/// +/// ::= +/// 'extension' 'type' 'const'? ? +/// ? +/// '{' +/// ( )* +/// '}' +/// +/// ::= +/// ('.' )? '(' ')' +/// +/// ::= | 'new' +/// +/// ::= +/// ... +/// There are no special rules for static members in extension types. They can +/// be declared and called or torn off as usual, e.g., +/// AnExtensionType.myStaticMethod(42) +/// +/// @description Checks that extension types may have static final and constant +/// members. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; + +extension type ET1(int id) { + static final int answer = 42; + static const zero = 0; + + const ET1.named() : id = zero; +} + +extension type ET2(List id) { + const ET2.named() : id = const [ET1.zero]; +} + +main() { + Expect.equals(42, ET1.answer); + Expect.identical(0, ET1.zero); + Expect.listEquals([0], ET2.named().id); + Expect.identical(const [0], const ET2.named().id); + Expect.identical(const [0], ET2.named().id); +}