mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 16:41:07 +00:00
[vm] Record toString()
TEST=language/records/simple/to_string_test Issue: https://github.com/dart-lang/sdk/issues/49719 Change-Id: I7e55a2486b1d964b24a287ffcf87c40c20f9cfe9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260480 Commit-Queue: Alexander Markov <alexmarkov@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
parent
b0ea39bfa1
commit
1139ed3373
|
@ -132,6 +132,28 @@ class _Record {
|
|||
return SystemHash.finish(hash);
|
||||
}
|
||||
|
||||
// Do not inline to avoid mixing _fieldAt with
|
||||
// record field accesses.
|
||||
@pragma("vm:never-inline")
|
||||
String toString() {
|
||||
StringBuffer buffer = StringBuffer("(");
|
||||
final int numFields = _numFields;
|
||||
final _List fieldNames = _fieldNames;
|
||||
final int numPositionalFields = numFields - fieldNames.length;
|
||||
for (int i = 0; i < numFields; ++i) {
|
||||
if (i != 0) {
|
||||
buffer.write(", ");
|
||||
}
|
||||
if (i >= numPositionalFields) {
|
||||
buffer.write(unsafeCast<String>(fieldNames[i - numPositionalFields]));
|
||||
buffer.write(": ");
|
||||
}
|
||||
buffer.write(_fieldAt(i).toString());
|
||||
}
|
||||
buffer.write(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@pragma("vm:recognized", "other")
|
||||
@pragma("vm:prefer-inline")
|
||||
external int get _numFields;
|
||||
|
|
33
tests/language/records/simple/to_string_test.dart
Normal file
33
tests/language/records/simple/to_string_test.dart
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code as governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// SharedOptions=--enable-experiment=records
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class A {
|
||||
final String name;
|
||||
const A(this.name);
|
||||
|
||||
String toString() => name;
|
||||
}
|
||||
|
||||
main() {
|
||||
// Although the order of fields in toString() is unspecified,
|
||||
// this test assumes that positional fields are printed first and
|
||||
// named fields are sorted lexicographically.
|
||||
// This test might need more sophisticated checks if there is
|
||||
// an implementation which doesn't follow that order.
|
||||
|
||||
Expect.equals("(1, 2)", (1, 2).toString());
|
||||
Expect.equals("(1, 2)", (const (1, 2)).toString());
|
||||
Expect.equals("(3, 2, 1)", (3, 2, 1).toString());
|
||||
|
||||
Expect.equals("(1, foo: 2)", (1, foo: 2).toString());
|
||||
Expect.equals("(1, foo: 2)", (foo: 2, 1).toString());
|
||||
|
||||
Expect.equals("(1, abc, bar: 3, foo: 2)", (1, foo: 2, "abc", bar: 3).toString());
|
||||
|
||||
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());
|
||||
}
|
Loading…
Reference in a new issue