|
| 1 | +// Copyright (c) 2024, 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 | +/// @assertion This new syntax also introduces new ambiguities in the grammar, |
| 6 | +/// similar to the one we introduced with generic functions. Examples include: |
| 7 | +/// |
| 8 | +/// f(a<b,c>(d)); // Existing ambiguity, resolved to a generic method call. |
| 9 | +/// f(x.a<b,c>[d]); // f((x.a<b, c>)[d]) or f((x.a < b), (c > [d])) |
| 10 | +/// f(x.a<b,c>-d); // f((x.a<b, c>)-d) or f((x.a < b), (c > -d])) |
| 11 | +/// The x.a<b,c> can be an explicitly instantiated generic function tear-off or |
| 12 | +/// an explicitly instantiated type literal named using a prefix, which is new. |
| 13 | +/// While neither type objects nor functions declare operator- or operator[], |
| 14 | +/// such could be added using extension methods. |
| 15 | +/// |
| 16 | +/// We will disambiguate such situations heuristically based on the token |
| 17 | +/// following the > that matches the < we are ambiguous about. In the existing |
| 18 | +/// ambiguity we treat ( as a sign that the < starts a generic invocation. We |
| 19 | +/// extend the number of tokens which, when following a potential type argument |
| 20 | +/// list, makes us choose to parse the previous tokens as that type argument |
| 21 | +/// list. |
| 22 | +/// |
| 23 | +/// There is a number of tokens which very consistently end an expression, and |
| 24 | +/// we include all those: |
| 25 | +/// |
| 26 | +/// ), }, ], ;, :, , |
| 27 | +/// |
| 28 | +/// Then we include tokens which we predict will continue a generic instantiation: |
| 29 | +/// |
| 30 | +/// ( . == != |
| 31 | +/// |
| 32 | +/// The first six are tokens which cannot possibly start an expression, and |
| 33 | +/// therefore cannot occur after a greater-than infix operator. The last four |
| 34 | +/// tokens can continue an expression, and of those only ( can also start an |
| 35 | +/// expression, and we already decided how to disambiguate that). |
| 36 | +/// |
| 37 | +/// There are many other tokens which currently cannot continue an expression |
| 38 | +/// (and therefore cannot validly follow a type argument list) or which cannot |
| 39 | +/// start an expression (and therefore cannot validly follow a greater-than |
| 40 | +/// operator), but in the service of keeping our future options open, we choose |
| 41 | +/// a design that does not rely on those restrictions. For example we omit most |
| 42 | +/// infix operators from being "continuation tokens", even though they currently |
| 43 | +/// cannot start a new expression, and therefore cannot follow a > infix |
| 44 | +/// operator. This leaves us open to allowing some of those operators as prefix |
| 45 | +/// operators in the future, like we currently allow the - operator. |
| 46 | +/// |
| 47 | +/// @description Checks that it is a syntax error if function with type argument |
| 48 | +/// specified is part of `as`, `as!` or `is` expressions |
| 49 | + |
| 50 | +
|
| 51 | +int bar<T>(T t) => 42; |
| 52 | +void foo<T1, T2, T3>() {} |
| 53 | + |
| 54 | +main() { |
| 55 | + const c1 = bar<int> as int Function(int); |
| 56 | +// ^^^ |
| 57 | +// [analyzer] unspecified |
| 58 | +// [cfe] unspecified |
| 59 | + |
| 60 | + const c2 = bar<int> is! int Function(int); |
| 61 | +// ^^^ |
| 62 | +// [analyzer] unspecified |
| 63 | +// [cfe] unspecified |
| 64 | + |
| 65 | + const c3 = bar<int> is int Function(int); |
| 66 | +// ^^^ |
| 67 | +// [analyzer] unspecified |
| 68 | +// [cfe] unspecified |
| 69 | + |
| 70 | + const c4 = foo<int, String, bool> as void Function(); |
| 71 | +// ^ |
| 72 | +// [analyzer] unspecified |
| 73 | +// [cfe] unspecified |
| 74 | + |
| 75 | + const c5 = foo<int, String, bool> is! void Function(); |
| 76 | +// ^ |
| 77 | +// [analyzer] unspecified |
| 78 | +// [cfe] unspecified |
| 79 | + |
| 80 | + const c6 = foo<int, String, bool> is void Function(); |
| 81 | +// ^ |
| 82 | +// [analyzer] unspecified |
| 83 | +// [cfe] unspecified |
| 84 | +} |
0 commit comments