Skip to content

Commit e4521ee

Browse files
authored
Fixes #2235. Add missing operator tests (#2239)
Note that one test has an intentional syntax error (static .. operator).
1 parent 2703b51 commit e4521ee

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion It is a compile-time error to declare an optional parameter in an
6+
/// operator.
7+
///
8+
/// @description Checks that a compile-time error is produced if a user-defined
9+
/// operator with arity 1 specifies an optional positional parameter.
10+
/// @author sgrekhov22@gmail.com
11+
12+
class C {
13+
int operator +([int other]) => 42;
14+
// ^^^^^^^^^
15+
// [analyzer] unspecified
16+
// [cfe] unspecified
17+
18+
int operator -([int other = 0]) => 42;
19+
// ^^^^^^^^^^^^^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
23+
int operator *(int other, [int other2 = 0]) => 42;
24+
// ^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
29+
mixin M {
30+
int operator +([int other]) => 42;
31+
// ^^^^^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
35+
int operator -([int other = 0]) => 42;
36+
// ^^^^^^^^^^^^^
37+
// [analyzer] unspecified
38+
// [cfe] unspecified
39+
40+
int operator *(int other, [int other2 = 0]) => 42;
41+
// ^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
}
45+
46+
enum E {
47+
e1, e2;
48+
49+
int operator +([int other]) => 42;
50+
// ^^^^^^^^^
51+
// [analyzer] unspecified
52+
// [cfe] unspecified
53+
54+
int operator -([int other = 0]) => 42;
55+
// ^^^^^^^^^^^^^
56+
// [analyzer] unspecified
57+
// [cfe] unspecified
58+
59+
int operator *(int other, [int other2 = 0]) => 42;
60+
// ^
61+
// [analyzer] unspecified
62+
// [cfe] unspecified
63+
}
64+
65+
main() {
66+
print(C);
67+
print(M);
68+
print(E);
69+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion It is a compile-time error to declare an optional parameter in an
6+
/// operator.
7+
///
8+
/// @description Checks that a compile-time error is produced if a user-defined
9+
/// operator with arity 1 specifies a named parameter.
10+
/// @author sgrekhov22@gmail.com
11+
/// @issue 53332
12+
13+
class C {
14+
int operator +({int other}) => 42;
15+
// ^^^^^^^^^
16+
// [analyzer] unspecified
17+
// [cfe] unspecified
18+
19+
int operator -({int other = 0}) => 42;
20+
// ^^^^^^^^^^^^^
21+
// [analyzer] unspecified
22+
// [cfe] unspecified
23+
24+
int operator *({required int other}) => 42;
25+
// ^^^^^^^^^^^^^^^^^^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}
29+
30+
mixin M {
31+
int operator +({int other}) => 42;
32+
// ^^^^^^^^^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
36+
int operator -({int other = 0}) => 42;
37+
// ^^^^^^^^^^^^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
41+
int operator *({required int other}) => 42;
42+
// ^^^^^^^^^^^^^^^^^^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
}
46+
47+
enum E {
48+
e1, e2;
49+
50+
int operator +({int other}) => 42;
51+
// ^^^^^^^^^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
55+
int operator -({int other = 0}) => 42;
56+
// ^^^^^^^^^^^^^
57+
// [analyzer] unspecified
58+
// [cfe] unspecified
59+
60+
int operator *({required int other}) => 42;
61+
// ^^^^^^^^^^^^^^^^^^
62+
// [analyzer] unspecified
63+
// [cfe] unspecified
64+
}
65+
66+
main() {
67+
print(C);
68+
print(M);
69+
print(E);
70+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion
6+
/// ⟨operatorSignature⟩ ::=
7+
/// ⟨type⟩? operator ⟨operator⟩ ⟨formalParameterList⟩
8+
/// ⟨operator⟩ ::= ‘~’
9+
/// | ⟨binaryOperator⟩
10+
/// | ‘[]
11+
/// | ‘[]=’
12+
/// ⟨binaryOperator⟩ ::= ⟨multiplicativeOperator⟩
13+
/// | ⟨additiveOperator⟩
14+
/// | ⟨shiftOperator⟩
15+
/// | ⟨relationalOperator⟩
16+
/// | ‘==’
17+
/// | ⟨bitwiseOperator⟩
18+
///
19+
/// @description Checks that it is a compile-time error to declare a static
20+
/// user-defined operator
21+
/// @author sgrekhov22@gmail.com
22+
23+
class C {
24+
static int operator +(int other) => 42;
25+
//^^^^^^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}
29+
30+
mixin M {
31+
static int operator +(int other) => 42;
32+
//^^^^^^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
}
36+
37+
enum E {
38+
e1, e2;
39+
40+
static int operator +(int other) => 42;
41+
//^^^^^^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
}
45+
46+
main() {
47+
print(C);
48+
print(M);
49+
print(E);
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion
6+
/// ⟨operatorSignature⟩ ::=
7+
/// ⟨type⟩? operator ⟨operator⟩ ⟨formalParameterList⟩
8+
/// ⟨operator⟩ ::= ‘~’
9+
/// | ⟨binaryOperator⟩
10+
/// | ‘[]
11+
/// | ‘[]=’
12+
/// ⟨binaryOperator⟩ ::= ⟨multiplicativeOperator⟩
13+
/// | ⟨additiveOperator⟩
14+
/// | ⟨shiftOperator⟩
15+
/// | ⟨relationalOperator⟩
16+
/// | ‘==’
17+
/// | ⟨bitwiseOperator⟩
18+
///
19+
/// @description Checks that it is a compile-time error if an enum declares an
20+
/// abstract user-defined operator
21+
/// @author sgrekhov22@gmail.com
22+
23+
enum E {
24+
e1, e2;
25+
26+
int operator +(int other);
27+
//^
28+
// [analyzer] unspecified
29+
// [cfe] unspecified
30+
}
31+
32+
main() {
33+
print(E);
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion
6+
/// ⟨operatorSignature⟩ ::=
7+
/// ⟨type⟩? operator ⟨operator⟩ ⟨formalParameterList⟩
8+
/// ⟨operator⟩ ::= ‘~’
9+
/// | ⟨binaryOperator⟩
10+
/// | ‘[]
11+
/// | ‘[]=’
12+
/// ⟨binaryOperator⟩ ::= ⟨multiplicativeOperator⟩
13+
/// | ⟨additiveOperator⟩
14+
/// | ⟨shiftOperator⟩
15+
/// | ⟨relationalOperator⟩
16+
/// | ‘==’
17+
/// | ⟨bitwiseOperator⟩
18+
///
19+
/// @description Checks that it is not an error to declare an abstract
20+
/// user-defined operator
21+
/// @author sgrekhov22@gmail.com
22+
23+
import '../../../../Utils/expect.dart';
24+
25+
abstract class A {
26+
int operator +(int other);
27+
}
28+
29+
mixin M {
30+
int operator *(int other);
31+
}
32+
33+
class C extends A with M {
34+
int operator +(int other) => 1;
35+
int operator *(int other) => 2;
36+
}
37+
38+
main() {
39+
Expect.equals(1, C() + 42);
40+
Expect.equals(2, C() * 42);
41+
}

0 commit comments

Comments
 (0)