Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2384. Fix not well-bound extension types. Add function-type dynamic test #2387

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 At run time, for a given instance o typed as an extension type V,
/// there is no reification of V associated with o.
///
/// @description Check that an extension type is 'erased' at run time
/// @author [email protected]

// SharedOptions=--enable-experiment=inline-class

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

extension type ET1(int id) {
int add(int other) => id + other;
}

extension type ET2<T>(T id) {
bool equal(T other) => id == other;
}

main() {
dynamic d1 = ET1(1);
Expect.throws(() {d1.id;});
Expect.throws(() {d1.add(1);});
dynamic d2 = ET2(1);
Expect.throws(() {d2.id;});
Expect.throws(() {d2.equal(1);});
dynamic d3 = ET2<int>(1);
Expect.throws(() {d3.id;});
Expect.throws(() {d3.equal(1);});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 Consider an invocation of the extension type member m on the
/// receiver expression e according to the extension type declaration V with
/// actual type arguments T1, ..., Ts. If the invocation includes an actual
/// argument part (possibly including some actual type arguments) then call it
/// args. If the invocation omits args, but includes a list of actual type
/// arguments then call them typeArgs. Assume that V declares the type variables
/// X1, ..., Xs
/// ...
/// Let Dm be the unique declaration named m that V has.
///
/// Evaluation of this invocation proceeds by evaluating e to an object o.
/// ...
/// Otherwise, the following is known: args is included, and Dm is a method. The
/// invocation proceeds to evaluate args to an actual argument list args1. Then
/// it executes the body of Dm in an environment where this and the name of the
/// representation are bound in the same way as in the getter invocation, the
/// type variables of V are bound to the actual values of T1, .. Ts, and the
/// formal parameters of m are bound to args1 in the same way that they would be
/// bound for a normal function call. If the body completes returning an object
/// o2 then the invocation evaluates to o2. If the body throws an object and a
/// stack trace then the invocation completes throwing the same object and stack
/// trace.
///
/// @description Check invocation of a representation type `call` member
/// @author [email protected]

// SharedOptions=--enable-experiment=inline-class

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

extension type V1<T extends Object Function(num)>(T id) {}

typedef num Foo(Object o);

Foo foo = (Object o) => o as num;

main() {
Expect.equals(42, V1<Foo>(foo).id(42));
Expect.equals(42, V1<Foo>(foo).id.call(42));
dynamic d = V1<Foo>(foo);
Expect.equals(42, d(42));
Expect.equals(42, d.call(42));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 Consider an invocation of the extension type member m on the
/// receiver expression e according to the extension type declaration V with
/// actual type arguments T1, ..., Ts. If the invocation includes an actual
/// argument part (possibly including some actual type arguments) then call it
/// args. If the invocation omits args, but includes a list of actual type
/// arguments then call them typeArgs. Assume that V declares the type variables
/// X1, ..., Xs
/// ...
/// Let Dm be the unique declaration named m that V has.
///
/// Evaluation of this invocation proceeds by evaluating e to an object o.
/// ...
/// Otherwise, the following is known: args is included, and Dm is a method. The
/// invocation proceeds to evaluate args to an actual argument list args1. Then
/// it executes the body of Dm in an environment where this and the name of the
/// representation are bound in the same way as in the getter invocation, the
/// type variables of V are bound to the actual values of T1, .. Ts, and the
/// formal parameters of m are bound to args1 in the same way that they would be
/// bound for a normal function call. If the body completes returning an object
/// o2 then the invocation evaluates to o2. If the body throws an object and a
/// stack trace then the invocation completes throwing the same object and stack
/// trace.
///
/// @description Check invocation of an extension type `call` member
/// @author [email protected]

// SharedOptions=--enable-experiment=inline-class

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

extension type ET1(int id) {
int call(int other) => id + other;
}

extension type ET2<T>(T id) {
T call() => id;
}

main() {
Expect.equals(3, ET1(1).call(2));
Expect.equals(5, ET1(2)(3));
Expect.equals("42", ET2<String>("42").call());
Expect.equals("42", ET2<String>("42")());
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
/// regular-bounded.
///
/// @description Checks that it is a compile-time error if the type
/// `V<T1, .. Ts>` is not regular-bounded.
/// `V<T1, .. Ts>` has type bound like V<T extends V<T>> (after extension type
/// erasure it become `extension type V<T extends T>(T id) {}` which is an
/// error)
/// @author [email protected]
/// @issue 54097

// SharedOptions=--enable-experiment=inline-class

extension type V<T extends V<T>>(T id) {}

main() {
List<V> l = [];
// ^
// ^
// [analyzer] unspecified
// [cfe] unspecified

main() {
print(V<Never>);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 Assume that T1, .. Ts are types, and V resolves to an extension
/// type declaration of the following form:
///
/// extension type V<X1 extends B1, .. Xs extends Bs>(T id) ... {
/// ... // Members.
/// }
/// ...
/// A compile-time error occurs if the type V<T1, .. Tk> provides a wrong number
/// of type arguments to V (when k is different from s), and if it is not
/// regular-bounded.
///
/// @description Checks that it is a compile-time error if an extension type
/// `V<T1, .. Ts>` has any dependency cycles in type bounds
/// @author [email protected]
/// @issue 54097

// SharedOptions=--enable-experiment=inline-class

extension type V1<T extends T>(T id) {}
// ^
// [analyzer] unspecified
// [cfe] unspecified

extension type V2<T1 extends T2, T2 extends T1>(T1 id) {}
// ^
// [analyzer] unspecified
// [cfe] unspecified

main() {
print(V1<Never>);
print(V2<Never, Never>);
}