dart-sdk/tests/language_2/call_method_implicit_tear_off_implements_function_test.dart
Paul Berry c4de71ede2 Re-land "Change front_end handling of callable classes."
Original description:
Change front_end handling of callable classes.

This CL is the first in a series of CLs to change the handling of
callable classes in Dart 2.0.

For purposes of this description, a "callable class" is a class whose
interface contains a `.call` method.

In Dart 1.0, a callable class was considered to be a subtype of the
`.call` method's function type.  This allowed the user to create
custom objects with similar behavior to closures, but with additional
fields and methods.

In Dart 2.0, a callable class is just an ordinary class, with no
subtype relation to any particular function type.  (Note however that
it is still permissible for a class to declare that it "implements
Function").

To reduce the amount of code broken by this change, a piece of
syntactic sugar is being added: if an expression whose static type is
a callable class appears where a function type is expected, an
implicit tear-off of the `.call` method is inserted.

Note that it is still possible at compile time to invoke an expression
whose static type is a callable class, and it is still possible at
runtime to invoke an expression whose runtime type is a callable
class; in both cases, this is considered an implicit invocation of the
class's `.call` method.  This is unchanged from Dart 1.0 behavior.

This CL introduces test cases for the new behavior, and implements the
implicit tear-off of `.call` in the front end.

Still to be implemented in future CLs:

- Spec text needs to be written.

- DDC/analyzer code needs to be written to perform implicit tear-offs
  of `.call`.

- The subtyping algorithm in DDC, analyzer, VM, and dart2js needs to
  be changed so that callable classes are no longer considered
  subtypes of any particular function type.

- A small corner case involving type parameters still needs to be
  addressed (see TODO in
  pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart).

Fixes #32064.

Change-Id: I0d397c608321e25ba42cc02aa8a516aa77aa7c9d
Reviewed-on: https://dart-review.googlesource.com/41280
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2018-02-14 15:22:25 +00:00

97 lines
1.8 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 test program to test arithmetic operations.
import "dart:async";
import "package:expect/expect.dart";
class B {}
class C implements Function {
B call(B b) => b;
}
typedef B BToB(B x);
typedef Object NullToObject(Null x);
C c = new C();
void check1(BToB f) {
Expect.isFalse(identical(c, f));
Expect.equals(c.call, f);
B b = new B();
Expect.identical(f(b), b);
}
void check2(FutureOr<BToB> f) {
Expect.isFalse(identical(c, f));
Expect.equals(c.call, f);
BToB f2 = f;
B b = new B();
Expect.identical(f2(b), b);
}
void check3(NullToObject f) {
Expect.isFalse(identical(c, f));
Expect.equals(c.call, f);
Expect.isNull(f(null));
}
void check4(FutureOr<NullToObject> f) {
Expect.isFalse(identical(c, f));
Expect.equals(c.call, f);
NullToObject f2 = f;
Expect.isNull(f2(null));
}
void check5(Function f) {
Expect.identical(c, f);
}
void check6(FutureOr<Function> f) {
Expect.identical(c, f);
}
void check7(C x) {
Expect.identical(c, x);
}
void check8(FutureOr<C> x) {
Expect.identical(c, x);
}
void check9(Object o) {
Expect.identical(c, o);
}
void check10(FutureOr<Object> o) {
Expect.identical(c, o);
}
void check11(dynamic d) {
Expect.identical(c, d);
}
void check12(FutureOr<dynamic> d) {
Expect.identical(c, d);
}
main() {
// Implicitly tears off c.call
check1(c); //# 01: ok
check2(c); //# 02: ok
check3(c); //# 03: ok
check4(c); //# 04: ok
// Does not tear off c.call
check5(c); //# 05: ok
check6(c); //# 06: ok
check7(c); //# 07: ok
check8(c); //# 08: ok
check9(c); //# 09: ok
check10(c); //# 10: ok
check11(c); //# 11: ok
check12(c); //# 12: ok
}