Skip to content

Commit f500a37

Browse files
authored
#2142. Add syntax tests (#2148)
Add syntax tests for extension type declarations.
1 parent 1530f9b commit f500a37

22 files changed

+890
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2023, 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+
/// @description Helper library for testing extension types
6+
/// @author sgrekhov22@gmail.com
7+
8+
// SharedOptions=--enable-experiment=inline-class
9+
10+
library extension_type_lib;
11+
12+
int x = 42;
13+
14+
extension type _PrivateExtensionType(int id) {
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// The token `type` is not made a built-in identifier: the built-in identifier
23+
/// extension that occurs right before type serves to disambiguate the extension
24+
/// type declaration with a fixed lookahead.
25+
///
26+
/// @description Checks that it is not an error to declare a class named `type`
27+
/// @author sgrekhov22@gmail.com
28+
29+
// SharedOptions=--enable-experiment=inline-class
30+
31+
class type {
32+
}
33+
34+
main() {
35+
type();
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// The token `type` is not made a built-in identifier: the built-in identifier
23+
/// extension that occurs right before type serves to disambiguate the extension
24+
/// type declaration with a fixed lookahead.
25+
///
26+
/// @description Checks that it is not an error to declare a mixin named `type`
27+
/// @author sgrekhov22@gmail.com
28+
29+
// SharedOptions=--enable-experiment=inline-class
30+
31+
mixin type on Object {
32+
}
33+
34+
class IA = Object with type;
35+
36+
main() {
37+
IA();
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// The token `type` is not made a built-in identifier: the built-in identifier
23+
/// extension that occurs right before type serves to disambiguate the extension
24+
/// type declaration with a fixed lookahead.
25+
///
26+
/// @description Checks that it is not an error to import a library with
27+
/// import prefix `type`
28+
/// @author sgrekhov22@gmail.com
29+
30+
// SharedOptions=--enable-experiment=inline-class
31+
32+
import "extension_type_lib.dart" as type;
33+
34+
main() {
35+
print(type.x);
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// The token `type` is not made a built-in identifier: the built-in identifier
23+
/// extension that occurs right before type serves to disambiguate the extension
24+
/// type declaration with a fixed lookahead.
25+
///
26+
/// @description Checks that it is not an error to declare a type alias named
27+
/// `type`
28+
/// @author sgrekhov22@gmail.com
29+
30+
// SharedOptions=--enable-experiment=inline-class
31+
32+
typedef type = String;
33+
34+
main() {
35+
type s = "type";
36+
s.substring(1);
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// @description Checks that it is a compile-time error if an extension type
23+
/// declaration doesn't contain `typeIdentifier`
24+
/// @author sgrekhov22@gmail.com
25+
26+
// SharedOptions=--enable-experiment=inline-class
27+
28+
extension type (int id) {
29+
// ^
30+
// [analyzer] unspecified
31+
// [cfe] unspecified
32+
}
33+
34+
main() {
35+
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// @description Checks that it is a compile-time error to refer an extension
23+
/// type with the `typeIdentifier` started with `_` outside of the library where
24+
/// it is defined
25+
/// @author sgrekhov22@gmail.com
26+
27+
// SharedOptions=--enable-experiment=inline-class
28+
29+
main() {
30+
print(_PrivateExtensionType);
31+
// ^^^^^^^^^^^^^^^^^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// @description Checks that an extension type declaration may have type
23+
/// parameters
24+
/// @author sgrekhov22@gmail.com
25+
26+
// SharedOptions=--enable-experiment=inline-class
27+
28+
import "../../Utils/expect.dart";
29+
30+
extension type ET<T>(T id) {}
31+
32+
main() {
33+
ET<int> et1 = ET<int>(42);
34+
Expect.equals(42, et1.id);
35+
36+
ET<String> et2 = ET<String>.new("42");
37+
Expect.equals("42", et2.id);
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// @description Checks that it is a compile-time error if an extension type
23+
/// declaration doesn't contain `representationDeclaration`
24+
/// @author sgrekhov22@gmail.com
25+
26+
// SharedOptions=--enable-experiment=inline-class
27+
28+
extension type ET {
29+
// ^^
30+
// [analyzer] unspecified
31+
// [cfe] unspecified
32+
}
33+
34+
main() {
35+
print(ET);
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// @description Checks that a `representationDeclaration` may have form
23+
/// `identifier`.`identifier`...
24+
/// @author sgrekhov22@gmail.com
25+
26+
// SharedOptions=--enable-experiment=inline-class
27+
28+
import "../../Utils/expect.dart";
29+
30+
extension type ET.et(int id) {}
31+
32+
main() {
33+
ET et = ET.et(42);
34+
Expect.equals(42, et.id);
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2023, 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 A rule for <extensionTypeDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in extension type declarations:
7+
///
8+
/// <extensionTypeDeclaration> ::=
9+
/// 'extension' 'type' 'const'? <typeIdentifier> <typeParameters>?
10+
/// <representationDeclaration> <interfaces>?
11+
/// '{'
12+
/// (<metadata> <extensionTypeMemberDeclaration>)*
13+
/// '}'
14+
///
15+
/// <representationDeclaration> ::=
16+
/// ('.' <identifierOrNew>)? '(' <metadata> <type> <identifier> ')'
17+
///
18+
/// <identifierOrNew> ::= <identifier> | 'new'
19+
///
20+
/// <extensionTypeMemberDeclaration> ::= <classMemberDefinition>
21+
///
22+
/// @description Checks that a `representationDeclaration` may have form
23+
/// `identifier`.new
24+
/// @author sgrekhov22@gmail.com
25+
26+
// SharedOptions=--enable-experiment=inline-class
27+
28+
import "../../Utils/expect.dart";
29+
30+
extension type ET.new(int id) {}
31+
32+
main() {
33+
ET et1 = ET(1);
34+
Expect.equals(1, et1.id);
35+
36+
ET et2 = ET.new(2);
37+
Expect.equals(2, et2.id);
38+
}

0 commit comments

Comments
 (0)