Skip to content

Commit 4d0b83e

Browse files
committed
dart-lang#1399. Add more tests for records (dart-lang#2511)
1 parent 121f1a0 commit 4d0b83e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2024, 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 A record type declares all of the members defined on [Object]. It
6+
/// also exposes getters for each named field where the name of the getter is
7+
/// the field's name and the getter's type is the field's type. For each
8+
/// positional field, it exposes a getter whose name is $ followed by the number
9+
/// of preceding positional fields and whose type is the type of the field.
10+
///
11+
/// @description Check that function types can be stored in records and then
12+
/// accessed and invoked
13+
/// @author [email protected]
14+
15+
import "../../Utils/expect.dart";
16+
17+
class C {
18+
int call() => 3;
19+
}
20+
21+
main() {
22+
(Function f1, {Function f2}) r1 = (() => 1, f2: () {return 2;});
23+
Expect.equals(1, r1.$1());
24+
Expect.equals(2, r1.f2());
25+
26+
(C,) r2 = (C(),);
27+
Expect.equals(3, r2.$1());
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2024, 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 A record is created using a record expression. The grammar is:
6+
///
7+
/// literal ::= record
8+
/// | // Existing literal productions...
9+
/// record ::= 'const'? '(' recordField ( ',' recordField )* ','? ')'
10+
/// recordField ::= (identifier ':' )? expression
11+
///
12+
/// This is identical to the grammar for a function call argument list. There
13+
/// are a couple of syntactic restrictions not captured by the grammar. It is a
14+
/// compile-time error if a record has any of:
15+
///
16+
/// The same field name more than once.
17+
///
18+
/// Only one positional field and no trailing comma.
19+
///
20+
/// No fields and a trailing comma. The expression (,) isn't allowed.
21+
///
22+
/// A field named hashCode, runtimeType, noSuchMethod, or toString.
23+
///
24+
/// A field name that starts with an underscore.
25+
///
26+
/// A field name that collides with the synthesized getter name of a positional
27+
/// field. For example: ('pos', $1: 'named') since the named field '$1' collides
28+
/// with the getter for the first positional field.
29+
///
30+
/// @description Checks that it is a compile-time error if a record has one
31+
/// named field with no trailing comma
32+
/// @author [email protected]
33+
34+
void foo((int,) r) {}
35+
36+
(String,) bar() => ("No trailing comma");
37+
// ^^^^^^^^^^^^^^^^^^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
41+
main() {
42+
foo((1));
43+
// ^^^
44+
// [analyzer] unspecified
45+
// [cfe] unspecified
46+
print(bar);
47+
}

0 commit comments

Comments
 (0)