Add tests for issue 53625. Tests valid and invalid extension type representation type and representation object reference declarations. Bug: https://dartbug.com/53625 Change-Id: I56b61e4c7e691db975da286b31a93a0635257088 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328160 Commit-Queue: Lasse Nielsen <lrn@google.com> Reviewed-by: Erik Ernst <eernst@google.com>
diff --git a/tests/language/extension_type/extension_type_representation_type_test.dart b/tests/language/extension_type/extension_type_representation_type_test.dart new file mode 100644 index 0000000..b034ff2 --- /dev/null +++ b/tests/language/extension_type/extension_type_representation_type_test.dart
@@ -0,0 +1,141 @@ +// 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. + +// SharedOptions=--enable-experiment=inline-class + +import "dart:async" show FutureOr; + +// Extension type declarations must have a "representation declaration" +// of the form: '(' <metadata> <type> <identifier> ')' +// +// Any type term is allowed. + +// Any special, or semi-special, type is allowed. +extension type V01(dynamic _) {} +extension type V02(void _) {} +extension type V03(Never _) {} +extension type V04(Null _) {} +extension type V05(Function _) {} +extension type V06(Record _) {} +extension type V07(Type _) {} +extension type V08(Object? _) {} +extension type V09(FutureOr<int> _) {} +extension type V10(FutureOr<int>? _) {} + +// Interface types. +extension type V11(List<int> _) {} +extension type V12(IType _) {} +extension type V13(FType _) {} +extension type V14(SType _) {} +extension type V15(MType _) {} +extension type V16(EType _) {} + +// Extension types. +extension type V17(ExtType _) {} + +// Record types. +extension type V18(() _) {} +extension type V19((int,) _) {} +extension type V20(({int x}) _) {} +extension type V21((int, String) _) {} +extension type V22((int, {String x}) _) {} +extension type V23(({int x, String y}) _) {} + +// Function types +extension type V24(Function() _) {} +extension type V25(void Function() _) {} +extension type V26(void Function(int) _) {} +extension type V27(void Function(int x) _) {} +extension type V28(void Function(int, String) _) {} +extension type V29(void Function(int, [String]) _) {} +extension type V30(void Function([int, String]) _) {} +extension type V31(void Function(int, {String y}) _) {} +extension type V32(void Function({int x, String y}) _) {} +extension type V33(void Function(int, {required String y}) _) {} +extension type V34(void Function({required int x, String y}) _) {} +extension type V35(Function Function(Function) Function() _) {} + +// Type variables +extension type V36<T>(T _) {} +extension type V37<T>(List<T> _) {} +extension type V38<T>(FutureOr<T?>? _) {} + +// Type aliases +extension type V39(AType _) {} +extension type V40(A<IType> _) {} +extension type V41(A<FType> _) {} +extension type V42(A<SType> _) {} +extension type V43(A<MType> _) {} +extension type V44(A<EType> _) {} +extension type V45(A<ExtType> _) {} + +// And can be created. +void main() { + V01(1)._; + V02(1)._; + try { + V03(0 as Never)._; + } on Error { + // Expected! + } + V04(null)._; + V05(() {})._; + V06(())._; + V07(int)._; + V08(1)._; + V09(1)._; + V10(1)._; + V11([])._; + V12(instance)._; + V13(instance)._; + V14(instance)._; + V15(instance)._; + V16(instance)._; + V17(ExtType(int))._; + V18(())._; + V19((1,))._; + V20((x: 1))._; + V21((1, "2"))._; + V22((1, x: "2"))._; + V23((x: 1, y: "2"))._; + V24(() {})._; + V25(() {})._; + V26((int x) {})._; + V27((int x) {})._; + V28((int x, String y) {})._; + V29((int x, [String y = "0"]) {})._; + V30(([int x = 0, String y = "0"]) {})._; + V31((int x, {String y = "0"}) {})._; + V32(({int x = 0, String y = "0"}) {})._; + V33((int x, {required String y}) {})._; + V34(({required int x, String y = "0"}) {})._; + V35(() => (Function f) => f)._; + V36<Type>(int)._; + V37<Type>([int])._; + V38<Type>(int)._; + V39(int)._; + V40(instance)._; + V41(instance)._; + V42(instance)._; + V43(instance)._; + V44(instance)._; + V45(ExtType(int))._; +} + +// Helpers. +extension type ExtType(Type x) implements Type {} + +abstract interface class IType implements Type {} + +mixin MType implements Type {} + +sealed class SType with MType {} + +final class FType extends SType implements IType {} + +enum EType implements FType { e } + +typedef A<X> = X; +typedef AType = Type; +const instance = EType.e;
diff --git a/tests/language/extension_type/regress_53625_error_test.dart b/tests/language/extension_type/regress_53625_error_test.dart new file mode 100644 index 0000000..55d09e9 --- /dev/null +++ b/tests/language/extension_type/regress_53625_error_test.dart
@@ -0,0 +1,183 @@ +// 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. + +// SharedOptions=--enable-experiment=inline-class + +// Regression check for https://dartbug.com/53625 +// +// Extension type declarations must have a "representation declaration" +// of the form: '(' <metadata> <type> <identifier> ')' +// where `<metadata>` can be empty, the other two not. +// +// Any other format is (currently) disallowed. + + +// The "representation declaration" is like the parameter list of +// an implicit constructor. +// It still does not accept any other parameter list shape than the above. + +extension type E00(int) {} +// ^^^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_TYPE +// ^ +// [cfe] unspecified + +extension type E01(int x,) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + +extension type E02(final int x) {} +// ^^^^^ +// [analyzer] SYNTACTIC_ERROR.REPRESENTATION_FIELD_MODIFIER +// [cfe] unspecified + +extension type E03(var x) {} +// ^^^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_TYPE +// [analyzer] SYNTACTIC_ERROR.REPRESENTATION_FIELD_MODIFIER +// [cfe] unspecified + +extension type E04(final x) {} +// ^^^^^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_TYPE +// [analyzer] SYNTACTIC_ERROR.REPRESENTATION_FIELD_MODIFIER +// [cfe] unspecified + +extension type E05(covariant int x) {} +// ^ +// [cfe] unspecified + +extension type E06(required int x) {} +// ^^^^^^^^ +// [analyzer] SYNTACTIC_ERROR.REPRESENTATION_FIELD_MODIFIER +// [cfe] unspecified + +extension type E07(int this.x) {} // Initializing formal. +// ^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + +extension type E08(this.x) {} // Initializing formal. +// ^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + +extension type E09(int super.x) implements E {} // Constructor super-parameter. +// ^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + +extension type E10(super.x) implements E {} // Constructor super-parameter. +// ^^^^^^^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_FIELD +// [cfe] unspecified + +extension type E11(int x()) {} // Old-style function parameter syntax. +// ^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + +// The "primary parameter" declares a "field", +// but still does not accept field modifiers or initializers. + +extension type E12(late int x) {} +// ^^^^ +// [analyzer] SYNTACTIC_ERROR.REPRESENTATION_FIELD_MODIFIER +// [cfe] unspecified + +extension type E13(int x = 0) {} +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified + +extension type E14(static int x) {} +// ^^^^^^ +// [analyzer] SYNTACTIC_ERROR.REPRESENTATION_FIELD_MODIFIER +// [cfe] unspecified + +extension type const E15(const int x) {} +// ^^^^^ +// [analyzer] SYNTACTIC_ERROR.REPRESENTATION_FIELD_MODIFIER +// [cfe] unspecified + +// Precisely one parameter is allowed and required. + +extension type E16() {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_FIELD +// [cfe] unspecified + +extension type E17(int x, String y) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.MULTIPLE_REPRESENTATION_FIELDS +// [cfe] unspecified + +extension type E18(int x, [String y = "2"]) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.MULTIPLE_REPRESENTATION_FIELDS +// [cfe] unspecified + +extension type E19(int x, {required String y}) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.MULTIPLE_REPRESENTATION_FIELDS +// [cfe] unspecified + +extension type E20(int x, {String y = "2"}) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.MULTIPLE_REPRESENTATION_FIELDS +// [cfe] unspecified + +extension type E21([int x = 0]) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_FIELD +// [cfe] unspecified + +extension type E22([int x = 0, int y = 0]) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_FIELD +// [cfe] unspecified + +extension type E23({required int x}) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_FIELD +// [cfe] unspecified + +extension type E24({int x = 0}) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_FIELD +// [cfe] unspecified + +extension type E25({int x = 0, int y = 0}) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_FIELD +// [cfe] unspecified + +// Annotations are allowed, but only at the start. + +extension type E26(@anno int) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_TYPE +// ^ +// [cfe] unspecified + +extension type E27(int @anno x) {} +// ^^^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_REPRESENTATION_TYPE +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN +// [cfe] unspecified + +extension type E28(int x @anno) {} +// ^ +// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN +// [cfe] unspecified + + +// Helpers +const anno = "Annotation"; + +extension type E(int x) {} + +void main() {}