Skip to content

Commit

Permalink
dart-lang#1400. Add object pattern tests for extension types
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Dec 28, 2023
1 parent 89ec72c commit a4f8f9c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023, 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.

/// @assertion An extension type V (which may include actual type arguments) can
/// be used in an object pattern (e.g., case V(): ... where V is an extension
/// type). Exhaustiveness analysis will treat such patterns as if they had been
/// an object pattern matching the extension type erasure of V.
///
/// @description Check that an extension type can be used in an object pattern
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

import "../../Utils/expect.dart";

class C {
int x;
C(this.x);
}

extension type ET1(C id) {}
extension type ET2(C id) implements C {}

main() {
var ET1() = ET1(C(0));
var ET2(x: v1) = ET2(C(1));
Expect.equals(1, v1);

var C(x: v2) = ET2(C(2));
Expect.equals(2, v2);

var ET1(id: v3) = ET1(C(3));
Expect.equals(3, v3.x);

var ET2(id: v4) = ET2(C(4));
Expect.equals(4, v4.x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023, 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.

/// @assertion An extension type V (which may include actual type arguments) can
/// be used in an object pattern (e.g., case V(): ... where V is an extension
/// type). Exhaustiveness analysis will treat such patterns as if they had been
/// an object pattern matching the extension type erasure of V.
///
/// @description Check that an extension type can be used in an object pattern.
/// Test generics
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

import "../../Utils/expect.dart";

class C<T> {
T x;
C(this.x);
}

extension type ET1<T>(C<T> id) {}
extension type ET2<T>(C<T> id) implements C<T> {}

main() {
var ET1<num>() = ET1(C(3.14));
var ET2<int>(x: v1) = ET2<int>(C(1));
Expect.equals(1, v1);

var C<String>(x: v2) = ET2<String>(C<String>("2"));
Expect.equals("2", v2);

var ET1<String>(id: v3) = ET1(C<String>("3"));
Expect.equals("3", v3.x);

var ET2<int>(id: v4) = ET2<int>(C(4));
Expect.equals(4, v4.x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2023, 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.

/// @assertion An extension type V (which may include actual type arguments) can
/// be used in an object pattern (e.g., case V(): ... where V is an extension
/// type). Exhaustiveness analysis will treat such patterns as if they had been
/// an object pattern matching the extension type erasure of V.
///
/// @description Check that an extension type erasure is not used in an object
/// pattern outside of switch statements and expressions
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

class C {
int x;
C(this.x);
}

extension type ET1(C id) {}
extension type ET2(C id) {}
extension type ET3(C id) implements C {}

main() {
var C(x: v1) = ET1(C(1));
// ^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
var ET1(x: v2) = ET1(C(2));
// ^
// [analyzer] unspecified
// [cfe] unspecified
var ET1() = C(42);
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
var ET1() = ET2(C(42));
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
var ET3(x: v3) = C(42);
// ^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
var ET3(x: v4) = ET1(C(42));
// ^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

0 comments on commit a4f8f9c

Please sign in to comment.