Skip to content

Commit ddc85dd

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[fasta][nnbd] Create DartTypes with nullability from parsed types
Parsed types are auxiliary representation of types from the type test framework in CFE. Change-Id: Icd7f1aa4d0711d4a83813a8584ac8d1a5826f6c7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/105422 Reviewed-by: Johnni Winther <johnniwinther@google.com> Commit-Queue: Dmitry Stefantsov <dmitryas@google.com>
1 parent 133a577 commit ddc85dd

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

pkg/front_end/test/fasta/types/kernel_type_parser.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ class KernelFromParsedType implements Visitor<Node, KernelEnvironment> {
161161
} else if (kernelArguments.length != typeVariables.length) {
162162
throw "Expected ${typeVariables.length} type arguments: $node";
163163
}
164-
return new InterfaceType(declaration, kernelArguments);
164+
return new InterfaceType(declaration, kernelArguments, node.nullability);
165165
} else if (declaration is TypeParameter) {
166166
if (arguments.isNotEmpty) {
167167
throw "Type variable can't have arguments (${node.name})";
168168
}
169-
return new TypeParameterType(declaration);
169+
return new TypeParameterType(declaration, null, node.nullability);
170170
} else if (declaration is Typedef) {
171-
return new TypedefType(declaration, kernelArguments);
171+
return new TypedefType(declaration, kernelArguments, node.nullability);
172172
} else {
173173
throw "Unhandled ${declaration.runtimeType}";
174174
}
@@ -263,7 +263,8 @@ class KernelFromParsedType implements Visitor<Node, KernelEnvironment> {
263263
return new FunctionType(positionalParameters, returnType,
264264
namedParameters: namedParameters,
265265
requiredParameterCount: node.arguments.required.length,
266-
typeParameters: parameterEnvironment.parameters);
266+
typeParameters: parameterEnvironment.parameters,
267+
nullability: node.nullability);
267268
}
268269

269270
VoidType visitVoidType(ParsedVoidType node, KernelEnvironment environment) {
@@ -280,7 +281,7 @@ class KernelFromParsedType implements Visitor<Node, KernelEnvironment> {
280281
TypeParameterType type =
281282
node.a.accept<Node, KernelEnvironment>(this, environment);
282283
DartType bound = node.b.accept<Node, KernelEnvironment>(this, environment);
283-
return new TypeParameterType(type.parameter, bound);
284+
return new TypeParameterType(type.parameter, bound, type.nullability);
284285
}
285286

286287
Supertype toSupertype(InterfaceType type) {

pkg/front_end/test/fasta/types/shared_type_tests.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,5 +412,16 @@ abstract class SubtypeTest<T, E> {
412412
isSubtype("({int c, int b, int a}) -> void", "({int b, int c}) -> void");
413413
isSubtype("({int a, int b, int c}) -> void", "({int c}) -> void");
414414
isSubtype("({int c, int b, int a}) -> void", "({int c}) -> void");
415+
416+
// Parsing of nullable and legacy types.
417+
isSubtype("int?", "int?");
418+
isSubtype("int*", "int*");
419+
isSubtype("(int) ->? int", "(int) ->? int");
420+
isSubtype("(int) ->* int", "(int) ->* int");
421+
isSubtype("(int, int*, int?) -> int?", "(int, int*, int?) -> int?");
422+
isSubtype("List<int>?", "List<int>?");
423+
isSubtype("List<int?>?", "List<int?>?");
424+
isSubtype("T & int?", "T & int?", typeParameters: "T extends Object?");
425+
isSubtype("T? & int?", "T? & int?", typeParameters: "T extends Object");
415426
}
416427
}

pkg/front_end/test/fasta/types/type_parser.dart

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import "package:kernel/ast.dart" show Nullability;
6+
57
import "package:front_end/src/fasta/scanner.dart" show scanString, Token;
68

79
import "package:front_end/src/fasta/parser/type_info_impl.dart"
@@ -16,7 +18,9 @@ class ParsedInterfaceType extends ParsedType {
1618

1719
final List<ParsedType> arguments;
1820

19-
ParsedInterfaceType(this.name, this.arguments);
21+
final Nullability nullability;
22+
23+
ParsedInterfaceType(this.name, this.arguments, this.nullability);
2024

2125
String toString() {
2226
StringBuffer sb = new StringBuffer();
@@ -116,7 +120,10 @@ class ParsedFunctionType extends ParsedType {
116120

117121
final ParsedArguments arguments;
118122

119-
ParsedFunctionType(this.typeVariables, this.returnType, this.arguments);
123+
final Nullability nullability;
124+
125+
ParsedFunctionType(
126+
this.typeVariables, this.returnType, this.arguments, this.nullability);
120127

121128
String toString() {
122129
StringBuffer sb = new StringBuffer();
@@ -260,6 +267,16 @@ class Parser {
260267
}
261268
}
262269

270+
Nullability parseNullability() {
271+
Nullability result = Nullability.nonNullable;
272+
if (optionalAdvance("?")) {
273+
result = Nullability.nullable;
274+
} else if (optionalAdvance("*")) {
275+
result = Nullability.legacy;
276+
}
277+
return result;
278+
}
279+
263280
ParsedType parseType() {
264281
if (optional("class")) return parseClass();
265282
if (optional("typedef")) return parseTypedef();
@@ -269,7 +286,9 @@ class Parser {
269286
if (optional("(") || optional("<")) {
270287
type = parseFunctionType();
271288
} else if (optionalAdvance("void")) {
272-
type = new ParsedInterfaceType("void", <ParsedType>[]);
289+
type = new ParsedInterfaceType(
290+
"void", <ParsedType>[], Nullability.nullable);
291+
optionalAdvance("?");
273292
} else {
274293
String name = parseName();
275294
List<ParsedType> arguments = <ParsedType>[];
@@ -283,7 +302,8 @@ class Parser {
283302
peek = splitCloser(peek) ?? peek;
284303
expect(">");
285304
}
286-
type = new ParsedInterfaceType(name, arguments);
305+
Nullability nullability = parseNullability();
306+
type = new ParsedInterfaceType(name, arguments, nullability);
287307
}
288308
if (result == null) {
289309
result = type;
@@ -304,8 +324,10 @@ class Parser {
304324
ParsedArguments arguments = parseArguments();
305325
expect("-");
306326
expect(">");
327+
Nullability nullability = parseNullability();
307328
ParsedType returnType = parseReturnType();
308-
return new ParsedFunctionType(typeVariables, returnType, arguments);
329+
return new ParsedFunctionType(
330+
typeVariables, returnType, arguments, nullability);
309331
}
310332

311333
String parseName() {

0 commit comments

Comments
 (0)