Skip to content

Commit 81df362

Browse files
mkustermannCommit Queue
authored and
Commit Queue
committed
Make utf8.encode() have Uint8List return type
Right now `utf8.encode()` has a static return type of `List<int>` due to extending `Encoding` (which extends `Codec<String, List<int>>`). We cannot easily change `Encoding` to extend `Codec<String, Uint8List>` because that would also change `utf8.decode()` to require `Uint8List` which would be a breaking change. So instead we override `utf8.encode()` to have more precise return type. Some parts of our SDK are run using the checked-in SDK, so it cannot rely on the changed return type yet (until checked-in SDK is rolled). So we use `const Utf8Encoder().convert()` as a temporary change, as that already has `Uint8List` return type. Issue #52801 TEST=ci CoreLibraryReviewExempt: More precise return type for existing API Change-Id: I2861d1f0eb3d292d8e3ec8437c0d441a2d2bd193 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254903 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Lasse Nielsen <[email protected]>
1 parent e1c12b3 commit 81df362

File tree

12 files changed

+35
-38
lines changed

12 files changed

+35
-38
lines changed

pkg/analysis_server/test/mocks.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ class MockProcess implements Process {
6868
int get pid => _pid;
6969

7070
@override
71-
Stream<List<int>> get stderr => Future.value(utf8.encode(_stderr)).asStream();
71+
Stream<List<int>> get stderr => Stream<List<int>>.value(utf8.encode(_stderr));
7272

7373
@override
74-
Stream<List<int>> get stdout => Future.value(utf8.encode(_stdout)).asStream();
74+
Stream<List<int>> get stdout => Stream<List<int>>.value(utf8.encode(_stdout));
7575

7676
@override
7777
bool kill([ProcessSignal signal = ProcessSignal.sigterm]) {

pkg/analyzer/lib/file_system/memory_file_system.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ class MemoryResourceProvider implements ResourceProvider {
145145
}
146146

147147
_pathToData[path] = _FileData(
148-
bytes: utf8.encode(content) as Uint8List,
148+
bytes: const Utf8Encoder().convert(content),
149149
timeStamp: nextStamp++,
150150
);
151151
_notifyWatchers(path, ChangeType.MODIFY);
152152
}
153153

154154
File newFile(String path, String content) {
155-
var bytes = utf8.encode(content) as Uint8List;
155+
var bytes = const Utf8Encoder().convert(content);
156156
return newFileWithBytes(path, bytes);
157157
}
158158

@@ -422,7 +422,7 @@ class _MemoryFile extends _MemoryResource implements File {
422422

423423
@override
424424
void writeAsStringSync(String content) {
425-
var bytes = utf8.encode(content) as Uint8List;
425+
var bytes = const Utf8Encoder().convert(content);
426426
writeAsBytesSync(bytes);
427427
}
428428
}

pkg/analyzer/lib/file_system/overlay_file_system.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class _OverlayFile extends _OverlayResource implements File {
182182
Uint8List readAsBytesSync() {
183183
String? content = provider._getOverlayContent(path);
184184
if (content != null) {
185-
return utf8.encode(content) as Uint8List;
185+
return const Utf8Encoder().convert(content);
186186
}
187187
return _file.readAsBytesSync();
188188
}

pkg/analyzer/lib/src/summary2/data_writer.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ class BufferedSink {
178178

179179
/// Write the [value] as UTF8 encoded byte array.
180180
void writeStringUtf8(String value) {
181-
var bytes = utf8.encode(value);
182-
writeUint8List(bytes as Uint8List);
181+
writeUint8List(const Utf8Encoder().convert(value));
183182
}
184183

185184
void writeStringUtf8Iterable(Iterable<String> items) {

pkg/front_end/lib/src/api_prototype/memory_file_system.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ class MemoryFileSystemEntity implements FileSystemEntity {
131131
/// If no file exists, one is created. If a file exists already, it is
132132
/// overwritten.
133133
void writeAsStringSync(String s) {
134-
// Note: the return type of utf8.encode is List<int>, but in practice it
135-
// always returns Uint8List. We rely on that for efficiency, so that we
136-
// don't have to make an extra copy.
137-
_update(uri, utf8.encode(s) as Uint8List);
134+
_update(uri, const Utf8Encoder().convert(s));
138135
}
139136

140137
void _update(Uri uri, Uint8List data) {

pkg/front_end/test/crashing_test_case_minimizer_impl.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class TestMinimizer {
293293
Future _tryToMinimizeImpl() async {
294294
// Set main to be basically empty up front.
295295
_settings._useInitialFs = true;
296-
_fs.data[_mainUri] = utf8.encode("main() {}") as Uint8List;
296+
_fs.data[_mainUri] = utf8.encode("main() {}");
297297
Component initialComponent = await _getInitialComponent();
298298
print("Compiled initially (without data)");
299299
// Remove fake cache.
@@ -326,7 +326,7 @@ class TestMinimizer {
326326
if (_knownByCompiler(uri!)) {
327327
String parsedString =
328328
_getFileAsStringContent(_fs.data[uri]!, _isUriNnbd(uri));
329-
_fs.data[uri] = utf8.encode(parsedString) as Uint8List;
329+
_fs.data[uri] = utf8.encode(parsedString);
330330
}
331331
} catch (e) {
332332
// crash in scanner/parser --- keep original file. This crash might
@@ -628,8 +628,7 @@ class TestMinimizer {
628628
for (int i = offsetOfLast; i < withoutInlineableString.length; i++) {
629629
builder.writeCharCode(withoutInlineableString.codeUnitAt(i));
630630
}
631-
final Uint8List inlinedWithoutChange =
632-
utf8.encode(builder.toString()) as Uint8List;
631+
final Uint8List inlinedWithoutChange = utf8.encode(builder.toString());
633632

634633
if (!_parsesWithoutError(inlinedWithoutChange, _isUriNnbd(uri))) {
635634
print("WARNING: Parser error after stuff at ${StackTrace.current}");
@@ -669,8 +668,7 @@ class TestMinimizer {
669668
for (int i = offsetOfLast; i < withoutInlineableString.length; i++) {
670669
builder.writeCharCode(withoutInlineableString.codeUnitAt(i));
671670
}
672-
Uint8List inlinedWithChange =
673-
utf8.encode(builder.toString()) as Uint8List;
671+
Uint8List inlinedWithChange = utf8.encode(builder.toString());
674672

675673
if (!_parsesWithoutError(inlinedWithChange, _isUriNnbd(uri))) {
676674
print("WARNING: Parser error after stuff at ${StackTrace.current}");
@@ -1015,7 +1013,7 @@ worlds:
10151013

10161014
bool outlined = false;
10171015
if (textualOutlined != null) {
1018-
Uint8List candidate = utf8.encode(textualOutlined) as Uint8List;
1016+
Uint8List candidate = utf8.encode(textualOutlined);
10191017
// Because textual outline doesn't do the right thing for nnbd, only
10201018
// replace if it's syntactically valid.
10211019
if (candidate.length != _fs.data[uri]!.length &&
@@ -1041,8 +1039,7 @@ worlds:
10411039
if (!string.trim().startsWith("//")) stringsLeft.add(string);
10421040
}
10431041

1044-
Uint8List candidate =
1045-
utf8.encode(stringsLeft.join("\n")) as Uint8List;
1042+
Uint8List candidate = utf8.encode(stringsLeft.join("\n"));
10461043
if (candidate.length != _fs.data[uri]!.length) {
10471044
if (await _shouldQuit()) return;
10481045
_fs.data[uri] = candidate;
@@ -1088,7 +1085,7 @@ worlds:
10881085
}
10891086
}
10901087
string = lines.join("\n");
1091-
_fs.data[uri] = utf8.encode(string) as Uint8List;
1088+
_fs.data[uri] = utf8.encode(string);
10921089
if (!await _crashesOnCompile(initialComponent)) {
10931090
// For some reason that didn't work.
10941091
_fs.data[uri] = data;
@@ -1226,7 +1223,7 @@ worlds:
12261223
}
12271224
}
12281225
string = lines.join("\n");
1229-
Uint8List candidate = utf8.encode(string) as Uint8List;
1226+
Uint8List candidate = utf8.encode(string);
12301227
if (candidate.length != data.length) {
12311228
_fs.data[uri] = candidate;
12321229
if (!await _crashesOnCompile(initialComponent)) {
@@ -1248,7 +1245,7 @@ worlds:
12481245
while (i < packagesModified.length) {
12491246
var oldEntry = packagesModified.removeAt(i);
12501247
String jsonString = jsonEncoder.convert(jsonModified);
1251-
candidate = utf8.encode(jsonString) as Uint8List;
1248+
candidate = utf8.encode(jsonString);
12521249
Uint8List? previous = _fs.data[uri];
12531250
_fs.data[uri] = candidate;
12541251
if (!await _crashesOnCompile(initialComponent)) {
@@ -2071,7 +2068,7 @@ worlds:
20712068
builder.writeCharCode(_dataCacheString!.codeUnitAt(j));
20722069
}
20732070

2074-
Uint8List candidate = utf8.encode(builder.toString()) as Uint8List;
2071+
Uint8List candidate = utf8.encode(builder.toString());
20752072
return candidate;
20762073
}
20772074
}
@@ -2130,7 +2127,7 @@ class _FakeFileSystem extends FileSystem {
21302127
if (tmp[i + 1] == null) {
21312128
data[key] = null;
21322129
} else if (tmp[i + 1] is String) {
2133-
data[key] = utf8.encode(tmp[i + 1]) as Uint8List;
2130+
data[key] = utf8.encode(tmp[i + 1]);
21342131
} else {
21352132
data[key] = Uint8List.fromList(new List<int>.from(tmp[i + 1]));
21362133
}

pkg/front_end/test/dartdoc_test_test.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:convert';
2-
import 'dart:typed_data';
32

43
import 'package:_fe_analyzer_shared/src/scanner/token.dart';
54
import 'package:front_end/src/api_prototype/memory_file_system.dart';
@@ -460,7 +459,7 @@ bool _expectImpl(dynamic actual, dynamic expected, StringBuffer explainer) {
460459
}
461460

462461
impl.CommentString? extractFirstComment(String test) {
463-
Token firstToken = impl.scanRawBytes(utf8.encode(test) as Uint8List);
462+
Token firstToken = impl.scanRawBytes(utf8.encode(test));
464463
Token token = firstToken;
465464
while (true) {
466465
CommentToken? comment = token.precedingComments;
@@ -476,6 +475,6 @@ impl.CommentString? extractFirstComment(String test) {
476475
}
477476

478477
List<impl.Test> extractTests(String test, [Uri? uri]) {
479-
return impl.extractTests(utf8.encode(test) as Uint8List,
480-
uri ?? new Uri(scheme: "darttest", path: "/foo.dart"));
478+
return impl.extractTests(
479+
utf8.encode(test), uri ?? new Uri(scheme: "darttest", path: "/foo.dart"));
481480
}

pkg/front_end/test/fasta/testing/suite.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
library fasta.testing.suite;
66

7-
import 'dart:convert' show jsonDecode, utf8;
7+
import 'dart:convert' show jsonDecode, utf8, Utf8Encoder;
88
import 'dart:io' show Directory, File, Platform;
99
import 'dart:typed_data' show Uint8List;
1010

@@ -1528,7 +1528,7 @@ class FuzzCompiles
15281528
for (FuzzAstVisitorSorterChunk chunk in fuzzAstVisitorSorter.chunks) {
15291529
sb.writeln(chunk.getSource());
15301530
}
1531-
Uint8List sortedData = utf8.encode(sb.toString()) as Uint8List;
1531+
Uint8List sortedData = const Utf8Encoder().convert(sb.toString());
15321532
fs.data[uri] = sortedData;
15331533
incrementalCompiler = new IncrementalCompiler.fromComponent(
15341534
new CompilerContext(compilationSetup.options), platform);
@@ -1644,7 +1644,7 @@ class FuzzCompiles
16441644
print("Skipping $uri -- couldn't find builder for it.");
16451645
continue;
16461646
}
1647-
Uint8List orgData = fs.data[uri] as Uint8List;
1647+
Uint8List orgData = fs.data[uri]!;
16481648
FuzzAstVisitorSorter fuzzAstVisitorSorter;
16491649
try {
16501650
fuzzAstVisitorSorter =
@@ -1696,7 +1696,7 @@ class FuzzCompiles
16961696
sb.writeln("import '${uri.pathSegments.last}';");
16971697
sb.writeln(chunk.getSource());
16981698
fs.data[getUriForChunk(currentSubFile)] =
1699-
utf8.encode(sb.toString()) as Uint8List;
1699+
const Utf8Encoder().convert(sb.toString());
17001700
print(" => Split into ${getUriForChunk(currentSubFile)}:\n"
17011701
"${sb.toString()}\n-------------\n");
17021702
currentSubFile++;
@@ -1711,7 +1711,7 @@ class FuzzCompiles
17111711
}
17121712
sb.writeln(orgFileOnlyHeaderSb.toString());
17131713
print(" => Main file becomes:\n${sb.toString()}\n-------------\n");
1714-
fs.data[uri] = utf8.encode(sb.toString()) as Uint8List;
1714+
fs.data[uri] = const Utf8Encoder().convert(sb.toString());
17151715
}
17161716

17171717
Result<ComponentResult>? passResult = await performFileInvalidation(

pkg/front_end/tool/dart_doctest_impl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ List<Test> extractTestsFromComment(
555555

556556
Test scanDartDoc(int scanOffset) {
557557
final Token firstToken =
558-
scanRawBytes(utf8.encode(comments.substring(scanOffset)) as Uint8List);
558+
scanRawBytes(utf8.encode(comments.substring(scanOffset)));
559559
final ErrorListener listener = new ErrorListener();
560560
final Parser parser = new Parser(listener,
561561
useImplicitCreationExpression: useImplicitCreationExpressionInCfe);

pkg/front_end/tool/perf.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Set<Source> scanReachableFiles(Uri entryUri) {
209209
/// Loads the file contents of all [files] as bytes.
210210
Set<Uint8List> loadFileContentsAsBytes(Set<Source> files) {
211211
return files.map((Source source) {
212-
final bytes = utf8.encode(source.contents.data) as Uint8List;
212+
final bytes = utf8.encode(source.contents.data);
213213
// CFE needs files to e 0-terminated.
214214
return Uint8List(bytes.length + 1)
215215
..setRange(0, bytes.length, bytes)

pkg/vm/bin/kernel_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ String _escapeDependency(Uri uri) {
701701
}
702702

703703
Uint8List _serializeDependencies(List<Uri> uris) {
704-
return utf8.encode(uris.map(_escapeDependency).join(" ")) as Uint8List;
704+
return utf8.encode(uris.map(_escapeDependency).join(" "));
705705
}
706706

707707
Future _processListDependenciesRequest(

sdk/lib/convert/utf.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ final class Utf8Codec extends Encoding {
6363
return decoder.convert(codeUnits);
6464
}
6565

66+
/// Encodes the [string] as UTF-8.
67+
Uint8List encode(String string) {
68+
return const Utf8Encoder().convert(string);
69+
}
70+
6671
Utf8Encoder get encoder => const Utf8Encoder();
6772
Utf8Decoder get decoder {
6873
// Switch between const objects to avoid allocation.

0 commit comments

Comments
 (0)