From fa81b265829018aa1761c014672467cd4527758d Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Tue, 30 Jan 2024 11:31:40 +0200 Subject: [PATCH] #1399. Add more tests for records --- LanguageFeatures/Records/members_A02_t09.dart | 28 +++++++++++ .../Records/record_expressions_A02_t06.dart | 47 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 LanguageFeatures/Records/members_A02_t09.dart create mode 100644 LanguageFeatures/Records/record_expressions_A02_t06.dart diff --git a/LanguageFeatures/Records/members_A02_t09.dart b/LanguageFeatures/Records/members_A02_t09.dart new file mode 100644 index 0000000000..619eea31a4 --- /dev/null +++ b/LanguageFeatures/Records/members_A02_t09.dart @@ -0,0 +1,28 @@ +// Copyright (c) 2024, 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 record type declares all of the members defined on [Object]. It +/// also exposes getters for each named field where the name of the getter is +/// the field's name and the getter's type is the field's type. For each +/// positional field, it exposes a getter whose name is $ followed by the number +/// of preceding positional fields and whose type is the type of the field. +/// +/// @description Check that function types can be stored in records and then +/// accessed and invoked +/// @author sgrekhov22@gmail.com + +import "../../Utils/expect.dart"; + +class C { + int call() => 3; +} + +main() { + (Function f1, {Function f2}) r1 = (() => 1, f2: () {return 2;}); + Expect.equals(1, r1.$1()); + Expect.equals(2, r1.f2()); + + (C,) r2 = (C(),); + Expect.equals(3, r2.$1()); +} diff --git a/LanguageFeatures/Records/record_expressions_A02_t06.dart b/LanguageFeatures/Records/record_expressions_A02_t06.dart new file mode 100644 index 0000000000..56eacff69f --- /dev/null +++ b/LanguageFeatures/Records/record_expressions_A02_t06.dart @@ -0,0 +1,47 @@ +// Copyright (c) 2024, 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 record is created using a record expression. The grammar is: +/// +/// literal ::= record +/// | // Existing literal productions... +/// record ::= 'const'? '(' recordField ( ',' recordField )* ','? ')' +/// recordField ::= (identifier ':' )? expression +/// +/// This is identical to the grammar for a function call argument list. There +/// are a couple of syntactic restrictions not captured by the grammar. It is a +/// compile-time error if a record has any of: +/// +/// The same field name more than once. +/// +/// Only one positional field and no trailing comma. +/// +/// No fields and a trailing comma. The expression (,) isn't allowed. +/// +/// A field named hashCode, runtimeType, noSuchMethod, or toString. +/// +/// A field name that starts with an underscore. +/// +/// A field name that collides with the synthesized getter name of a positional +/// field. For example: ('pos', $1: 'named') since the named field '$1' collides +/// with the getter for the first positional field. +/// +/// @description Checks that it is a compile-time error if a record has one +/// named field with no trailing comma +/// @author sgrekhov22@gmail.com + +void foo((int,) r) {} + +(String,) bar() => ("No trailing comma"); +// ^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + +main() { + foo((1)); +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified + print(bar); +}