diff --git a/pkg/compiler/test/inference/data/issue_48571.dart b/pkg/compiler/test/inference/data/issue_48571.dart new file mode 100644 index 00000000000..6bfaefde58a --- /dev/null +++ b/pkg/compiler/test/inference/data/issue_48571.dart @@ -0,0 +1,83 @@ +// Copyright (c) 2022, 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. + +/*member: Base.:[subclass=Base]*/ +abstract class Base {} + +/*member: Child1.:[exact=Child1]*/ +class Child1 extends Base {} + +/*member: Child2.:[exact=Child2]*/ +class Child2 extends Base {} + +/*member: trivial:Value([exact=JSBool], value: true)*/ +bool trivial(/*[exact=JSBool]*/ x) => true; + +/*member: either:Union([exact=Child1], [exact=Child2])*/ +Base either = DateTime.now() + . /*[exact=DateTime]*/ millisecondsSinceEpoch /*invoke: [subclass=JSInt]*/ > + 0 + ? Child2() + : Child1(); + +/*member: test1:[null|exact=Child1]*/ +test1() { + Base child = either; + if (trivial(child is Child1 && true)) return child; + return null; +} + +/*member: test2:[null|exact=Child1]*/ +test2() { + Base child = either; + if (child is Child1 || trivial(child is Child1 && true)) return child; + return null; +} + +/*member: test3:[null]*/ +test3() { + Base child = either; + if (trivial(child is Child1 && true) && child is Child2) return child; + return null; +} + +/*member: test4:[null]*/ +test4() { + Base child = either; + if (child is Child2 && trivial(child is Child1 && true)) return child; + return null; +} + +/*member: test5:[null|exact=Child1]*/ +test5() { + Base child = either; + if ((child is Child1 && true) /*invoke: [exact=JSBool]*/ == false) + return child; + return null; +} + +/*member: test6:Union(null, [exact=Child1], [exact=Child2])*/ +test6() { + Base child = either; + if (trivial(child is Child1 ? false : true)) return child; + return null; +} + +/*member: test7:[null|exact=Child1]*/ +test7() { + Base child = either; + if (trivial(trivial(child is Child1 && true))) return child; + return null; +} + +/*member: main:[null]*/ +main() { + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + test7(); +} diff --git a/tests/web/regress/issue/48571_test.dart b/tests/web/regress/issue/48571_test.dart new file mode 100644 index 00000000000..9a15a70df33 --- /dev/null +++ b/tests/web/regress/issue/48571_test.dart @@ -0,0 +1,66 @@ +// Copyright (c) 2022, 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'; + +abstract class Base {} + +class Child1 extends Base {} + +class Child2 extends Base {} + +bool trivial(x) => true; +Base either = DateTime.now().millisecondsSinceEpoch > 0 ? Child2() : Child1(); + +test1() { + Base child = either; + if (trivial(child is Child1 && true)) return child; + return null; +} + +test2() { + Base child = either; + if (child is Child1 || trivial(child is Child1 && true)) return child; + return null; +} + +test3() { + Base child = either; + if (trivial(child is Child1 && true) && child is Child2) return child; + return null; +} + +test4() { + Base child = either; + if (child is Child2 && trivial(child is Child1 && true)) return child; + return null; +} + +test5() { + Base child = either; + if ((child is Child1 && true) == false) return child; + return null; +} + +test6() { + Base child = either; + if (trivial(child is Child1 ? false : true)) return child; + return null; +} + +test7() { + Base child = either; + if (trivial(trivial(child is Child1 && true))) return child; + return null; +} + +main() { + Expect.isTrue(test1() is Child2, "test1"); + Expect.isTrue(test2() is Child2, "test2"); + Expect.isTrue(test3() is Child2, "test3"); + Expect.isTrue(test4() is Child2, "test4"); + Expect.isTrue(test5() is Child2, "test5"); + Expect.isTrue(test6() is Child2, "test6"); + Expect.isTrue(test7() is Child2, "test7"); +} diff --git a/tests/web_2/regress/issue/48571_test.dart b/tests/web_2/regress/issue/48571_test.dart new file mode 100644 index 00000000000..d724ffa9477 --- /dev/null +++ b/tests/web_2/regress/issue/48571_test.dart @@ -0,0 +1,68 @@ +// Copyright (c) 2022, 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:expect/expect.dart'; + +abstract class Base {} + +class Child1 extends Base {} + +class Child2 extends Base {} + +bool trivial(x) => true; +Base either = DateTime.now().millisecondsSinceEpoch > 0 ? Child2() : Child1(); + +test1() { + Base child = either; + if (trivial(child is Child1 && true)) return child; + return null; +} + +test2() { + Base child = either; + if (child is Child1 || trivial(child is Child1 && true)) return child; + return null; +} + +test3() { + Base child = either; + if (trivial(child is Child1 && true) && child is Child2) return child; + return null; +} + +test4() { + Base child = either; + if (child is Child2 && trivial(child is Child1 && true)) return child; + return null; +} + +test5() { + Base child = either; + if ((child is Child1 && true) == false) return child; + return null; +} + +test6() { + Base child = either; + if (trivial(child is Child1 ? false : true)) return child; + return null; +} + +test7() { + Base child = either; + if (trivial(trivial(child is Child1 && true))) return child; + return null; +} + +main() { + Expect.isTrue(test1() is Child2, "test1"); + Expect.isTrue(test2() is Child2, "test2"); + Expect.isTrue(test3() is Child2, "test3"); + Expect.isTrue(test4() is Child2, "test4"); + Expect.isTrue(test5() is Child2, "test5"); + Expect.isTrue(test6() is Child2, "test6"); + Expect.isTrue(test7() is Child2, "test7"); +}