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

#1401. Calculating the static type of the pattern tests added #1847

Merged
merged 7 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
61 changes: 61 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A11_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check the calculation static type of a logical-and pattern.
/// Test that missing types in a type schema are filled in from the initializing
/// expression
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

import "../../Utils/static_type_helper.dart";

class A {}
class B extends A {}
class C extends B {}
class D extends C {}

main() {
A a = A();
B b = B();
C c = C();
D d = D();
{
// It's important to specify type arguments of the initializing expression
// here and below to avoid a type inference from the pattern type schema to
// a initializing expression
var ([A x1, x2] && [y1, B y2]) = <B>[b, c];
x1.expectStaticType<Exactly<A>>();
x2.expectStaticType<Exactly<B>>();
y1.expectStaticType<Exactly<B>>();
y2.expectStaticType<Exactly<B>>();
}
{
final ([A x1, x2] && [y1, B y2]) = <C>[c, d];
x1.expectStaticType<Exactly<A>>();
x2.expectStaticType<Exactly<C>>();
y1.expectStaticType<Exactly<C>>();
y2.expectStaticType<Exactly<B>>();
}
{
var ([A x1, x2] && [y1, B y2]) = <B>[b, b];
x1.expectStaticType<Exactly<A>>();
x2.expectStaticType<Exactly<B>>();
y1.expectStaticType<Exactly<B>>();
y2.expectStaticType<Exactly<B>>();
}
{
final ([A x1, x2] && [y1, B y2]) = <C>[d, d];
x1.expectStaticType<Exactly<A>>();
x2.expectStaticType<Exactly<C>>();
y1.expectStaticType<Exactly<C>>();
y2.expectStaticType<Exactly<B>>();
}
}
26 changes: 26 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A11_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation of the static type of a logical-and
/// pattern inserts implicit coercions
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

main() {
var ([double v1] && [num v2]) = [42];
v1.expectStaticType<Exactly<double>>();
v2.expectStaticType<Exactly<num>>();
Expect.identical(42.0, v1);
Expect.identical(42.0, v2);
}
37 changes: 37 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A11_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation of the static type of a logical-and
/// pattern performs casts from dynamic and generic function instantiation
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

T foo<T>(T t) => t;

main() {
dynamic pi = 3.14;
var ([double v1] && [num v2]) = [pi];
v1.expectStaticType<Exactly<double>>();
v2.expectStaticType<Exactly<num>>();
Expect.equals(3.14, v1);
Expect.equals(3.14, v2);

Expect.throws(() {
final ([int v3] && [num v4]) = [pi];
});

var ([int Function(int) v5] && [num Function(num) v6]) = [foo];
v5.expectStaticType<Exactly<int Function(int)>>();
v6.expectStaticType<Exactly<num Function(num)>>();
}
24 changes: 24 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A12_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation of the static type of a null-assert
/// pattern inserts implicit coercions
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

main() {
var [double? v1!] = [42];
v1.expectStaticType<Exactly<double>>();
Expect.identical(42.0, v1);
}
28 changes: 28 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A12_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation og the static type of a null-assert
/// pattern performs casts from dynamic
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

main() {
dynamic pi = 3.14;
final (double? v1!) = pi;
v1.expectStaticType<Exactly<double?>>();

Expect.throws(() {
var (int? v2!) = pi as dynamic;
});
}
37 changes: 37 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A13_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check the static type of a variable pattern. Test that missing
/// types in a type schema are filled in from the initializing expression
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

main() {
var (v1) = 42;
v1.expectStaticType<Exactly<int>>();

final (v2) = [42];
v2.expectStaticType<Exactly<List<int>>>();

Object? o = Object();
if (1 > 2) {
o = null;
}
var (v3) = o;
v3.expectStaticType<Exactly<Object?>>();

o = null;
final (v4) = o;
v4.expectStaticType<Exactly<Object?>>();
}
28 changes: 28 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A13_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation of the static type of a variable
/// pattern performs implicit coercions
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

main() {
var (double v1) = 42;
v1.expectStaticType<Exactly<double>>();
Expect.identical(42.0, v1);

final (double v2) = 42;
v2.expectStaticType<Exactly<double>>();
Expect.identical(42.0, v2);
}
34 changes: 34 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A13_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation of the static type of a variable
/// pattern performs casts from dynamic and generic function instantiation
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

T foo<T>(T t) => t;

main() {
dynamic pi = 3.14;
final (double v1) = pi;
v1.expectStaticType<Exactly<double>>();
Expect.equals(3.14, v1);

Expect.throws(() {
var (int v2) = pi as dynamic;
});

final (int Function(int) v3) = foo;
v3.expectStaticType<Exactly<int Function(int)>>();
}
38 changes: 38 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A14_t01.dart
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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check the static type of an identifier pattern. Test that
/// missing types in a type schema are filled in from the initializing
/// expression
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/static_type_helper.dart";

main() {
var (v1) = 42;
v1.expectStaticType<Exactly<int>>();

final (v2) = [42];
v2.expectStaticType<Exactly<List<int>>>();

var (v3,) = (1 as int?,);
v3.expectStaticType<Exactly<int?>>();

var (v4,) = (1 as num,);
v4.expectStaticType<Exactly<num>>();

final (v5,) = ("String",);
v5.expectStaticType<Exactly<String>>();

var (v6,) = ("String" as Object?,);
v6.expectStaticType<Exactly<Object?>>();
}
23 changes: 23 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A14_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation of the static type of an identifier
/// pattern performs implicit coercions
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

main() {
double v1 = 0;
[v1] = [42];
Expect.identical(42.0, v1);
}
36 changes: 36 additions & 0 deletions LanguageFeatures/Patterns/type_inference_A14_t03.dart
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 Calculate the static type of the pattern. Using that value type,
/// recurse through the pattern again downwards to the leaf subpatterns filling
/// in any missing types in the pattern. This process may also insert implicit
/// coercions and casts from dynamic when values flow into a pattern during
/// matching.
///
/// @description Check that the calculation of the static type of an identifier
/// pattern performs casts from dynamic and generic function instantiation
/// @author [email protected]

// SharedOptions=--enable-experiment=patterns

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

T foo<T>(T t) => t;

main() {
dynamic pi = 3.14;
double v1 = 0.1;
(v1) = pi;
v1.expectStaticType<Exactly<double>>();

Expect.throws(() {
int v2 = 0;
(v2) = pi;
});

final int Function(int) v3;
(v3) = foo;
v3.expectStaticType<Exactly<int Function(int)>>();
}
Loading