diff --git a/tests/language/constructor/explicit_instantiation_syntax_test.dart b/tests/language/constructor/explicit_instantiation_syntax_test.dart new file mode 100644 index 00000000000..3b3ddd3b92f --- /dev/null +++ b/tests/language/constructor/explicit_instantiation_syntax_test.dart @@ -0,0 +1,299 @@ +// Copyright (c) 2021, 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. + +// SharedOptions=--enable-experiment=constructor-tearoffs + +import 'dart:core' hide dynamic; +import 'dart:core' as core show dynamic; + +extension E on X { + X operator <(_) => this; + X operator >(_) => this; + X operator <=(_) => this; + X operator >=(_) => this; + // X operator ==(_) => this; Member of object, can't be extension method. + X operator -(_) => this; + X operator +(_) => this; + X operator /(_) => this; + X operator ~/(_) => this; + X operator *(_) => this; + X operator %(_) => this; + X operator |(_) => this; + X operator ^(_) => this; + X operator &(_) => this; + X operator <<(_) => this; + X operator >>>(_) => this; + X operator >>(_) => this; + + X operator -() => this; + X operator ~() => this; + X operator [](_) => this; + operator []=(_1, _2) => this; +} + +class C { + C(); + C.named(); +} + +void f(_1, _2) {} +void g(_) {} +void h(_1, [_2]) {} + +int i = 0; + +class Test { + var x; + + void test() { + // Test the behavior of the parser on `id ...` where the `...` + // stands for each of the tokens in Dart. In each case, additional tokens + // are added at the end, if this can make the construct parseable. + // `f` is used to force the parse to be two expressions; `g` is used to + // force the parse to be one expression; `h` is used to allow both (in + // cases where both possibilities must be a dead end). + + f(C !true); + + g(C != 1); + + f(C """ """); + + f(C ""); + + // `#!` is not a symbol, there's no way to make this work. + h(C #!); //# 1: syntax error + + f(C #foo); + + h(C % 1); //# 2: syntax error + g((C) % 1); + + h(C %= 1); //# 3: syntax error + + h(true + C && true); //# 4: syntax error + g(true + (C) && true); + + h(C & 1); //# 5: syntax error + g((C) & 1); + + h(C &= 1); //# 6: syntax error + + g(C()); + + g(C); + + h(C * 1); //# 7: syntax error + g((C) * 1); + + h(C */ 1); //# 8: syntax error + + h(C *= 1); //# 9: syntax error + + h(C + 1); //# 10: syntax error + g((C) + 1); + + f(C ++ i); + + h(C += 1); //# 11: syntax error + + f(C , 1); + + f(C - 1); + + f(C -- i); + + h(C -= 1); //# 12: syntax error + + g(C . named()); + + g(C .. toString()); + + h(C ...); //# 13: syntax error + + h(C ...?); //# 14: syntax error + + h(C / 1); //# 15: syntax error + g((C) / 1); + + g(C /**/); //# 16: ok + f(C /**/ - 1); + + g(C // + ); + f(C // + - + 1); + + h(C /= 1); //# 17: syntax error + + g({C : 1}); + + C ; //# 18: ok + + h(C < 1); //# 19: syntax error + g((C) < 1); + + h(C << 1); //# 20: syntax error + g((C) << 1); + + h(C <<= 1); //# 21: syntax error + + h(C <= 1); //# 22: syntax error + g((C) <= 1); + + h(C = 1); //# 23: syntax error + + g(C == 1); + + h(C =>); //# 24: syntax error + + // The operator `>>` is a single token in the grammar. + h(C > 1); //# 25: syntax error + h((C) > 1); + + h(true + C ? 1 : 1); //# 26: syntax error + g(true + (C) ? 1 : 1); + + g(C ?. toString()); //# 27: syntax error + g((C) ?. toString()); //# 28: static type warning + + h(C ?.. toString()); //# 29: syntax error + + h(C ?? 1); //# 30: syntax error + g(null + (C) ?? 1); + + h(C ??= 1); //# 31: syntax error + + h(C @deprecated 1); //# 32: syntax error + + f(C [ 1 ]); + + f(C ''); + + f(C ''' '''); + + g([ C ]); + + h(C ^ 1); //# 33: syntax error + g((C) ^ 1); + + h(C ^= 1); //# 34: syntax error + + h(C | 1); //# 35: syntax error + g((C) | 1); + + h(C |= 1); //# 36: syntax error + + h(true + C || true); //# 37: syntax error + g(true + (C) || true); + + f(C ~ 1); + + h(C ~/ 1); //# 38: syntax error + g((C) ~/ 1); + + h(C ~/= 1); //# 39: syntax error + + f(C {}); + g({ C }); + + // Keywords with no special status. + { + var async, hide, of, on, show, sync; + f(C async); + f(C hide); + f(C of); + f(C on); + f(C show); + f(C sync); + } + + // Contextual reserved words (no special status here). + { + var await, yield; + f(C await); + f(C yield); + } + + // Built-in identifiers. + { + var abstract, as, covariant, deferred, dynamic, export; + var extension, external, factory, Function, get, implements; + var import, interface, late, library, mixin, operator, part; + var required, set, static, typedef; + f(C abstract); + f(C as); + g((C) as core.dynamic); + f(C covariant); + f(C deferred); + f(C dynamic); + f(C export); + f(C extension); + f(C external); + f(C factory); + f(C Function); + f(C get); + f(C implements); + f(C import); + f(C interface); + f(C late); + f(C library); + f(C mixin); + f(C operator); + f(C part); + f(C required); + f(C set); + f(C static); + f(C typedef); + } + + // Reserved words. + h(C assert(true)); //# 40: syntax error + switch (1) { + case 0: C break; //# 41: syntax error + C case 1: break; //# 42: syntax error + } + h(C catch); //# 43: syntax error + h(C class D {}); //# 44: syntax error + f(C const []); + while (++i < 10) { + C continue; //# 45: syntax error + } + h(C default); //# 46: syntax error + h(C do); //# 47: syntax error + h(C else); //# 48: syntax error + h(C enum {}); //# 49: syntax error + h(C extends C); //# 50: syntax error + f(C false); + h(C final); //# 51: syntax error + h(C finally); //# 52: syntax error + h(C for); //# 53: syntax error + h(C if); //# 54: syntax error + h(C in); //# 55: syntax error + h(C is Object); //# 56: syntax error + g((C) is Object); + f(C new C()); + f(C null); + h(C rethrow); //# 57: syntax error + h(C return); //# 58: syntax error + f(C super.toString); + h(C switch); //# 59: syntax error + f(C this.x); + + // Right operand of `>` is a ``, and `throw 0` isn't. + h(C throw 0); //# 60: syntax error + + f(C true); + h(C try); //# 61: syntax error + h(C var); //# 62: syntax error + h(C void); //# 63: syntax error + h(C while); //# 64: syntax error + h(C with); //# 65: syntax error + } +} + +void main() { + Test().test(); +}