Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1400. Add cascade invocation test and more constants tests #2545

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! An instance constant doesn't exist, but we should check that it isn't magically brought into existence with an extension type.

/// @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);
}
Loading