blob: e77adc2bbf91d8cdca6f886ca180649c2daa19d0 [file] [log] [blame]
// Copyright (c) 2018, 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.
// @dart = 2.7
import 'package:async_helper/async_helper.dart';
import 'package:compiler/src/common_elements.dart';
import 'package:compiler/src/compiler.dart';
import 'package:compiler/src/elements/entities.dart';
import 'package:compiler/src/js_backend/runtime_types_resolution.dart';
import 'package:compiler/src/js_emitter/model.dart';
import 'package:compiler/src/js_model/js_strategy.dart';
import 'package:compiler/src/js/js.dart' as js;
import 'package:compiler/src/world.dart';
import 'package:expect/expect.dart';
import '../helpers/program_lookup.dart';
import '../helpers/memory_compiler.dart';
const String code = '''
class A<T> {
final field;
factory A.fact(t) => new A(t);
A(t) : field = t is T;
// A call to A.fact.
callAfact() => new A<int>.fact(0).runtimeType;
main() {
main() {
asyncTest(() async {
CompilationResult result =
await runCompiler(memorySourceFiles: {'main.dart': code});
Compiler compiler = result.compiler;
JsBackendStrategy backendStrategy = compiler.backendStrategy;
JClosedWorld closedWorld = compiler.backendClosedWorldForTesting;
RuntimeTypesNeed rtiNeed = closedWorld.rtiNeed;
ElementEnvironment elementEnvironment = closedWorld.elementEnvironment;
ProgramLookup programLookup = new ProgramLookup(backendStrategy);
js.Name getName(String name) {
return backendStrategy.namerForTesting
.globalPropertyNameForMember(lookupMember(elementEnvironment, name));
void checkParameters(String name,
{int expectedParameterCount, bool needsTypeArguments}) {
FunctionEntity function = lookupMember(elementEnvironment, name);
"Unexpected type argument need for $function.");
Method method = programLookup.getMethod(function);
Expect.isNotNull(method, "No method found for $function");
js.Fun fun = method.code;
Expect.equals(expectedParameterCount, fun.params.length,
"Unexpected parameter count on $function: ${js.nodeToString(fun)}");
// The declarations should have type parameters only when needed.
expectedParameterCount: 2, needsTypeArguments: false);
checkParameters('A.', expectedParameterCount: 2, needsTypeArguments: false);
checkArguments(String name, String targetName,
{int expectedTypeArguments}) {
FunctionEntity function = lookupMember(elementEnvironment, name);
Method method = programLookup.getMethod(function);
js.Fun fun = method.code;
js.Name selector = getName(targetName);
bool callFound = false;
forEachNode(fun, onCall: (js.Call node) {
js.Expression target = js.undefer(;
if (target is js.PropertyAccess) {
js.Node targetSelector = js.undefer(target.selector);
if (targetSelector is js.Name && targetSelector.key == selector.key) {
callFound = true;
"Unexpected argument count in $function call to $targetName: "
"No call to $targetName in $function found: "
// The declarations should have type parameters only when needed by the
// selector.
checkArguments('callAfact', 'A.fact', expectedTypeArguments: 2);