Skip to content

Commit 1139ed3

Browse files
alexmarkovCommit Bot
authored and
Commit Bot
committed
[vm] Record toString()
TEST=language/records/simple/to_string_test Issue: #49719 Change-Id: I7e55a2486b1d964b24a287ffcf87c40c20f9cfe9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260480 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent b0ea39b commit 1139ed3

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

sdk/lib/_internal/vm/lib/object_patch.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ class _Record {
132132
return SystemHash.finish(hash);
133133
}
134134

135+
// Do not inline to avoid mixing _fieldAt with
136+
// record field accesses.
137+
@pragma("vm:never-inline")
138+
String toString() {
139+
StringBuffer buffer = StringBuffer("(");
140+
final int numFields = _numFields;
141+
final _List fieldNames = _fieldNames;
142+
final int numPositionalFields = numFields - fieldNames.length;
143+
for (int i = 0; i < numFields; ++i) {
144+
if (i != 0) {
145+
buffer.write(", ");
146+
}
147+
if (i >= numPositionalFields) {
148+
buffer.write(unsafeCast<String>(fieldNames[i - numPositionalFields]));
149+
buffer.write(": ");
150+
}
151+
buffer.write(_fieldAt(i).toString());
152+
}
153+
buffer.write(")");
154+
return buffer.toString();
155+
}
156+
135157
@pragma("vm:recognized", "other")
136158
@pragma("vm:prefer-inline")
137159
external int get _numFields;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code as governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// SharedOptions=--enable-experiment=records
6+
7+
import "package:expect/expect.dart";
8+
9+
class A {
10+
final String name;
11+
const A(this.name);
12+
13+
String toString() => name;
14+
}
15+
16+
main() {
17+
// Although the order of fields in toString() is unspecified,
18+
// this test assumes that positional fields are printed first and
19+
// named fields are sorted lexicographically.
20+
// This test might need more sophisticated checks if there is
21+
// an implementation which doesn't follow that order.
22+
23+
Expect.equals("(1, 2)", (1, 2).toString());
24+
Expect.equals("(1, 2)", (const (1, 2)).toString());
25+
Expect.equals("(3, 2, 1)", (3, 2, 1).toString());
26+
27+
Expect.equals("(1, foo: 2)", (1, foo: 2).toString());
28+
Expect.equals("(1, foo: 2)", (foo: 2, 1).toString());
29+
30+
Expect.equals("(1, abc, bar: 3, foo: 2)", (1, foo: 2, "abc", bar: 3).toString());
31+
32+
Expect.equals("((A1, A2), (foo: A3), (A7, bar: A5, baz: A6, foo: A4))", ((A("A1"), A("A2")), const (foo: A("A3")), (foo: A("A4"), bar: A("A5"), baz: A("A6"), A("A7"))).toString());
33+
}

0 commit comments

Comments
 (0)