Skip to content

Commit

Permalink
[parser] Fix shifting that fails on web
Browse files Browse the repository at this point in the history
This CL uses Martins suggested fix (adding 1 to avoid shifting a
negative), and adds a simple test that compiles the parser to dart2js
(with asserts enabled) and runs it via d8. This will perhaps catch
breakage up front another time.
To my knowledge this is not a supported use case though, so likely
we can't but in much effort for any future big breakages.

Fixes #50048

Change-Id: Ic5ac6e63f2d6d32e38ba562ed21dbe85328935cc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/261301
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
  • Loading branch information
jensjoha authored and Commit Queue committed Sep 27, 2022
1 parent c04673f commit 4124e85
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/_fe_analyzer_shared/lib/src/scanner/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class SimpleToken implements Token {
* token.
*/
@override
int get offset => _typeAndOffset >> 8;
int get offset => (_typeAndOffset >> 8) - 1;

/**
* Set the offset from the beginning of the file to the first character in
Expand All @@ -525,7 +525,9 @@ class SimpleToken implements Token {
@override
void set offset(int value) {
assert(_tokenTypesByIndex.length < 256);
_typeAndOffset = (value << 8) | (_typeAndOffset & 0xff);
// See https://github.com/dart-lang/sdk/issues/50048 for details.
assert(value >= -1);
_typeAndOffset = ((value + 1) << 8) | (_typeAndOffset & 0xff);
}

/**
Expand All @@ -551,7 +553,10 @@ class SimpleToken implements Token {
* Initialize a newly created token to have the given [type] and [offset].
*/
SimpleToken(TokenType type, int offset, [this._precedingComment])
: _typeAndOffset = ((offset << 8) | type.index) {
: _typeAndOffset = (((offset + 1) << 8) | type.index) {
// See https://github.com/dart-lang/sdk/issues/50048 for details.
assert(offset >= -1);

// Assert the encoding of the [type] is fully reversible.
assert(type.index < 256 && _tokenTypesByIndex.length < 256);
assert(identical(offset, this.offset));
Expand Down
70 changes: 70 additions & 0 deletions pkg/front_end/test/web_parser_git_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2022, 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.

import 'dart:io';

import "utils/io_utils.dart";

Future<void> main(List<String> args) async {
Uri dart = repoDir.resolve(
"tools/sdks/dart-sdk/bin/dart${Platform.isWindows ? ".exe" : ""}");
if (!new File.fromUri(dart).existsSync()) {
throw "Couldn't find $dart executable.";
}

Uri d8 = repoDir.resolve(_d8executable);
if (!new File.fromUri(d8).existsSync()) {
throw "Couldn't find $d8 executable.";
}

Uri fileHelper =
repoDir.resolve("pkg/front_end/test/web_parser_git_test_helper.dart");
if (!new File.fromUri(fileHelper).existsSync()) {
throw "Couldn't find $fileHelper file.";
}

Directory tempDir = Directory.systemTemp.createTempSync("web_parser_test");
try {
Uri outFile = tempDir.uri.resolve("out.js");
ProcessResult dartRun = Process.runSync(dart.toFilePath(), [
"compile",
"js",
"--output",
outFile.toFilePath(),
"--enable-asserts",
fileHelper.toFilePath()
]);
if (dartRun.exitCode != 0) {
throw "---\n"
"Dart run returned ${dartRun.exitCode}.\n"
"stdout: ${dartRun.stdout}\n\n"
"stderr: ${dartRun.stderr}"
"---";
}
ProcessResult d8Run =
Process.runSync(d8.toFilePath(), [outFile.toFilePath()]);
if (d8Run.exitCode != 0) {
throw "---\n"
"D8 run returned ${d8Run.exitCode}.\n"
"stdout: ${d8Run.stdout}\n\n"
"stderr: ${d8Run.stderr}\n"
"---";
}
} finally {
tempDir.deleteSync(recursive: true);
}
}

final Uri repoDir = computeRepoDirUri();

String get _d8executable {
if (Platform.isWindows) {
return 'third_party/d8/windows/d8.exe';
} else if (Platform.isLinux) {
return 'third_party/d8/linux/d8';
} else if (Platform.isMacOS) {
return 'third_party/d8/macos/d8';
}
throw UnsupportedError('Unsupported platform.');
}
38 changes: 38 additions & 0 deletions pkg/front_end/test/web_parser_git_test_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2022, 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.

import 'package:_fe_analyzer_shared/src/parser/forwarding_listener.dart'
show ForwardingListener;
import 'package:_fe_analyzer_shared/src/parser/parser.dart' show Parser;
import 'package:_fe_analyzer_shared/src/scanner/scanner.dart'
show ScannerConfiguration, StringScanner;
import 'package:_fe_analyzer_shared/src/scanner/token.dart' show Token;

void main(List<String> args) {
String source = """
void main(List<String> args) {
print("Hello, World!");
}
""";

ScannerConfiguration scannerConfiguration = new ScannerConfiguration(
enableExtensionMethods: true,
enableNonNullable: true,
enableTripleShift: true);

StringScanner scanner = new StringScanner(
source,
includeComments: true,
configuration: scannerConfiguration,
languageVersionChanged: (scanner, languageVersion) {
// For now don't do anything, but having it (making it non-null) means the
// configuration won't be reset.
},
);
Token firstToken = scanner.tokenize();
ForwardingListener listener = new ForwardingListener();
Parser parser = new Parser(listener);
parser.parseUnit(firstToken);
print("--- End of parsing ---");
}

0 comments on commit 4124e85

Please sign in to comment.