Files
sdk/tests/language_2/function/regress_45601_test.dart
T
Nicholas Shahan 8e2539d24c [ddc] Fix equality tests for Functions
Marks `Function` as an extensible type so that DDC knows to use the
symbolized version of the equals operator.

The `Function` class at runtime already has its equals method attached
as the "symbolized" version. Consider `a == b`. When DDC knows `a` is
statically a raw `Function` and non-nullable (more likely with sound
null safety) the generated code should call the symbolized equals
member ex: `a[$_equals](b)`. Without this change the generated code
would be `a.equals(b)` and fail at runtime because the method does
not exist.

With this change co19_2/LibTest/core/Function/operator_eq_A01_t01
starts passing.

Change-Id: I80dd2abbbb04f1b7ab7e21dd14561a45f6e81459
Fixes: https://github.com/dart-lang/sdk/issues/45601
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194204
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2021-04-15 22:02:07 +00:00

43 lines
1.3 KiB
Dart

// Copyright (c) 2021, 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:expect/expect.dart";
// Regression test for https://github.com/dart-lang/sdk/issues/45601
// When comparing values of type `Function` for equality DDC was generating
// code that would throw at runtime.
void fn<T>(T t) => null;
void main() {
testStaticEquality();
testDynamicEquality();
}
/// Ensure `==` calls on function values that are statically typed as `Function`
/// work as expected.
void testStaticEquality() {
Function staticFunction = fn;
Expect.isTrue(staticFunction == fn);
Expect.isFalse(staticFunction == main);
Function staticFunction2 = null;
Expect.isFalse(staticFunction2 == staticFunction);
staticFunction2 = fn;
Expect.isTrue(staticFunction2 == staticFunction);
}
/// Ensure `==` calls on function values that are statically typed as `dynamic`
/// work as expected.
void testDynamicEquality() {
dynamic dynamicFunction = fn;
Expect.isTrue(dynamicFunction == fn);
Expect.isFalse(dynamicFunction == main);
dynamic dynamicFunction2 = null;
Expect.isFalse(dynamicFunction2 == dynamicFunction);
dynamicFunction2 = fn;
Expect.isTrue(dynamicFunction2 == dynamicFunction);
}