Skip to content

Commit

Permalink
[cfe] Handle UnknownType using defaultDartType
Browse files Browse the repository at this point in the history
Change-Id: I4c3d805a4be6a6f95d08869325da077b654436d7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/114517
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
  • Loading branch information
johnniwinther authored and commit-bot@chromium.org committed Sep 9, 2019
1 parent 129d4a6 commit ff0fda7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
15 changes: 9 additions & 6 deletions pkg/front_end/lib/src/fasta/kernel/verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:kernel/ast.dart'
AsExpression,
Class,
Component,
DartType,
ExpressionStatement,
Field,
Let,
Expand All @@ -32,7 +33,7 @@ import '../fasta_codes.dart'

import '../severity.dart' show Severity;

import '../type_inference/type_schema.dart' show TypeSchemaVisitor, UnknownType;
import '../type_inference/type_schema.dart' show UnknownType;

import 'kernel_shadow_ast.dart' show SyntheticExpressionJudgment;

Expand All @@ -47,8 +48,7 @@ List<LocatedMessage> verifyComponent(Component component,
return verifier.errors;
}

class FastaVerifyingVisitor extends VerifyingVisitor
implements TypeSchemaVisitor<void> {
class FastaVerifyingVisitor extends VerifyingVisitor {
final List<LocatedMessage> errors = <LocatedMessage>[];

Uri fileUri;
Expand Down Expand Up @@ -174,9 +174,12 @@ class FastaVerifyingVisitor extends VerifyingVisitor
}

@override
visitUnknownType(UnknownType node) {
// Note: we can't pass [node] to [problem] because it's not a [TreeNode].
problem(null, "Unexpected appearance of the unknown type.");
defaultDartType(DartType node) {
if (node is UnknownType) {
// Note: we can't pass [node] to [problem] because it's not a [TreeNode].
problem(null, "Unexpected appearance of the unknown type.");
}
super.defaultDartType(node);
}

@override
Expand Down
28 changes: 5 additions & 23 deletions pkg/front_end/lib/src/fasta/type_inference/type_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ String typeSchemaToString(DartType schema) {
}

/// Extension of [Printer] that represents the unknown type as `?`.
class TypeSchemaPrinter extends Printer implements TypeSchemaVisitor<Null> {
class TypeSchemaPrinter extends Printer {
TypeSchemaPrinter(StringSink sink,
{NameSystem syntheticNames,
bool showExternal,
Expand All @@ -47,17 +47,11 @@ class TypeSchemaPrinter extends Printer implements TypeSchemaVisitor<Null> {
annotator: annotator);

@override
visitUnknownType(UnknownType node) {
defaultDartType(covariant UnknownType node) {
writeWord('?');
}
}

/// Extension of [DartTypeVisitor] which can visit [UnknownType].
class TypeSchemaVisitor<R> extends DartTypeVisitor<R> {
/// Called when [UnknownType] is visited.
R visitUnknownType(UnknownType node) => defaultDartType(node);
}

/// The unknown type (denoted `?`) is an object which can appear anywhere that
/// a type is expected. It represents a component of a type which has not yet
/// been fixed by inference.
Expand All @@ -78,16 +72,7 @@ class UnknownType extends DartType {

@override
R accept<R>(DartTypeVisitor<R> v) {
if (v is TypeSchemaVisitor<R>) {
return v.visitUnknownType(this);
} else {
// Note: in principle it seems like this should throw, since any visitor
// that operates on a type schema ought to inherit from TypeSchemaVisitor.
// However, that would make it impossible to use toString() on any type
// schema, since toString() uses the kernel's Printer visitor, which can't
// possibly inherit from TypeSchemaVisitor since it's inside kernel.
return v.defaultDartType(this);
}
return v.defaultDartType(this);
}

@override
Expand All @@ -99,9 +84,9 @@ class UnknownType extends DartType {
}

/// Visitor that computes [isKnown].
class _IsKnownVisitor extends TypeSchemaVisitor<bool> {
class _IsKnownVisitor extends DartTypeVisitor<bool> {
@override
bool defaultDartType(DartType node) => true;
bool defaultDartType(DartType node) => node is! UnknownType;

@override
bool visitFunctionType(FunctionType node) {
Expand Down Expand Up @@ -130,7 +115,4 @@ class _IsKnownVisitor extends TypeSchemaVisitor<bool> {
}
return true;
}

@override
bool visitUnknownType(UnknownType node) => false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
// BSD-style license that can be found in the LICENSE.md file.

import 'package:kernel/ast.dart'
show DartType, DynamicType, FunctionType, InterfaceType, NamedType;
show
DartType,
DartTypeVisitor,
DynamicType,
FunctionType,
InterfaceType,
NamedType;

import 'package:kernel/core_types.dart' show CoreTypes;

import 'type_schema.dart' show TypeSchemaVisitor, UnknownType;
import 'type_schema.dart' show UnknownType;

/// Returns the greatest closure of the given type [schema] with respect to `?`.
///
Expand Down Expand Up @@ -46,7 +52,7 @@ DartType leastClosure(CoreTypes coreTypes, DartType schema) =>
/// Each visitor method returns `null` if there are no `?`s contained in the
/// type, otherwise it returns the result of substituting `?` with `Null` or
/// `Object`, as appropriate.
class _TypeSchemaEliminationVisitor extends TypeSchemaVisitor<DartType> {
class _TypeSchemaEliminationVisitor extends DartTypeVisitor<DartType> {
final DartType nullType;

bool isLeastClosure;
Expand Down Expand Up @@ -115,8 +121,12 @@ class _TypeSchemaEliminationVisitor extends TypeSchemaVisitor<DartType> {
}

@override
DartType visitUnknownType(UnknownType node) =>
isLeastClosure ? nullType : const DynamicType();
DartType defaultDartType(DartType node) {
if (node is UnknownType) {
return isLeastClosure ? nullType : const DynamicType();
}
return null;
}

/// Runs an instance of the visitor on the given [schema] and returns the
/// resulting type. If the schema contains no instances of `?`, the original
Expand Down
15 changes: 4 additions & 11 deletions pkg/front_end/test/fasta/type_inference/type_schema_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class _OrdinaryVisitor<R> extends Visitor<R> {
}
}

class _TypeSchemaVisitor<R> extends Visitor<R> implements TypeSchemaVisitor<R> {
class _TypeSchemaVisitor<R> extends Visitor<R> {
final _UnaryFunction<DartType, R> _defaultDartType;
final _UnaryFunction<UnknownType, R> _visitUnknownType;

Expand All @@ -129,19 +129,12 @@ class _TypeSchemaVisitor<R> extends Visitor<R> implements TypeSchemaVisitor<R> {

@override
R defaultDartType(DartType node) {
if (_defaultDartType != null) {
if (node is UnknownType && _visitUnknownType != null) {
return _visitUnknownType(node);
} else if (_defaultDartType != null) {
return _defaultDartType(node);
} else {
return super.defaultDartType(node);
}
}

@override
R visitUnknownType(UnknownType node) {
if (_visitUnknownType != null) {
return _visitUnknownType(node);
} else {
return defaultDartType(node);
}
}
}

0 comments on commit ff0fda7

Please sign in to comment.