[kernel] Reland: Migrate remaining bin/lib libraries in package:kernel

Change-Id: I5eacb92ea6ce00d83b3440a473cca8dad0892a87
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/197165
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
This commit is contained in:
Johnni Winther
2021-04-28 14:36:53 +00:00
committed by commit-bot@chromium.org
parent 4c0a369f73
commit efe0ddccab
19 changed files with 257 additions and 291 deletions
+3 -6
View File
@@ -3,8 +3,6 @@
// 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.
// @dart = 2.9
import 'dart:io';
import 'package:kernel/kernel.dart';
import 'package:kernel/src/tool/command_line_util.dart';
@@ -32,8 +30,7 @@ class TypeCounter extends RecursiveVisitor {
Map<String, int> _typeCounts = <String, int>{};
defaultNode(Node node) {
String key = node.runtimeType.toString();
_typeCounts[key] ??= 0;
_typeCounts[key]++;
_typeCounts[key] = (_typeCounts[key] ??= 0) + 1;
super.defaultNode(node);
}
@@ -43,8 +40,8 @@ class TypeCounter extends RecursiveVisitor {
data.add([type, count]);
});
data.sort((a, b) {
int aCount = a[1];
int bCount = b[1];
int aCount = a[1] as int;
int bCount = b[1] as int;
return bCount - aCount;
});
for (var entry in data) {
-2
View File
@@ -3,8 +3,6 @@
// 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.
// @dart = 2.9
import 'dart:io';
import 'package:kernel/kernel.dart';
+3 -5
View File
@@ -3,8 +3,6 @@
// 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.
// @dart = 2.9
import 'dart:io';
import 'package:kernel/kernel.dart';
@@ -64,9 +62,9 @@ class WrappedBinaryBuilder extends BinaryBuilder {
linkTableSize += byteOffset;
}
Map<Uri, Source> readUriToSource({bool readCoverage}) {
Map<Uri, Source> readUriToSource({required bool readCoverage}) {
uriToSourceSize -= byteOffset;
var result = super.readUriToSource(readCoverage: readCoverage);
Map<Uri, Source> result = super.readUriToSource(readCoverage: readCoverage);
uriToSourceSize += byteOffset;
return result;
}
@@ -104,7 +102,7 @@ class WrappedBinaryBuilder extends BinaryBuilder {
print("Constant table: ${_bytesToReadable(constantTableSize)}");
print("");
for (Uri uri in librarySizes.keys) {
print("Library '$uri': ${_bytesToReadable(librarySizes[uri])}.");
print("Library '$uri': ${_bytesToReadable(librarySizes[uri]!)}.");
}
}
}
-2
View File
@@ -3,8 +3,6 @@
// 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.
// @dart = 2.9
import 'dart:async';
import 'dart:io';
-2
View File
@@ -3,8 +3,6 @@
// 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.
// @dart = 2.9
import 'dart:io';
import 'package:kernel/error_formatter.dart';
+11 -12
View File
@@ -3,8 +3,6 @@
// 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.
// @dart = 2.9
import 'package:kernel/kernel.dart';
import 'package:kernel/naive_type_checker.dart';
import 'package:kernel/text/ast_to_text.dart';
@@ -36,7 +34,7 @@ Incompatible override of ${superMember} with ${ownMember}:
? where
: _findEnclosingMember(where);
String sourceLocation = '<unknown source>';
String sourceLine = null;
String? sourceLine = null;
// Try finding original source line.
final int fileOffset = _findFileOffset(where);
@@ -44,11 +42,12 @@ Incompatible override of ${superMember} with ${ownMember}:
final Uri fileUri = _fileUriOf(context);
final Component component = context.enclosingComponent;
final Source source = component.uriToSource[fileUri];
final Location location = component.getLocation(fileUri, fileOffset);
final int lineStart = source.lineStarts[location.line - 1];
final int lineEnd = (location.line < source.lineStarts.length)
? source.lineStarts[location.line]
final Source source = component.uriToSource[fileUri]!;
final Location location = component.getLocation(fileUri, fileOffset)!;
final List<int> lineStarts = source.lineStarts!;
final int lineStart = lineStarts[location.line - 1];
final int lineEnd = (location.line < lineStarts.length)
? lineStarts[location.line]
: (source.source.length - 1);
if (lineStart < source.source.length &&
lineEnd < source.source.length &&
@@ -66,7 +65,7 @@ Incompatible override of ${superMember} with ${ownMember}:
name = context.name;
} else if (context is Procedure || context is Constructor) {
final dynamic parent = context.parent;
final String parentName =
final String? parentName =
parent is Class ? parent.name : (parent as Library).name;
name = "${parentName}::${context.name.text}";
} else {
@@ -108,7 +107,7 @@ Source:
static String _realign(String str, [String prefix = '| ']) =>
str.trimRight().replaceAll('\n', '\n${prefix}');
static int _findFileOffset(TreeNode context) {
static int _findFileOffset(TreeNode? context) {
while (context != null && context.fileOffset == TreeNode.noOffset) {
context = context.parent;
}
@@ -117,9 +116,9 @@ Source:
}
static Member _findEnclosingMember(TreeNode n) {
TreeNode context = n;
TreeNode? context = n;
while (context is! Member) {
context = context.parent;
context = context!.parent;
}
return context;
}
+4 -6
View File
@@ -2,14 +2,12 @@
// 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.
// @dart = 2.9
library kernel.external_name;
import 'ast.dart';
/// Returns external (native) name of given [Member].
String getExternalName(Member procedure) {
String? getExternalName(Member procedure) {
// Native procedures are marked as external and have an annotation,
// which looks like this:
//
@@ -22,7 +20,7 @@ String getExternalName(Member procedure) {
return null;
}
for (final Expression annotation in procedure.annotations) {
final String value = _getExternalNameValue(annotation);
final String? value = _getExternalNameValue(annotation);
if (value != null) {
return value;
}
@@ -34,7 +32,7 @@ String getExternalName(Member procedure) {
List<String> getNativeExtensionUris(Library library) {
final List<String> uris = <String>[];
for (Expression annotation in library.annotations) {
final String value = _getExternalNameValue(annotation);
final String? value = _getExternalNameValue(annotation);
if (value != null) {
uris.add(value);
}
@@ -42,7 +40,7 @@ List<String> getNativeExtensionUris(Library library) {
return uris;
}
String _getExternalNameValue(Expression annotation) {
String? _getExternalNameValue(Expression annotation) {
if (annotation is ConstructorInvocation) {
if (_isExternalName(annotation.target.enclosingClass)) {
return (annotation.arguments.positional.single as StringLiteral).value;
+9 -11
View File
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
import 'class_hierarchy.dart';
import 'core_types.dart';
import 'kernel.dart';
@@ -79,7 +77,7 @@ ${superMember} is a ${_memberKind(superMember)}
final DartType superType = setterType(host, superMember);
final bool isCovariant = ownMember is Field
? ownMember.isCovariant
: ownMember.function.positionalParameters[0].isCovariant;
: ownMember.function!.positionalParameters[0].isCovariant;
if (!_isValidParameterOverride(isCovariant, ownType, superType)) {
if (isCovariant) {
return failures.reportInvalidOverride(ownMember, superMember, '''
@@ -101,7 +99,8 @@ ${ownType} is not a subtype of ${superType}
}
}
} else {
final String msg = _checkFunctionOverride(host, ownMember, superMember);
final String? msg =
_checkFunctionOverride(host, ownMember, superMember as Procedure);
if (msg != null) {
return failures.reportInvalidOverride(ownMember, superMember, msg);
}
@@ -128,7 +127,7 @@ ${ownType} is not a subtype of ${superType}
Substitution _makeSubstitutionForMember(Class host, Member member) {
final Supertype hostType =
hierarchy.getClassAsInstanceOf(host, member.enclosingClass);
hierarchy.getClassAsInstanceOf(host, member.enclosingClass!)!;
return Substitution.fromSupertype(hostType);
}
@@ -137,11 +136,10 @@ ${ownType} is not a subtype of ${superType}
///
/// Note: this function is a copy of [SubtypeTester._isFunctionSubtypeOf]
/// but it additionally accounts for parameter covariance.
String _checkFunctionOverride(
Class host, Member ownMember, Member superMember) {
if (ownMember is Procedure &&
(ownMember.isMemberSignature ||
(ownMember.isForwardingStub && !ownMember.isForwardingSemiStub))) {
String? _checkFunctionOverride(
Class host, Procedure ownMember, Procedure superMember) {
if (ownMember.isMemberSignature ||
(ownMember.isForwardingStub && !ownMember.isForwardingSemiStub)) {
// Synthesized members are not obligated to override super members.
return null;
}
@@ -222,7 +220,7 @@ super method declares ${superParameter.type}
ownFunction.namedParameters,
key: (v) => v.name);
for (VariableDeclaration superParameter in superFunction.namedParameters) {
final VariableDeclaration ownParameter =
final VariableDeclaration? ownParameter =
ownParameters[superParameter.name];
if (ownParameter == null) {
return 'override is missing ${superParameter.name} parameter';
+10 -11
View File
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.
// @dart = 2.9
import 'package:kernel/src/bounds_checks.dart';
import '../ast.dart';
@@ -15,7 +13,8 @@ import '../type_algebra.dart';
/// The algorithm is specified at
/// https://github.com/dart-lang/language/blob/master/accepted/future-releases/nnbd/feature-specification.md#constant-instances
DartType computeConstCanonicalType(DartType type, CoreTypes coreTypes,
{bool isNonNullableByDefault}) {
{required bool isNonNullableByDefault}) {
// ignore: unnecessary_null_comparison
assert(isNonNullableByDefault != null);
if (type is InvalidType) {
@@ -112,7 +111,7 @@ DartType computeConstCanonicalType(DartType type, CoreTypes coreTypes,
assert(type.declaredNullability == Nullability.nonNullable);
List<TypeParameter> canonicalizedTypeParameters;
Substitution substitution;
Substitution? substitution;
if (type.typeParameters.isEmpty) {
canonicalizedTypeParameters = const <TypeParameter>[];
substitution = null;
@@ -178,24 +177,24 @@ DartType computeConstCanonicalType(DartType type, CoreTypes coreTypes,
}
// Canonicalize typedef type, just in case.
TypedefType canonicalizedTypedefType;
if (type.typedefType == null) {
TypedefType? canonicalizedTypedefType;
TypedefType? typedefType = type.typedefType;
if (typedefType == null) {
canonicalizedTypedefType = null;
} else {
List<DartType> canonicalizedTypeArguments;
if (type.typedefType.typeArguments.isEmpty) {
if (typedefType.typeArguments.isEmpty) {
canonicalizedTypeArguments = const <DartType>[];
} else {
canonicalizedTypeArguments = new List<DartType>.of(
type.typedefType.typeArguments,
growable: false);
canonicalizedTypeArguments =
new List<DartType>.of(typedefType.typeArguments, growable: false);
for (int i = 0; i < canonicalizedTypeArguments.length; ++i) {
canonicalizedTypeArguments[i] = computeConstCanonicalType(
canonicalizedTypeArguments[i], coreTypes,
isNonNullableByDefault: isNonNullableByDefault);
}
}
canonicalizedTypedefType = new TypedefType(type.typedefType.typedefNode,
canonicalizedTypedefType = new TypedefType(typedefType.typedefNode,
Nullability.legacy, canonicalizedTypeArguments);
}
@@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.
// @dart = 2.9
import '../ast.dart';
import '../core_types.dart';
import '../visitor.dart';
@@ -64,7 +62,7 @@ class DartTypeEquivalence implements DartTypeVisitor1<bool, DartType> {
// themselves if the typedef types are equal.
assert(() {
DartTypeEquivalence copy = this.copy();
if (!copy.areEqual(node.typedefType, other.typedefType)) {
if (!copy.areEqual(node.typedefType!, other.typedefType!)) {
return true;
}
FunctionType nodeWithoutTypedefType = new FunctionType(
@@ -85,7 +83,7 @@ class DartTypeEquivalence implements DartTypeVisitor1<bool, DartType> {
typedefType: null);
return copy.areEqual(nodeWithoutTypedefType, otherWithoutTypedefType);
}());
return node.typedefType.accept1(this, other.typedefType);
return node.typedefType!.accept1(this, other.typedefType);
}
// Perform simple number checks before the checks on parts.
@@ -130,7 +128,7 @@ class DartTypeEquivalence implements DartTypeVisitor1<bool, DartType> {
String otherName = other.namedParameters[i].name;
DartType otherType = other.namedParameters[i].type;
if (!nodeNamedParameters.containsKey(otherName) ||
!nodeNamedParameters[otherName].accept1(this, otherType)) {
!nodeNamedParameters[otherName]!.accept1(this, otherType)) {
result = false;
}
}
@@ -251,7 +249,7 @@ class DartTypeEquivalence implements DartTypeVisitor1<bool, DartType> {
return false;
}
return nodeIsIntersection
? node.promotedBound.accept1(this, other.promotedBound)
? node.promotedBound!.accept1(this, other.promotedBound)
: true;
}
return false;
@@ -315,7 +313,7 @@ class DartTypeEquivalence implements DartTypeVisitor1<bool, DartType> {
TypeParameter _lookup(TypeParameter parameter) {
for (int i = _alphaRenamingStack.length - 1; i >= 0; --i) {
if (_alphaRenamingStack[i].containsKey(parameter)) {
return _alphaRenamingStack[i][parameter];
return _alphaRenamingStack[i][parameter]!;
}
}
return parameter;
-2
View File
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
library kernel.batch_util;
import 'dart:convert';
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
import 'dart:io';
import 'package:kernel/kernel.dart';
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
import 'package:kernel/ast.dart';
import 'package:kernel/visitor.dart';
@@ -32,7 +30,7 @@ class _LibraryCollector extends RecursiveVisitor {
seen(node);
} else if (node is Name) {
if (node.library != null) {
seen(node.library);
seen(node.library!);
}
}
super.defaultNode(node);
@@ -44,10 +42,10 @@ class _LibraryCollector extends RecursiveVisitor {
}
void seen(TreeNode node) {
TreeNode parent = node;
TreeNode? parent = node;
while (parent != null && parent is! Library) {
parent = parent.parent;
}
allSeenLibraries.add(parent);
allSeenLibraries.add(parent as Library);
}
}
-2
View File
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
const String mockSdk = """
class Object;
class Comparable<T>;
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
import 'package:kernel/ast.dart';
/// Returns a [Component] object containing empty definitions of core SDK
@@ -20,9 +18,9 @@ Component createMockSdkComponent() {
coreLib.addClass(objectClass);
Class addClass(Library lib, String name,
{Supertype supertype,
List<TypeParameter> typeParameters,
List<Supertype> implementedTypes}) {
{Supertype? supertype,
List<TypeParameter>? typeParameters,
List<Supertype>? implementedTypes}) {
Class c = new Class(
name: name,
supertype: supertype ?? objectClass.asThisSupertype,
@@ -36,7 +34,7 @@ Component createMockSdkComponent() {
InterfaceType objectType =
new InterfaceType(objectClass, coreLib.nonNullable);
TypeParameter typeParam(String name, [DartType bound]) {
TypeParameter typeParam(String name, [DartType? bound]) {
return new TypeParameter(name, bound ?? objectType);
}
+30 -36
View File
@@ -2,12 +2,10 @@
// 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.
// @dart = 2.9
import 'package:kernel/ast.dart' show Nullability;
abstract class ParsedType {
R accept<R, A>(Visitor<R, A> visitor, [A a]);
R accept<R, A>(Visitor<R, A> visitor, A a);
}
enum ParsedNullability {
@@ -31,8 +29,6 @@ Nullability interpretParsedNullability(ParsedNullability parsedNullability,
case ParsedNullability.omitted:
return ifOmitted;
}
return throw new UnsupportedError(
"$parsedNullability in interpretParsedNullability");
}
String parsedNullabilityToString(ParsedNullability parsedNullability) {
@@ -44,8 +40,6 @@ String parsedNullabilityToString(ParsedNullability parsedNullability) {
case ParsedNullability.omitted:
return '';
}
return throw new UnsupportedError(
"$parsedNullability parsedNullabilityToString");
}
class ParsedInterfaceType extends ParsedType {
@@ -69,7 +63,7 @@ class ParsedInterfaceType extends ParsedType {
return "$sb";
}
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitInterfaceType(this, a);
}
}
@@ -82,10 +76,10 @@ abstract class ParsedDeclaration extends ParsedType {
class ParsedClass extends ParsedDeclaration {
final List<ParsedTypeVariable> typeVariables;
final ParsedInterfaceType supertype;
final ParsedInterfaceType mixedInType;
final ParsedInterfaceType? supertype;
final ParsedInterfaceType? mixedInType;
final List<ParsedType> interfaces;
final ParsedFunctionType callableType;
final ParsedFunctionType? callableType;
ParsedClass(String name, this.typeVariables, this.supertype, this.mixedInType,
this.interfaces, this.callableType)
@@ -118,7 +112,7 @@ class ParsedClass extends ParsedDeclaration {
return "$sb";
}
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitClass(this, a);
}
}
@@ -144,7 +138,7 @@ class ParsedExtension extends ParsedDeclaration {
return "$sb";
}
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitExtension(this, a);
}
}
@@ -170,7 +164,7 @@ class ParsedTypedef extends ParsedDeclaration {
return "$sb;";
}
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitTypedef(this, a);
}
}
@@ -202,7 +196,7 @@ class ParsedFunctionType extends ParsedType {
return "$sb";
}
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitFunctionType(this, a);
}
}
@@ -210,7 +204,7 @@ class ParsedFunctionType extends ParsedType {
class ParsedVoidType extends ParsedType {
String toString() => "void";
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitVoidType(this, a);
}
}
@@ -218,7 +212,7 @@ class ParsedVoidType extends ParsedType {
class ParsedTypeVariable extends ParsedType {
final String name;
final ParsedType bound;
final ParsedType? bound;
ParsedTypeVariable(this.name, this.bound);
@@ -231,7 +225,7 @@ class ParsedTypeVariable extends ParsedType {
return "$sb";
}
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitTypeVariable(this, a);
}
}
@@ -251,7 +245,7 @@ class ParsedIntersectionType extends ParsedType {
return "$sb";
}
R accept<R, A>(Visitor<R, A> visitor, [A a]) {
R accept<R, A>(Visitor<R, A> visitor, A a) {
return visitor.visitIntersectionType(this, a);
}
}
@@ -311,10 +305,10 @@ class ParsedNamedArgument {
class Token {
final int charOffset;
final String text;
final String? text;
final bool isIdentifier;
Token next;
Token? next;
Token(this.charOffset, this.text, {this.isIdentifier: false});
@@ -331,7 +325,7 @@ class Parser {
bool get atEof => peek.isEof;
void advance() {
peek = peek.next;
peek = peek.next!;
}
String computeLocation() {
@@ -404,7 +398,7 @@ class Parser {
results.add(type);
} while (optionalAdvance("&"));
// Parse `A & B & C` as `A & (B & C)` and not `(A & B) & C`.
ParsedType result;
ParsedType? result;
for (ParsedType type in results.reversed) {
if (result == null) {
result = type;
@@ -412,7 +406,7 @@ class Parser {
result = new ParsedIntersectionType(type, result);
}
}
return result;
return result!;
}
ParsedType parseReturnType() {
@@ -436,7 +430,7 @@ class Parser {
throw "Expected a name, "
"but got '${peek.text}'\n${computeLocation()}";
}
String result = peek.text;
String result = peek.text!;
advance();
return result;
}
@@ -485,7 +479,7 @@ class Parser {
ParsedTypeVariable parseTypeVariable() {
String name = parseName();
ParsedType bound;
ParsedType? bound;
if (optionalAdvance("extends")) {
bound = parseType();
}
@@ -496,12 +490,12 @@ class Parser {
expect("class");
String name = parseName();
List<ParsedTypeVariable> typeVariables = parseTypeVariablesOpt();
ParsedType supertype;
ParsedType mixedInType;
ParsedInterfaceType? supertype;
ParsedInterfaceType? mixedInType;
if (optionalAdvance("extends")) {
supertype = parseType();
supertype = parseType() as ParsedInterfaceType;
if (optionalAdvance("with")) {
mixedInType = parseType();
mixedInType = parseType() as ParsedInterfaceType;
}
}
List<ParsedType> interfaces = <ParsedType>[];
@@ -510,7 +504,7 @@ class Parser {
interfaces.add(parseType());
} while (optionalAdvance(","));
}
ParsedFunctionType callableType;
ParsedFunctionType? callableType;
if (optionalAdvance("{")) {
callableType = parseFunctionType();
expect("}");
@@ -526,7 +520,7 @@ class Parser {
String name = parseName();
List<ParsedTypeVariable> typeVariables = parseTypeVariablesOpt();
expect("on");
ParsedType onType = parseType();
ParsedInterfaceType onType = parseType() as ParsedInterfaceType;
expect(";");
return new ParsedExtension(name, typeVariables, onType);
}
@@ -585,8 +579,8 @@ bool isWhiteSpace(int c) =>
Token scanString(String text) {
int offset = 0;
Token first;
Token current;
Token? first;
Token? current;
while (offset < text.length) {
int c = text.codeUnitAt(offset);
if (isWhiteSpace(c)) {
@@ -621,7 +615,7 @@ Token scanString(String text) {
} else {
current.next = eof;
}
return first;
return first!;
}
List<ParsedType> parse(String text) {
@@ -635,7 +629,7 @@ List<ParsedType> parse(String text) {
List<ParsedTypeVariable> parseTypeVariables(String text) {
Parser parser = new Parser(scanString(text), text);
List<ParsedType> result = parser.parseTypeVariablesOpt();
List<ParsedTypeVariable> result = parser.parseTypeVariablesOpt();
if (!parser.atEof) {
throw "Expected EOF, but got '${parser.peek.text}'\n"
"${parser.computeLocation()}";
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
import "package:kernel/ast.dart" hide Visitor;
import 'package:kernel/core_types.dart' show CoreTypes;
@@ -41,7 +39,7 @@ Component parseComponent(String source, Uri uri) {
}
Library parseLibrary(Uri uri, String text,
{Uri fileUri, TypeParserEnvironment environment}) {
{Uri? fileUri, TypeParserEnvironment? environment}) {
fileUri ??= uri;
environment ??= new TypeParserEnvironment(uri, fileUri);
Library library =
@@ -53,15 +51,17 @@ Library parseLibrary(Uri uri, String text,
environment._registerDeclaration(
name,
new Class(fileUri: fileUri, name: name)
..typeParameters.addAll(new List<TypeParameter>.filled(
type.typeVariables.length, null)));
..typeParameters.addAll(new List<TypeParameter>.generate(
type.typeVariables.length,
(int i) => new TypeParameter('T$i'))));
} else if (type is ParsedExtension) {
String name = type.name;
environment._registerDeclaration(
name,
new Extension(fileUri: fileUri, name: name)
..typeParameters.addAll(new List<TypeParameter>.filled(
type.typeVariables.length, null)));
..typeParameters.addAll(new List<TypeParameter>.generate(
type.typeVariables.length,
(int i) => new TypeParameter('T$i'))));
}
}
for (ParsedType type in types) {
@@ -80,15 +80,16 @@ Library parseLibrary(Uri uri, String text,
}
class Env {
Component component;
late Component component;
CoreTypes coreTypes;
late CoreTypes coreTypes;
TypeParserEnvironment _libraryEnvironment;
late TypeParserEnvironment _libraryEnvironment;
final bool isNonNullableByDefault;
Env(String source, {this.isNonNullableByDefault}) {
Env(String source, {required this.isNonNullableByDefault}) {
// ignore: unnecessary_null_comparison
assert(isNonNullableByDefault != null);
Uri libraryUri = Uri.parse('memory:main.dart');
Uri coreUri = Uri.parse("dart:core");
@@ -108,18 +109,18 @@ class Env {
}
DartType parseType(String text,
{Map<String, DartType Function()> additionalTypes}) {
{Map<String, DartType Function()>? additionalTypes}) {
return _libraryEnvironment.parseType(text,
additionalTypes: additionalTypes);
}
List<DartType> parseTypes(String text,
{Map<String, DartType Function()> additionalTypes}) {
{Map<String, DartType Function()>? additionalTypes}) {
return _libraryEnvironment.parseTypes(text,
additionalTypes: additionalTypes);
}
List<TypeParameter> extendWithTypeParameters(String typeParameters) {
List<TypeParameter> extendWithTypeParameters(String? typeParameters) {
if (typeParameters == null || typeParameters.isEmpty) {
return <TypeParameter>[];
}
@@ -130,7 +131,7 @@ class Env {
}
void withTypeParameters(
String typeParameters, void Function(List<TypeParameter>) f) {
String? typeParameters, void Function(List<TypeParameter>) f) {
if (typeParameters == null || typeParameters.isEmpty) {
f(<TypeParameter>[]);
} else {
@@ -150,7 +151,7 @@ class TypeParserEnvironment {
final Map<String, TreeNode> _declarations = <String, TreeNode>{};
final TypeParserEnvironment _parent;
final TypeParserEnvironment? _parent;
/// Collects types to set their nullabilities after type parameters are ready.
///
@@ -167,7 +168,7 @@ class TypeParserEnvironment {
TypeParserEnvironment(this.uri, this.fileUri, [this._parent]);
Node _kernelFromParsedType(ParsedType type,
{Map<String, DartType Function()> additionalTypes}) {
{Map<String, DartType Function()>? additionalTypes}) {
Node node = type.accept(
new _KernelFromParsedType(additionalTypes: additionalTypes), this);
return node;
@@ -175,14 +176,14 @@ class TypeParserEnvironment {
/// Parses a single type.
DartType parseType(String text,
{Map<String, DartType Function()> additionalTypes}) {
{Map<String, DartType Function()>? additionalTypes}) {
return _kernelFromParsedType(type_parser.parse(text).single,
additionalTypes: additionalTypes);
additionalTypes: additionalTypes) as DartType;
}
/// Parses a list of types separated by commas.
List<DartType> parseTypes(String text,
{Map<String, DartType Function()> additionalTypes}) {
{Map<String, DartType Function()>? additionalTypes}) {
return (parseType("(${text}) -> void", additionalTypes: additionalTypes)
as FunctionType)
.positionalParameters;
@@ -190,19 +191,19 @@ class TypeParserEnvironment {
bool isObject(String name) => name == "Object" && "$uri" == "dart:core";
Class get objectClass => lookupDeclaration("Object");
Class get objectClass => lookupDeclaration("Object") as Class;
TreeNode lookupDeclaration(String name) {
TreeNode result = _declarations[name];
TreeNode? result = _declarations[name];
if (result == null && _parent != null) {
return _parent.lookupDeclaration(name);
return _parent!.lookupDeclaration(name);
}
if (result == null) throw "Not found: $name";
return result;
}
TreeNode _registerDeclaration(String name, TreeNode declaration) {
TreeNode existing = _declarations[name];
T _registerDeclaration<T extends TreeNode>(String name, T declaration) {
TreeNode? existing = _declarations[name];
if (existing != null) {
throw "Duplicated declaration: $name";
}
@@ -214,12 +215,13 @@ class TypeParserEnvironment {
.._declarations.addAll(declarations);
}
TypeParserEnvironment extendWithTypeParameters(String typeParameters) {
TypeParserEnvironment extendWithTypeParameters(String? typeParameters) {
if (typeParameters?.isEmpty ?? true) return this;
return extendToParameterEnvironment(typeParameters).environment;
return extendToParameterEnvironment(typeParameters!).environment;
}
ParameterEnvironment extendToParameterEnvironment(String typeParameters) {
// ignore: unnecessary_null_comparison
assert(typeParameters != null && typeParameters.isNotEmpty);
return const _KernelFromParsedType().computeTypeParameterEnvironment(
parseTypeVariables("<${typeParameters}>"), this);
@@ -228,23 +230,34 @@ class TypeParserEnvironment {
/// Returns the predefined type by the [name], if any.
///
/// Use this in subclasses to add support for additional predefined types.
DartType getPredefinedNamedType(String name) {
DartType? getPredefinedNamedType(String name) {
if (_parent != null) {
return _parent.getPredefinedNamedType(name);
return _parent!.getPredefinedNamedType(name);
}
return null;
}
}
class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
final Map<String, DartType Function()> additionalTypes; // Can be null.
final Map<String, DartType Function()>? additionalTypes; // Can be null.
const _KernelFromParsedType({this.additionalTypes});
DartType _parseType(ParsedType type, TypeParserEnvironment environment) {
return type.accept<Node, TypeParserEnvironment>(this, environment)
as DartType;
}
InterfaceType? _parseOptionalInterfaceType(
ParsedType? type, TypeParserEnvironment environment) {
return type?.accept<Node, TypeParserEnvironment>(this, environment)
as InterfaceType?;
}
DartType visitInterfaceType(
ParsedInterfaceType node, TypeParserEnvironment environment) {
String name = node.name;
DartType predefined = environment.getPredefinedNamedType(name);
DartType? predefined = environment.getPredefinedNamedType(name);
if (predefined != null) {
return predefined;
} else if (name == "dynamic") {
@@ -268,16 +281,15 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
// Don't return a const object to ensure we test implementations that use
// identical.
return new InvalidType();
} else if (additionalTypes != null && additionalTypes.containsKey(name)) {
return additionalTypes[name].call();
} else if (additionalTypes != null && additionalTypes!.containsKey(name)) {
return additionalTypes![name]!.call();
}
TreeNode declaration = environment.lookupDeclaration(name);
List<ParsedType> arguments = node.arguments;
List<DartType> kernelArguments =
new List<DartType>.filled(arguments.length, null);
new List<DartType>.filled(arguments.length, dummyDartType);
for (int i = 0; i < arguments.length; i++) {
kernelArguments[i] =
arguments[i].accept<Node, TypeParserEnvironment>(this, environment);
kernelArguments[i] = _parseType(arguments[i], environment);
}
if (name == "FutureOr") {
return new FutureOrType(kernelArguments.single,
@@ -296,7 +308,8 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
}
List<TypeParameter> typeVariables = declaration.typeParameters;
if (kernelArguments.isEmpty && typeVariables.isNotEmpty) {
kernelArguments = new List<DartType>.filled(typeVariables.length, null);
kernelArguments =
new List<DartType>.filled(typeVariables.length, dummyDartType);
for (int i = 0; i < typeVariables.length; i++) {
kernelArguments[i] = typeVariables[i].defaultType;
}
@@ -310,7 +323,7 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
}
Nullability nullability =
identical(declaration.bound, TypeParameter.unsetBoundSentinel)
? null
? Nullability.nonNullable
: TypeParameterType.computeNullabilityFromBound(declaration);
TypeParameterType type = new TypeParameterType(
declaration,
@@ -320,6 +333,7 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
// the bound because it's not yet available, it will be set to null. In
// that case, put it to the list to be updated later, when the bound is
// available.
// ignore: unnecessary_null_comparison
if (type.declaredNullability == null) {
environment.pendingNullabilities.add(type);
}
@@ -337,7 +351,7 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
Class visitClass(ParsedClass node, TypeParserEnvironment environment) {
String name = node.name;
Class cls = environment.lookupDeclaration(name);
Class cls = environment.lookupDeclaration(name) as Class;
ParameterEnvironment parameterEnvironment =
computeTypeParameterEnvironment(node.typeVariables, environment);
List<TypeParameter> parameters = parameterEnvironment.parameters;
@@ -347,8 +361,8 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
..addAll(parameters);
{
TypeParserEnvironment environment = parameterEnvironment.environment;
InterfaceType type = node.supertype
?.accept<Node, TypeParserEnvironment>(this, environment);
InterfaceType? type =
_parseOptionalInterfaceType(node.supertype, environment);
if (type == null) {
if (!environment.isObject(name)) {
cls.supertype = environment.objectClass.asRawSupertype;
@@ -356,15 +370,15 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
} else {
cls.supertype = toSupertype(type);
}
InterfaceType mixedInType = node.mixedInType
?.accept<Node, TypeParserEnvironment>(this, environment);
InterfaceType? mixedInType =
_parseOptionalInterfaceType(node.mixedInType, environment);
if (mixedInType != null) {
cls.mixedInType = toSupertype(mixedInType);
}
List<ParsedType> interfaces = node.interfaces;
for (int i = 0; i < interfaces.length; i++) {
cls.implementedTypes.add(toSupertype(interfaces[i]
.accept<Node, TypeParserEnvironment>(this, environment)));
cls.implementedTypes.add(toSupertype(
_parseOptionalInterfaceType(interfaces[i], environment)!));
}
}
return cls;
@@ -373,7 +387,7 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
Extension visitExtension(
ParsedExtension node, TypeParserEnvironment environment) {
String name = node.name;
Extension ext = environment.lookupDeclaration(name);
Extension ext = environment.lookupDeclaration(name) as Extension;
ParameterEnvironment parameterEnvironment =
computeTypeParameterEnvironment(node.typeVariables, environment);
List<TypeParameter> parameters = parameterEnvironment.parameters;
@@ -383,8 +397,8 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
..addAll(parameters);
{
TypeParserEnvironment environment = parameterEnvironment.environment;
DartType onType =
node.onType?.accept<Node, TypeParserEnvironment>(this, environment);
DartType onType = node.onType
.accept<Node, TypeParserEnvironment>(this, environment) as DartType;
ext.onType = onType;
}
return ext;
@@ -400,7 +414,7 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
DartType type;
{
TypeParserEnvironment environment = parameterEnvironment.environment;
type = node.type.accept<Node, TypeParserEnvironment>(this, environment);
type = _parseType(node.type, environment);
if (type is FunctionType) {
FunctionType f = type;
type = new FunctionType(
@@ -429,21 +443,16 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
DartType returnType;
{
TypeParserEnvironment environment = parameterEnvironment.environment;
returnType = node.returnType
?.accept<Node, TypeParserEnvironment>(this, environment);
returnType = _parseType(node.returnType, environment);
for (ParsedType argument in node.arguments.required) {
positionalParameters.add(
argument.accept<Node, TypeParserEnvironment>(this, environment));
positionalParameters.add(_parseType(argument, environment));
}
for (ParsedType argument in node.arguments.positional) {
positionalParameters.add(
argument.accept<Node, TypeParserEnvironment>(this, environment));
positionalParameters.add(_parseType(argument, environment));
}
for (ParsedNamedArgument argument in node.arguments.named) {
namedParameters.add(new NamedType(
argument.name,
argument.type
.accept<Node, TypeParserEnvironment>(this, environment),
argument.name, _parseType(argument.type, environment),
isRequired: argument.isRequired));
}
}
@@ -468,9 +477,8 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
TypeParameterType visitIntersectionType(
ParsedIntersectionType node, TypeParserEnvironment environment) {
TypeParameterType type =
node.a.accept<Node, TypeParserEnvironment>(this, environment);
DartType bound =
node.b.accept<Node, TypeParserEnvironment>(this, environment);
_parseType(node.a, environment) as TypeParameterType;
DartType bound = _parseType(node.b, environment);
return new TypeParameterType.intersection(
type.parameter, type.nullability, bound);
}
@@ -482,8 +490,8 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
ParameterEnvironment computeTypeParameterEnvironment(
List<ParsedTypeVariable> typeVariables,
TypeParserEnvironment environment) {
List<TypeParameter> typeParameters =
new List<TypeParameter>.filled(typeVariables.length, null);
List<TypeParameter> typeParameters = new List<TypeParameter>.filled(
typeVariables.length, dummyTypeParameter);
Map<String, TypeParameter> typeParametersByName = <String, TypeParameter>{};
for (int i = 0; i < typeVariables.length; i++) {
String name = typeVariables[i].name;
@@ -493,7 +501,7 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
environment._extend(typeParametersByName);
Class objectClass = environment.objectClass;
for (int i = 0; i < typeVariables.length; i++) {
ParsedType bound = typeVariables[i].bound;
ParsedType? bound = typeVariables[i].bound;
TypeParameter typeParameter = typeParameters[i];
if (bound == null) {
typeParameter
@@ -501,8 +509,7 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
objectClass, Nullability.nullable, const <DartType>[])
..defaultType = const DynamicType();
} else {
DartType type =
bound.accept<Node, TypeParserEnvironment>(this, nestedEnvironment);
DartType type = _parseType(bound, nestedEnvironment);
typeParameter
..bound = type
// The default type will be overridden below, but we need to set it
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
library kernel.transformations.empty;
import '../ast.dart';
+102 -106
View File
@@ -2,8 +2,6 @@
// 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.
// @dart = 2.9
library kernel.type_checker;
import 'ast.dart';
@@ -20,9 +18,9 @@ abstract class TypeChecker {
final CoreTypes coreTypes;
final ClassHierarchy hierarchy;
final bool ignoreSdk;
TypeEnvironment environment;
Library currentLibrary;
InterfaceType currentThisType;
final TypeEnvironment environment;
Library? currentLibrary;
InterfaceType? currentThisType;
TypeChecker(this.coreTypes, this.hierarchy, {this.ignoreSdk: true})
: environment = new TypeEnvironment(coreTypes, hierarchy);
@@ -68,14 +66,14 @@ abstract class TypeChecker {
DartType getterType(Class host, Member member) {
Supertype hostType =
hierarchy.getClassAsInstanceOf(host, member.enclosingClass);
hierarchy.getClassAsInstanceOf(host, member.enclosingClass!)!;
Substitution substitution = Substitution.fromSupertype(hostType);
return substitution.substituteType(member.getterType);
}
DartType setterType(Class host, Member member) {
Supertype hostType =
hierarchy.getClassAsInstanceOf(host, member.enclosingClass);
hierarchy.getClassAsInstanceOf(host, member.enclosingClass!)!;
Substitution substitution = Substitution.fromSupertype(hostType);
return substitution.substituteType(member.setterType, contravariant: true);
}
@@ -128,12 +126,12 @@ class TypeCheckingVisitor
final ClassHierarchy hierarchy;
CoreTypes get coreTypes => environment.coreTypes;
Library get currentLibrary => checker.currentLibrary;
Class get currentClass => checker.currentThisType.classNode;
InterfaceType get currentThisType => checker.currentThisType;
Library? get currentLibrary => checker.currentLibrary;
Class? get currentClass => checker.currentThisType?.classNode;
InterfaceType? get currentThisType => checker.currentThisType;
DartType currentReturnType;
DartType currentYieldType;
DartType? currentReturnType;
DartType? currentYieldType;
AsyncMarker currentAsyncMarker = AsyncMarker.Sync;
TypeCheckingVisitor(this.checker, this.environment, this.hierarchy);
@@ -147,7 +145,7 @@ class TypeCheckingVisitor
}
Expression checkAndDowncastExpression(Expression from, DartType to) {
TreeNode parent = from.parent;
TreeNode? parent = from.parent;
DartType type = visitExpression(from);
Expression result = checker.checkAndDowncastExpression(from, type, to);
result.parent = parent;
@@ -193,7 +191,7 @@ class TypeCheckingVisitor
visitField(Field node) {
if (node.initializer != null) {
node.initializer =
checkAndDowncastExpression(node.initializer, node.type);
checkAndDowncastExpression(node.initializer!, node.type);
}
}
@@ -223,14 +221,14 @@ class TypeCheckingVisitor
.forEach(handleOptionalParameter);
node.namedParameters.forEach(handleOptionalParameter);
if (node.body != null) {
visitStatement(node.body);
visitStatement(node.body!);
}
currentAsyncMarker = oldAsyncMarker;
}
void handleNestedFunctionNode(FunctionNode node) {
DartType oldReturn = currentReturnType;
DartType oldYield = currentYieldType;
DartType? oldReturn = currentReturnType;
DartType? oldYield = currentYieldType;
currentReturnType = _getInternalReturnType(node);
currentYieldType = _getYieldType(node);
handleFunctionNode(node);
@@ -241,19 +239,19 @@ class TypeCheckingVisitor
void handleOptionalParameter(VariableDeclaration parameter) {
if (parameter.initializer != null) {
// Default parameter values cannot be downcast.
checkExpressionNoDowncast(parameter.initializer, parameter.type);
checkExpressionNoDowncast(parameter.initializer!, parameter.type);
}
}
Substitution getReceiverType(
TreeNode access, Expression receiver, Member member) {
DartType type = visitExpression(receiver);
Class superclass = member.enclosingClass;
Class superclass = member.enclosingClass!;
if (superclass.supertype == null) {
return Substitution.empty; // Members on Object are always accessible.
}
while (type is TypeParameterType) {
type = (type as TypeParameterType).bound;
type = type.bound;
}
if (type is NeverType || type is NullType) {
// The bottom type is a subtype of all types, so it should be allowed.
@@ -261,7 +259,7 @@ class TypeCheckingVisitor
}
if (type is InterfaceType) {
// The receiver type should implement the interface declaring the member.
List<DartType> upcastTypeArguments =
List<DartType>? upcastTypeArguments =
hierarchy.getTypeArgumentsAsInstanceOf(type, superclass);
if (upcastTypeArguments != null) {
return Substitution.fromPairs(
@@ -280,27 +278,27 @@ class TypeCheckingVisitor
Substitution getSuperReceiverType(Member member) {
return Substitution.fromSupertype(
hierarchy.getClassAsInstanceOf(currentClass, member.enclosingClass));
hierarchy.getClassAsInstanceOf(currentClass!, member.enclosingClass!)!);
}
DartType handleCall(Arguments arguments, DartType functionType,
{Substitution receiver: Substitution.empty,
List<TypeParameter> typeParameters}) {
List<TypeParameter>? typeParameters}) {
if (functionType is FunctionType) {
typeParameters ??= functionType.typeParameters;
if (arguments.positional.length < functionType.requiredParameterCount) {
fail(arguments, 'Too few positional arguments');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
if (arguments.positional.length >
functionType.positionalParameters.length) {
fail(arguments, 'Too many positional arguments');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
List<DartType> typeArguments = arguments.types;
if (typeArguments.length != typeParameters.length) {
fail(arguments, 'Wrong number of type arguments');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
Substitution substitution = _instantiateFunction(
typeParameters, typeArguments, arguments,
@@ -328,7 +326,7 @@ class TypeCheckingVisitor
}
if (!found) {
fail(argument.value, 'Unexpected named parameter: ${argument.name}');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
}
return substitution.substituteType(functionType.returnType);
@@ -339,7 +337,7 @@ class TypeCheckingVisitor
}
}
DartType _getInternalReturnType(FunctionNode function) {
DartType? _getInternalReturnType(FunctionNode function) {
switch (function.asyncMarker) {
case AsyncMarker.Sync:
return function.returnType;
@@ -359,17 +357,17 @@ class TypeCheckingVisitor
case AsyncMarker.SyncYielding:
// The SyncStar transform wraps the original function body twice,
// where the inner most function returns bool.
TreeNode parent = function.parent;
TreeNode? parent = function.parent;
while (parent is! FunctionNode) {
parent = parent.parent;
parent = parent!.parent;
}
FunctionNode enclosingFunction = parent as FunctionNode;
FunctionNode enclosingFunction = parent;
if (enclosingFunction.dartAsyncMarker == AsyncMarker.Sync) {
parent = enclosingFunction.parent;
while (parent is! FunctionNode) {
parent = parent.parent;
parent = parent!.parent;
}
enclosingFunction = parent as FunctionNode;
enclosingFunction = parent;
if (enclosingFunction.dartAsyncMarker == AsyncMarker.SyncStar) {
return coreTypes.boolLegacyRawType;
}
@@ -381,7 +379,7 @@ class TypeCheckingVisitor
}
}
DartType _getYieldType(FunctionNode function) {
DartType? _getYieldType(FunctionNode function) {
switch (function.asyncMarker) {
case AsyncMarker.Sync:
case AsyncMarker.Async:
@@ -408,7 +406,7 @@ class TypeCheckingVisitor
Substitution _instantiateFunction(List<TypeParameter> typeParameters,
List<DartType> typeArguments, TreeNode where,
{Substitution receiverSubstitution}) {
{Substitution? receiverSubstitution}) {
Substitution instantiation =
Substitution.fromPairs(typeParameters, typeArguments);
Substitution substitution = receiverSubstitution == null
@@ -459,7 +457,7 @@ class TypeCheckingVisitor
.computeThisFunctionType(class_.enclosingLibrary.nonNullable),
typeParameters: class_.typeParameters);
return new InterfaceType(
target.enclosingClass, currentLibrary.nonNullable, arguments.types);
target.enclosingClass, currentLibrary!.nonNullable, arguments.types);
}
@override
@@ -470,7 +468,7 @@ class TypeCheckingVisitor
@override
DartType visitFunctionExpression(FunctionExpression node) {
handleNestedFunctionNode(node.function);
return node.function.computeThisFunctionType(currentLibrary.nonNullable);
return node.function.computeThisFunctionType(currentLibrary!.nonNullable);
}
@override
@@ -491,7 +489,7 @@ class TypeCheckingVisitor
@override
DartType visitLet(Let node) {
DartType value = visitExpression(node.variable.initializer);
DartType value = visitExpression(node.variable.initializer!);
if (node.variable.type is DynamicType) {
node.variable.type = value;
}
@@ -509,12 +507,12 @@ class TypeCheckingVisitor
DartType type = visitExpression(node.expression);
if (type is! FunctionType) {
fail(node, 'Not a function type: $type');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
FunctionType functionType = type;
if (functionType.typeParameters.length != node.typeArguments.length) {
fail(node, 'Wrong number of type arguments');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
return _instantiateFunction(
functionType.typeParameters, node.typeArguments, node)
@@ -527,7 +525,7 @@ class TypeCheckingVisitor
node.expressions[i] =
checkAndDowncastExpression(node.expressions[i], node.typeArgument);
}
return environment.listType(node.typeArgument, currentLibrary.nonNullable);
return environment.listType(node.typeArgument, currentLibrary!.nonNullable);
}
@override
@@ -536,7 +534,7 @@ class TypeCheckingVisitor
node.expressions[i] =
checkAndDowncastExpression(node.expressions[i], node.typeArgument);
}
return environment.setType(node.typeArgument, currentLibrary.nonNullable);
return environment.setType(node.typeArgument, currentLibrary!.nonNullable);
}
@override
@@ -555,7 +553,7 @@ class TypeCheckingVisitor
entry.value = checkAndDowncastExpression(entry.value, node.valueType);
}
return environment.mapType(
node.keyType, node.valueType, currentLibrary.nonNullable);
node.keyType, node.valueType, currentLibrary!.nonNullable);
}
DartType handleDynamicCall(DartType receiver, Arguments arguments) {
@@ -568,15 +566,15 @@ class TypeCheckingVisitor
TreeNode access, FunctionType function, Arguments arguments) {
if (function.requiredParameterCount > arguments.positional.length) {
fail(access, 'Too few positional arguments');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
if (function.positionalParameters.length < arguments.positional.length) {
fail(access, 'Too many positional arguments');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
if (function.typeParameters.length != arguments.types.length) {
fail(access, 'Wrong number of type arguments');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
Substitution instantiation =
Substitution.fromPairs(function.typeParameters, arguments.types);
@@ -589,7 +587,7 @@ class TypeCheckingVisitor
}
for (int i = 0; i < arguments.named.length; ++i) {
NamedExpression argument = arguments.named[i];
DartType parameterType = function.getNamedParameter(argument.name);
DartType? parameterType = function.getNamedParameter(argument.name);
if (parameterType != null) {
DartType expectedType =
instantiation.substituteType(parameterType, contravariant: true);
@@ -597,7 +595,7 @@ class TypeCheckingVisitor
checkAndDowncastExpression(argument.value, expectedType);
} else {
fail(argument.value, 'Unexpected named parameter: ${argument.name}');
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
}
return instantiation.substituteType(function.returnType);
@@ -605,7 +603,7 @@ class TypeCheckingVisitor
@override
DartType visitMethodInvocation(MethodInvocation node) {
Member target = node.interfaceTarget;
Member? target = node.interfaceTarget;
if (target == null) {
DartType receiver = visitExpression(node.receiver);
if (node.name.text == '==') {
@@ -626,34 +624,31 @@ class TypeCheckingVisitor
receiver, argument);
} else {
return handleCall(node.arguments, target.getterType,
receiver: getReceiverType(node, node.receiver, node.interfaceTarget));
receiver: getReceiverType(node, node.receiver, target));
}
}
@override
DartType visitPropertyGet(PropertyGet node) {
if (node.interfaceTarget == null) {
Member? target = node.interfaceTarget;
if (target == null) {
final DartType receiver = visitExpression(node.receiver);
checkUnresolvedInvocation(receiver, node);
return const DynamicType();
} else {
Substitution receiver =
getReceiverType(node, node.receiver, node.interfaceTarget);
return receiver.substituteType(node.interfaceTarget.getterType);
Substitution receiver = getReceiverType(node, node.receiver, target);
return receiver.substituteType(target.getterType);
}
}
@override
DartType visitPropertySet(PropertySet node) {
Member? target = node.interfaceTarget;
DartType value = visitExpression(node.value);
if (node.interfaceTarget != null) {
Substitution receiver =
getReceiverType(node, node.receiver, node.interfaceTarget);
checkAssignable(
node.value,
value,
receiver.substituteType(node.interfaceTarget.setterType,
contravariant: true));
if (target != null) {
Substitution receiver = getReceiverType(node, node.receiver, target);
checkAssignable(node.value, value,
receiver.substituteType(target.setterType, contravariant: true));
} else {
final DartType receiver = visitExpression(node.receiver);
checkUnresolvedInvocation(receiver, node);
@@ -680,7 +675,7 @@ class TypeCheckingVisitor
@override
DartType visitRethrow(Rethrow node) {
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
@override
@@ -708,8 +703,8 @@ class TypeCheckingVisitor
@override
DartType visitListConcatenation(ListConcatenation node) {
DartType type =
environment.iterableType(node.typeArgument, currentLibrary.nonNullable);
DartType type = environment.iterableType(
node.typeArgument, currentLibrary!.nonNullable);
for (Expression part in node.lists) {
DartType partType = visitExpression(part);
checkAssignable(node, type, partType);
@@ -719,8 +714,8 @@ class TypeCheckingVisitor
@override
DartType visitSetConcatenation(SetConcatenation node) {
DartType type =
environment.iterableType(node.typeArgument, currentLibrary.nonNullable);
DartType type = environment.iterableType(
node.typeArgument, currentLibrary!.nonNullable);
for (Expression part in node.sets) {
DartType partType = visitExpression(part);
checkAssignable(node, type, partType);
@@ -731,7 +726,7 @@ class TypeCheckingVisitor
@override
DartType visitMapConcatenation(MapConcatenation node) {
DartType type = environment.mapType(
node.keyType, node.valueType, currentLibrary.nonNullable);
node.keyType, node.valueType, currentLibrary!.nonNullable);
for (Expression part in node.maps) {
DartType partType = visitExpression(part);
checkAssignable(node, type, partType);
@@ -749,7 +744,7 @@ class TypeCheckingVisitor
checkAssignable(node, fieldType, valueType);
});
return new InterfaceType(
node.classNode, currentLibrary.nonNullable, node.typeArguments);
node.classNode, currentLibrary!.nonNullable, node.typeArguments);
}
@override
@@ -764,38 +759,38 @@ class TypeCheckingVisitor
@override
DartType visitSuperMethodInvocation(SuperMethodInvocation node) {
if (node.interfaceTarget == null) {
checkUnresolvedInvocation(currentThisType, node);
return handleDynamicCall(currentThisType, node.arguments);
Member? target = node.interfaceTarget;
if (target == null) {
checkUnresolvedInvocation(currentThisType!, node);
return handleDynamicCall(currentThisType!, node.arguments);
} else {
return handleCall(node.arguments, node.interfaceTarget.getterType,
receiver: getSuperReceiverType(node.interfaceTarget));
return handleCall(node.arguments, target.getterType,
receiver: getSuperReceiverType(target));
}
}
@override
DartType visitSuperPropertyGet(SuperPropertyGet node) {
if (node.interfaceTarget == null) {
checkUnresolvedInvocation(currentThisType, node);
Member? target = node.interfaceTarget;
if (target == null) {
checkUnresolvedInvocation(currentThisType!, node);
return const DynamicType();
} else {
Substitution receiver = getSuperReceiverType(node.interfaceTarget);
return receiver.substituteType(node.interfaceTarget.getterType);
Substitution receiver = getSuperReceiverType(target);
return receiver.substituteType(target.getterType);
}
}
@override
DartType visitSuperPropertySet(SuperPropertySet node) {
Member? target = node.interfaceTarget;
DartType value = visitExpression(node.value);
if (node.interfaceTarget != null) {
Substitution receiver = getSuperReceiverType(node.interfaceTarget);
checkAssignable(
node.value,
value,
receiver.substituteType(node.interfaceTarget.setterType,
contravariant: true));
if (target != null) {
Substitution receiver = getSuperReceiverType(target);
checkAssignable(node.value, value,
receiver.substituteType(target.setterType, contravariant: true));
} else {
checkUnresolvedInvocation(currentThisType, node);
checkUnresolvedInvocation(currentThisType!, node);
}
return value;
}
@@ -807,13 +802,13 @@ class TypeCheckingVisitor
@override
DartType visitThisExpression(ThisExpression node) {
return currentThisType;
return currentThisType!;
}
@override
DartType visitThrow(Throw node) {
visitExpression(node.expression);
return NeverType.fromNullability(currentLibrary.nonNullable);
return NeverType.fromNullability(currentLibrary!.nonNullable);
}
@override
@@ -836,7 +831,7 @@ class TypeCheckingVisitor
@override
DartType visitLoadLibrary(LoadLibrary node) {
return environment.futureType(
const DynamicType(), currentLibrary.nonNullable);
const DynamicType(), currentLibrary!.nonNullable);
}
@override
@@ -853,7 +848,7 @@ class TypeCheckingVisitor
visitAssertStatement(AssertStatement node) {
visitExpression(node.condition);
if (node.message != null) {
visitExpression(node.message);
visitExpression(node.message!);
}
}
@@ -907,25 +902,25 @@ class TypeCheckingVisitor
DartType getIterableElementType(DartType iterable) {
if (iterable is InterfaceType) {
Member iteratorGetter =
Member? iteratorGetter =
hierarchy.getInterfaceMember(iterable.classNode, iteratorName);
if (iteratorGetter == null) return const DynamicType();
List<DartType> castedIterableArguments =
hierarchy.getTypeArgumentsAsInstanceOf(
iterable, iteratorGetter.enclosingClass);
iterable, iteratorGetter.enclosingClass!)!;
DartType iteratorType = Substitution.fromPairs(
iteratorGetter.enclosingClass.typeParameters,
iteratorGetter.enclosingClass!.typeParameters,
castedIterableArguments)
.substituteType(iteratorGetter.getterType);
if (iteratorType is InterfaceType) {
Member currentGetter =
Member? currentGetter =
hierarchy.getInterfaceMember(iteratorType.classNode, currentName);
if (currentGetter == null) return const DynamicType();
List<DartType> castedIteratorTypeArguments =
hierarchy.getTypeArgumentsAsInstanceOf(
iteratorType, currentGetter.enclosingClass);
iteratorType, currentGetter.enclosingClass!)!;
return Substitution.fromPairs(
currentGetter.enclosingClass.typeParameters,
currentGetter.enclosingClass!.typeParameters,
castedIteratorTypeArguments)
.substituteType(currentGetter.getterType);
}
@@ -935,7 +930,7 @@ class TypeCheckingVisitor
DartType getStreamElementType(DartType stream) {
if (stream is InterfaceType) {
List<DartType> asStreamArguments =
List<DartType>? asStreamArguments =
hierarchy.getTypeArgumentsAsInstanceOf(stream, coreTypes.streamClass);
if (asStreamArguments == null) return const DynamicType();
return asStreamArguments.single;
@@ -948,7 +943,7 @@ class TypeCheckingVisitor
node.variables.forEach(visitVariableDeclaration);
if (node.condition != null) {
node.condition = checkAndDowncastExpression(
node.condition, environment.coreTypes.boolLegacyRawType);
node.condition!, environment.coreTypes.boolLegacyRawType);
}
node.updates.forEach(visitExpression);
visitStatement(node.body);
@@ -965,7 +960,7 @@ class TypeCheckingVisitor
node.condition, environment.coreTypes.boolLegacyRawType);
visitStatement(node.then);
if (node.otherwise != null) {
visitStatement(node.otherwise);
visitStatement(node.otherwise!);
}
}
@@ -976,15 +971,16 @@ class TypeCheckingVisitor
@override
visitReturnStatement(ReturnStatement node) {
if (node.expression != null) {
Expression? expression = node.expression;
if (expression != null) {
if (currentReturnType == null) {
fail(node, 'Return of a value from void method');
} else {
DartType type = visitExpression(node.expression);
DartType type = visitExpression(expression);
if (currentAsyncMarker == AsyncMarker.Async) {
type = environment.flatten(type);
}
checkAssignable(node.expression, type, currentReturnType);
checkAssignable(expression, type, currentReturnType!);
}
}
}
@@ -1016,7 +1012,7 @@ class TypeCheckingVisitor
visitVariableDeclaration(VariableDeclaration node) {
if (node.initializer != null) {
node.initializer =
checkAndDowncastExpression(node.initializer, node.type);
checkAndDowncastExpression(node.initializer!, node.type);
}
}
@@ -1034,18 +1030,18 @@ class TypeCheckingVisitor
? coreTypes.streamClass
: coreTypes.iterableClass;
DartType type = visitExpression(node.expression);
List<DartType> asContainerArguments = type is InterfaceType
List<DartType>? asContainerArguments = type is InterfaceType
? hierarchy.getTypeArgumentsAsInstanceOf(type, container)
: null;
if (asContainerArguments != null) {
checkAssignable(
node.expression, asContainerArguments[0], currentYieldType);
node.expression, asContainerArguments[0], currentYieldType!);
} else {
fail(node.expression, '$type is not an instance of $container');
}
} else {
node.expression =
checkAndDowncastExpression(node.expression, currentYieldType);
checkAndDowncastExpression(node.expression, currentYieldType!);
}
}