Skip to content

Commit

Permalink
dart-lang#1400. Add cascade operator test and more constants tests (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Feb 15, 2024
1 parent c059f5b commit f4e68e6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<int> 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);
}
42 changes: 42 additions & 0 deletions LanguageFeatures/Extension-types/syntax_A08_t05.dart
Original file line number Diff line number Diff line change
@@ -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 <extensionTypeDeclaration> is added to the grammar,
/// along with some rules for elements used in extension type declarations:
///
/// <extensionTypeDeclaration> ::=
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
/// <representationDeclaration> <interfaces>?
/// '{'
/// (<metadata> <extensionTypeMemberDeclaration>)*
/// '}'
///
/// <representationDeclaration> ::=
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
///
/// <identifierOrNew> ::= <identifier> | 'new'
///
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
/// ...
/// 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);
}
51 changes: 51 additions & 0 deletions LanguageFeatures/Extension-types/syntax_A10_t03.dart
Original file line number Diff line number Diff line change
@@ -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 <extensionTypeDeclaration> is added to the grammar,
/// along with some rules for elements used in extension type declarations:
///
/// <extensionTypeDeclaration> ::=
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
/// <representationDeclaration> <interfaces>?
/// '{'
/// (<metadata> <extensionTypeMemberDeclaration>)*
/// '}'
///
/// <representationDeclaration> ::=
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
///
/// <identifierOrNew> ::= <identifier> | 'new'
///
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
/// ...
/// 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<int> 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);
}

0 comments on commit f4e68e6

Please sign in to comment.