Files
sdk/pkg/dev_compiler/test/expression_compiler/expression_compiler_test.dart
T
Ben Konyi 364b36691c Reland "[ DDC / CFE ] Add support for allowing imports of unsupported libraries"
This reverts commit c616db31d2.

Reason for revert: Fix landed downstream in Flutter engine: https://github.com/flutter/flutter/pull/180127

Original change's description:
> Revert "[ DDC / CFE ] Add support for allowing imports of unsupported libraries"
>
> This reverts commit b5e60be49d.
>
> Reason for revert: broke Flutter web engine tests
>
> Original change's description:
> > [ DDC / CFE ] Add support for allowing imports of unsupported libraries
> >
> > This change adds support for allowing for imports of unsupported
> > platform-specific libraries when the
> > `--include-unsupported-platform-library-stubs` flag is provided to the
> > CFE.
> >
> > This flag sets the `includeUnsupportedPlatformLibraryStubs` property in
> > `TargetFlags`, which `Target`s can use to conditionally return different
> > `DartLibrarySupport` objects with different supported/unsupported
> > library sets.
> >
> > A `checkForUnsupportedDartColonImports` function has been added to
> > `Target` that uses the value of `dartLibrarySupport` to determine if
> > there's any unsupported library imports. This function is called after
> > the various transformation operations provided by the `Target`
> > implementation, meaning the import of an unsupported library specified
> > in `dartLibrarySupport` will now result in a compilation error (this
> > includes `dart:mirrors` imports for VM targets when mirrors are
> > disabled, which was previously handled by the VM itself).
> >
> > Related to https://github.com/dart-lang/sdk/issues/62125
> >
> > TEST=Tests added / modified
> >
> > Change-Id: Ife819b2e1a6d28f67d80aab6701cd23a1724aa4d
> > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465760
> > Reviewed-by: Nicholas Shahan <nshahan@google.com>
> > Reviewed-by: Johnni Winther <johnniwinther@google.com>
> > Commit-Queue: Ben Konyi <bkonyi@google.com>
>
> Change-Id: I0b59f00e55a2424f783351abd977eb38409ce01f
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/469100
> Reviewed-by: Nate Biggs <natebiggs@google.com>
> Commit-Queue: Alexander Markov <alexmarkov@google.com>
> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> Reviewed-by: Ben Konyi <bkonyi@google.com>
> Reviewed-by: Sigmund Cherem <sigmund@google.com>

Change-Id: I1ae2eac675432286aebabea3c1f58caf35a27fbb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/469240
Commit-Queue: Ben Konyi <bkonyi@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
2025-12-19 12:53:22 -08:00

249 lines
6.4 KiB
Dart

// Copyright (c) 2020, 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:dev_compiler/src/compiler/module_builder.dart';
import 'package:dev_compiler/src/kernel/target.dart';
import 'package:kernel/target/targets.dart';
import 'package:test/test.dart';
import '../shared_test_options.dart';
import 'expression_compiler_suite.dart';
void main(List<String> args) {
for (var moduleFormat in [ModuleFormat.amd, ModuleFormat.ddc]) {
group('Module format: $moduleFormat |', () {
runTests(SetupCompilerOptions(moduleFormat: moduleFormat, args: args));
});
}
}
void runTests(SetupCompilerOptions setup) {
group('Expression compilations on the same expression compiler |', () {
var source = '''
main() {
}
void foo() {
// Breakpoint
}
''';
late ExpressionCompilerTestDriver driver;
setUp(() {
driver = ExpressionCompilerTestDriver(setup, source);
});
tearDown(() {
driver.delete();
});
test('successful expression compilations', () async {
var compiler = await driver.createCompiler();
await driver.check(
compiler: compiler,
scope: <String, String>{},
expression: 'true',
expectedResult: contains('return true;'),
);
await driver.check(
compiler: compiler,
scope: <String, String>{},
expression: 'false',
expectedResult: contains('return false;'),
);
});
test('some successful expression compilations', () async {
var compiler = await driver.createCompiler();
await driver.check(
compiler: compiler,
scope: <String, String>{},
expression: 'true',
expectedResult: contains('return true;'),
);
await driver.check(
compiler: compiler,
scope: <String, String>{},
expression: 'blah',
expectedError: "Undefined name 'blah'",
);
await driver.check(
compiler: compiler,
scope: <String, String>{},
expression: 'false',
expectedResult: contains('return false;'),
);
});
test('failing expression compilations', () async {
var compiler = await driver.createCompiler();
await driver.check(
compiler: compiler,
scope: <String, String>{},
expression: 'blah1',
expectedError: "Undefined name 'blah1'",
);
await driver.check(
compiler: compiler,
scope: <String, String>{},
expression: 'blah2',
expectedError: "Undefined name 'blah2'",
);
});
});
group('Expression compiler dart: import tests', () {
var source = '''
import 'dart:io' show Directory;
import 'dart:io' as p;
import 'dart:convert' as p;
main() {
print(Directory.systemTemp);
print(p.Directory.systemTemp);
print(p.utf8.decoder);
}
void foo() {
// Breakpoint
}
''';
late ExpressionCompilerTestDriver driver;
setUp(() {
driver = ExpressionCompilerTestDriver(setup, source);
});
tearDown(() {
driver.delete();
});
test('expression referencing unnamed import', () async {
await driver.check(
scope: <String, String>{},
expression: 'Directory.systemTemp',
expectedResult: contains('return io.Directory.systemTemp;'),
);
});
test('expression referencing named import', () async {
await driver.check(
scope: <String, String>{},
expression: 'p.Directory.systemTemp',
expectedResult: contains('return io.Directory.systemTemp;'),
);
});
test(
'expression referencing another library with the same named import',
() async {
await driver.check(
scope: <String, String>{},
expression: 'p.utf8.decoder',
expectedResult: contains('return convert.utf8.decoder;'),
);
},
);
});
group('Expression compiler dart: platform import tests', () {
var source = '''
import 'dart:ffi' as ffi;
main() {
print(ffi.Abi.current());
}
void foo() {
// Breakpoint
}
''';
late ExpressionCompilerTestDriver driver;
setUp(() {
driver = ExpressionCompilerTestDriver(setup, source)
..setup.options.target = DevCompilerTarget(
TargetFlags(includeUnsupportedPlatformLibraryStubs: true),
);
});
tearDown(() {
driver.delete();
});
test('expression referencing dart:ffi', () async {
await driver.check(
scope: <String, String>{},
expression: 'ffi.Abi.current()',
expectedResult: contains('return ffi.Abi.current();'),
);
});
});
group('Expression compiler package: import tests', () {
var source = '''
import 'package:a/a.dart' show topLevelMethod;
import 'package:a/a.dart' as prefix;
import 'package:b/b.dart' as prefix;
main() {
print(topLevelMethod(99));
print(prefix.topLevelMethod(99));
print(prefix.anotherTopLevelMethod('hello'));
}
void foo() {
// Breakpoint
}
''';
var a = 'bool topLevelMethod(int i) => i.isEven;';
var b = 'int anotherTopLevelMethod(String s) => s.length;';
late ExpressionCompilerTestDriver driver;
setUp(() {
driver = ExpressionCompilerTestDriver(
setup,
source,
additionalLibraries: [(name: 'a', source: a), (name: 'b', source: b)],
);
});
tearDown(() {
driver.delete();
});
test('expression referencing unnamed import', () async {
await driver.check(
scope: <String, String>{},
expression: 'topLevelMethod(99)',
expectedResult: contains('return a.topLevelMethod(99);'),
);
});
test('expression referencing named import', () async {
await driver.check(
scope: <String, String>{},
expression: 'prefix.topLevelMethod(99)',
expectedResult: contains('return a.topLevelMethod(99);'),
);
});
test(
'expression referencing another library with the same named import',
() async {
await driver.check(
scope: <String, String>{},
expression: 'prefix.anotherTopLevelMethod("hello")',
expectedResult: contains('return b.anotherTopLevelMethod("hello")'),
);
},
);
});
}