Skip to content
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
75 changes: 75 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A04_t17.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 We introduce grammar productions of the form:
/// ```
/// <postfixExpression> ::= ... -- all current productions
/// | <staticMemberShorthand> -- added production
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthandValue> -- No selectors, no `.new`.
///
/// <staticMemberShorthand> ::= <staticMemberShorthandHead> <selector>*
///
/// <staticMemberShorthandHead> ::=
/// <staticMemberShorthandValue>
/// | '.' 'new' -- shorthand unnamed constructor
///
/// <staticMemberShorthandValue> ::= -- something that can potentially create a value.
/// | '.' <identifier> -- shorthand for qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand for constant object creation
/// ```
///
/// @description Checks that the static access shorthand syntax can be used with
/// the `[]` operator.
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
static C zero = C(0);
int index;
List<int> values = [1, 2, 3];

C(this.index);
int operator [](C ind) => values[ind.index];
}

mixin M {
static M two = MO(2);
List<int> values = [1, 2, 3];

int operator [](M ind) => values[(ind as MO).index];
}
class MO with M {
int index;
MO(this.index);
}

enum E {
e0(0), e1(1), e2(2);
final int value;
static E two = E.e2;
const E(this.value);

int operator [](E ind) => values[ind.value].value;
}

extension type ET(int v) implements int {
static ET two = ET(2);
static List<int> values = [1, 2, 3];

int operator [](ET index) => ET.values[index.v];
}

main() {
Expect.equals(1, C(0)[.zero]);
Expect.equals(3, (MO(0) as M)[.two]);
Expect.equals(2, E.e1[.two]);
Expect.equals(1, E.e1[.e1]);
Expect.equals(3, ET(0)[.two]);

}
94 changes: 94 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A04_t18.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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 We introduce grammar productions of the form:
/// ```
/// <postfixExpression> ::= ... -- all current productions
/// | <staticMemberShorthand> -- added production
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthandValue> -- No selectors, no `.new`.
///
/// <staticMemberShorthand> ::= <staticMemberShorthandHead> <selector>*
///
/// <staticMemberShorthandHead> ::=
/// <staticMemberShorthandValue>
/// | '.' 'new' -- shorthand unnamed constructor
///
/// <staticMemberShorthandValue> ::= -- something that can potentially create a value.
/// | '.' <identifier> -- shorthand for qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand for constant object creation
/// ```
///
/// @description Checks that the static access shorthand syntax can be used with
/// the `[]=` operator. Test the first operand.
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
static C zero = C(0);
int index;
List<int> values = [1, 2, 3];

C(this.index);
void operator []=(C ind, int value) {
values[ind.index] = value;
}
}

mixin M {
static M two = MO(2);
List<int> values = [1, 2, 3];

void operator []=(M ind, int value) {
values[(ind as MO).index] = value;
}
}
class MO with M {
int index;
MO(this.index);
}

enum E {
e0(0), e1(1), e2(2);
final int value;
static List<int> vals = [1, 2, 3];
static E two = E.e2;
const E(this.value);

void operator []=(E ind, int value) {
E.vals[ind.value] = value;
}
}

extension type ET(int v) implements int {
static ET two = ET(2);
static List<int> values = [1, 2, 3];

void operator []=(ET index, int value) {
ET.values[index.v] = value;
}
}

main() {
C c = C(0);
c[.zero] = 42;
Expect.equals(42, c.values[0]);

M m = MO(0);
m[.two] = 42;
Expect.equals(42, m.values[2]);

E.e0[.two] = 42;
Expect.equals(42, E.vals[2]);
E.e0[.e1] = 42;
Expect.equals(42, E.vals[1]);

ET et = ET(0);
et[.two] = 42;
Expect.equals(42, ET.values[2]);
}
91 changes: 91 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A04_t19.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 We introduce grammar productions of the form:
/// ```
/// <postfixExpression> ::= ... -- all current productions
/// | <staticMemberShorthand> -- added production
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthandValue> -- No selectors, no `.new`.
///
/// <staticMemberShorthand> ::= <staticMemberShorthandHead> <selector>*
///
/// <staticMemberShorthandHead> ::=
/// <staticMemberShorthandValue>
/// | '.' 'new' -- shorthand unnamed constructor
///
/// <staticMemberShorthandValue> ::= -- something that can potentially create a value.
/// | '.' <identifier> -- shorthand for qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand for constant object creation
/// ```
///
/// @description Checks that the static access shorthand syntax can be used with
/// the `[]=` operator. Test the second operand.
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
static C zero = C(0);
int v;
List<int> values = [1, 2, 3];

C(this.v);
void operator []=(int index, C value) {
values[index] = value.v;
}
}

mixin M {
static M two = MO(2);
List<int> values = [1, 2, 3];

void operator []=(int index, M value) {
values[index] = (value as MO).v;
}
}
class MO with M {
int v;
MO(this.v);
}

enum E {
e0(0), e1(1), e2(2);
final int v;
static List<int> vals = [1, 2, 3];
static E two = E.e2;
const E(this.v);

void operator []=(int index, E value) {
E.vals[index] = value.v;
}
}

extension type ET(int v) implements int {
static ET two = ET(2);
static List<int> values = [1, 2, 3];

void operator []=(int index, ET value) {
ET.values[index] = value.v;
}
}

main() {
C c = C(0);
c[1] = .zero;
Expect.equals(0, c.values[1]);
M m = MO(0);
m[0] = .two;
Expect.equals(2, m.values[0]);
E.e0[0] = .two;
Expect.equals(2, E.vals[0]);
E.e0[0] = .e1;
Expect.equals(1, E.vals[0]);
ET et = ET(0);
et[0] = .two;
Expect.equals(2, ET.values[0]);
}
76 changes: 76 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A06_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 We introduce grammar productions of the form:
/// ```
/// <postfixExpression> ::= ... -- all current productions
/// | <staticMemberShorthand> -- added production
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthandValue> -- No selectors, no `.new`.
///
/// <staticMemberShorthand> ::= <staticMemberShorthandHead> <selector>*
///
/// <staticMemberShorthandHead> ::=
/// <staticMemberShorthandValue>
/// | '.' 'new' -- shorthand unnamed constructor
///
/// <staticMemberShorthandValue> ::= -- something that can potentially create a value.
/// | '.' <identifier> -- shorthand for qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand for constant object creation
/// ```
///
/// @description Checks that it is a compile-time error to use the static access
/// shorthand syntax with the `as` operator.
/// @author sgrekhov22@gmail.com

// SharedOptions=--enable-experiment=enum-shorthands

class C {
static C one = C(1);
int v;
C(this.v);
}

mixin M on C {
static M two = MC(2);
}
class MC = C with M;

enum E {
e1;

static E one = E.e1;
}

extension type ET(int v) implements int {
static ET one = ET(1);
}

main() {
C c = .one as C; // ignore: unnecessary_cast
// ^
// [analyzer] unspecified
// [cfe] unspecified

M m = .two as M; // ignore: unnecessary_cast
// ^
// [analyzer] unspecified
// [cfe] unspecified

E e1 = .one as E; // ignore: unnecessary_cast
// ^
// [analyzer] unspecified
// [cfe] unspecified

E e2 = .e1 as E; // ignore: unnecessary_cast
// ^
// [analyzer] unspecified
// [cfe] unspecified

ET et = .one as ET; // ignore: unnecessary_cast
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Loading
Loading