Files
sdk/pkg/compiler/test/model/supermixin_test.dart
T
Johnni Winther 52db62dd65 [cfe] Use MixinSuperStub
This CL adds stub for each concrete mixed in member into mixin
applications. The body of the stub calls the original member via
a super call.

This addition means that resolution of super access now use the
stub and not the original declaration as the target, which means
that it will be correct even when mixed in members are cloned. For
this reason, dart2js no longer needs to perform its own super
member resolution.

When a super call targets a mixin super stub (after cloning) it
can be optimized away by redirecting the call to the `stubTarget`
of the call. This optimization is performed in dart2js.

Since dart2js now uses the correct super target, its runtime
mixin application needs to avoid overriding members already
declared in the mixin application. These members are the
forwarding super stubs which ensure that correct runtime types
when members with covariant parameters are implemented.


Change-Id: Iab71ffcc400aa6a683987bc20b9553a263ebc8e1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176526
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
2020-12-25 23:32:17 +00:00

72 lines
2.4 KiB
Dart

// 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/commandline_options.dart';
import 'package:compiler/src/common_elements.dart';
import 'package:compiler/src/elements/entities.dart';
import 'package:expect/expect.dart';
import '../helpers/program_lookup.dart';
import '../helpers/memory_compiler.dart';
const String source = r'''
import 'package:expect/expect.dart';
class SuperA {
method1(a) => 'A$a';
}
class SuperB extends SuperA {
method1(a) => 'B$a';
}
class Mixin extends SuperA {
method1(a) => super.method1('M$a');
method2(a) => 'M$a';
}
class Class extends SuperB with Mixin {}
main() {
var c = new Class();
Expect.equals("BMC", c.method1('C'));
Expect.equals("MC", c.method2('C'));
}
''';
main() {
asyncTest(() async {
CompilationResult result = await runCompiler(
memorySourceFiles: {'main.dart': source},
options: <String>[Flags.disableInlining]);
Expect.isTrue(result.isSuccess);
JElementEnvironment elementEnvironment =
result.compiler.backendClosedWorldForTesting.elementEnvironment;
ClassEntity cls = lookupClass(elementEnvironment, 'Class');
ClassEntity mixin = lookupClass(elementEnvironment, 'Mixin');
ClassEntity superA = lookupClass(elementEnvironment, 'SuperA');
ClassEntity superB = lookupClass(elementEnvironment, 'SuperB');
ClassEntity superClass = elementEnvironment.getSuperClass(cls);
Expect.isTrue(elementEnvironment.isMixinApplicationWithMembers(superClass));
Expect.equals(mixin, elementEnvironment.getEffectiveMixinClass(superClass));
Expect.equals(superA, elementEnvironment.getSuperClass(mixin));
Expect.equals(superB, elementEnvironment.getSuperClass(superClass));
MemberEntity method1 = lookupMember(elementEnvironment, 'Class.method1');
Expect.equals(superClass, method1.enclosingClass);
MemberEntity method2 = lookupMember(elementEnvironment, 'Class.method2');
Expect.equals(mixin, method2.enclosingClass);
ProgramLookup lookup = new ProgramLookup(result.compiler.backendStrategy);
ClassData data = lookup.getClassData(superClass);
Expect.isNotNull(data.getMethod(method1));
Expect.isNull(data.getMethod(method2));
});
}