|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// These tests verify that the proper context type is applied to default values |
| 6 | +// of closure parameters. |
| 7 | + |
| 8 | +import 'package:expect/expect.dart'; |
| 9 | +import '../static_type_helper.dart'; |
| 10 | + |
| 11 | +// If the closure has an optional positional parameter with an explicit type: |
| 12 | +// |
| 13 | +// - The explicit parameter type will be used as the type inference context for |
| 14 | +// the default value of the closure parameter |
| 15 | +void testPositionalExplicit() { |
| 16 | + var f = ([List<num> x = const []]) { |
| 17 | + x.expectStaticType<Exactly<List<num>>>(); |
| 18 | + Expect.type<List<num>>(x); |
| 19 | + Expect.identical(x, const <num>[]); |
| 20 | + }; |
| 21 | + f(); |
| 22 | +} |
| 23 | + |
| 24 | +// If the closure has an optional positional parameter with an implicit type, |
| 25 | +// and the closure's context is a function type with a required positional |
| 26 | +// parameter: |
| 27 | +// |
| 28 | +// - The type of the closure's parameter will be inferred from the |
| 29 | +// corresponding required parameter in the context type |
| 30 | +// - And that type in turn will be used as the type inference context for the |
| 31 | +// default value of the closure parameter. |
| 32 | +void testPositionalInferredFromRequired() { |
| 33 | + void Function(List<int>) f = ([x = const []]) { |
| 34 | + x.expectStaticType<Exactly<List<int>>>(); |
| 35 | + Expect.type<List<int>>(x); |
| 36 | + Expect.identical(x, const <int>[]); |
| 37 | + }; |
| 38 | + (f as void Function([List<int>]))(); |
| 39 | +} |
| 40 | + |
| 41 | +// If the closure has an optional positional parameter with an implicit type, |
| 42 | +// and the closure's context is a function type with an optional positional |
| 43 | +// parameter: |
| 44 | +// |
| 45 | +// - The type of the closure's parameter will be inferred from the |
| 46 | +// corresponding optional parameter in the context type |
| 47 | +// - And that type in turn will be used as the type inference context for the |
| 48 | +// default value of the closure parameter. |
| 49 | +void testPositionalInferredFromOptional() { |
| 50 | + void Function([List<int>]) f = ([x = const []]) { |
| 51 | + x.expectStaticType<Exactly<List<int>>>(); |
| 52 | + Expect.type<List<int>>(x); |
| 53 | + Expect.identical(x, const <int>[]); |
| 54 | + }; |
| 55 | + f(); |
| 56 | +} |
| 57 | + |
| 58 | +// If the closure has an optional named parameter with an explicit type: |
| 59 | +// |
| 60 | +// - The explicit parameter type will be used as the type inference context for |
| 61 | +// the default value of the closure parameter |
| 62 | +void testNamedExplicit() { |
| 63 | + var f = ({List<num> x = const []}) { |
| 64 | + x.expectStaticType<Exactly<List<num>>>(); |
| 65 | + Expect.type<List<num>>(x); |
| 66 | + Expect.identical(x, const <num>[]); |
| 67 | + }; |
| 68 | + f(); |
| 69 | +} |
| 70 | + |
| 71 | +// If the closure has an optional named parameter with an implicit type, and the |
| 72 | +// closure's context is a function type with a required named parameter: |
| 73 | +// |
| 74 | +// - The type of the closure's parameter will be inferred from the |
| 75 | +// corresponding required parameter in the context type |
| 76 | +// - And that type in turn will be used as the type inference context for the |
| 77 | +// default value of the closure parameter. |
| 78 | +void testNamedInferredFromRequired() { |
| 79 | + void Function({required List<int> x}) f = ({x = const []}) { |
| 80 | + x.expectStaticType<Exactly<List<int>>>(); |
| 81 | + Expect.type<List<int>>(x); |
| 82 | + Expect.identical(x, const <int>[]); |
| 83 | + }; |
| 84 | + (f as void Function({List<int> x}))(); |
| 85 | +} |
| 86 | + |
| 87 | +// If the closure has an optional named parameter with an implicit type, and the |
| 88 | +// closure's context is a function type with an optional named parameter: |
| 89 | +// |
| 90 | +// - The type of the closure's parameter will be inferred from the |
| 91 | +// corresponding optional parameter in the context type |
| 92 | +// - And that type in turn will be used as the type inference context for the |
| 93 | +// default value of the closure parameter. |
| 94 | +void testNamedInferredFromOptional() { |
| 95 | + void Function({List<int> x}) f = ({x = const []}) { |
| 96 | + x.expectStaticType<Exactly<List<int>>>(); |
| 97 | + Expect.type<List<int>>(x); |
| 98 | + Expect.identical(x, const <int>[]); |
| 99 | + }; |
| 100 | + f(); |
| 101 | +} |
| 102 | + |
| 103 | +main() { |
| 104 | + testPositionalExplicit(); |
| 105 | + testPositionalInferredFromRequired(); |
| 106 | + testPositionalInferredFromOptional(); |
| 107 | + testNamedExplicit(); |
| 108 | + testNamedInferredFromRequired(); |
| 109 | + testNamedInferredFromOptional(); |
| 110 | +} |
0 commit comments