[cfe] Use type parser in type schema elimination tests

Closes: #44095.

Bug: https://github.com/dart-lang/sdk/issues/44095
Change-Id: Iff15b0686557388c4e701075a4f6e4fc16dfa5fa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170901
Commit-Queue: Dmitry Stefantsov <dmitryas@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
This commit is contained in:
Dmitry Stefantsov 2020-11-09 16:54:39 +00:00 committed by commit-bot@chromium.org
parent 61612b54de
commit 783d83950b
4 changed files with 88 additions and 191 deletions

View file

@ -6,7 +6,7 @@ import 'package:front_end/src/fasta/type_inference/type_schema.dart';
import 'package:front_end/src/fasta/type_inference/type_schema_elimination.dart'
as typeSchemaElimination;
import 'package:kernel/ast.dart';
import 'package:kernel/core_types.dart';
import 'package:kernel/testing/type_parser_environment.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
@ -18,13 +18,10 @@ main() {
@reflectiveTest
class TypeSchemaEliminationTest {
static const DartType unknownType = const UnknownType();
CoreTypes coreTypes = new _MockCoreTypes();
DartType get dynamicType => const DynamicType();
DartType get nullType => new NullType();
final Env env = new Env("");
final Map<String, DartType Function()> additionalTypes = {
"UNKNOWN": () => new UnknownType()
};
DartType greatestClosure(DartType schema) {
return typeSchemaElimination.greatestClosure(
@ -36,116 +33,62 @@ class TypeSchemaEliminationTest {
schema, const DynamicType(), const NeverType(Nullability.nonNullable));
}
void testGreatest(String type, String expectedClosure) {
expect(
greatestClosure(env.parseType(type, additionalTypes: additionalTypes)),
env.parseType(expectedClosure, additionalTypes: additionalTypes));
}
void testLeast(String type, String expectedClosure) {
expect(leastClosure(env.parseType(type, additionalTypes: additionalTypes)),
env.parseType(expectedClosure, additionalTypes: additionalTypes));
}
void test_greatestClosure_contravariant() {
expect(
greatestClosure(new FunctionType(
[unknownType], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'(Never) →* dynamic');
expect(
greatestClosure(new FunctionType([], dynamicType, Nullability.legacy,
namedParameters: [new NamedType('foo', unknownType)]))
.leakingDebugToString(),
'({foo: Never}) →* dynamic');
testGreatest("(UNKNOWN) ->* dynamic", "(Never) ->* dynamic");
testGreatest("({UNKNOWN foo}) ->* dynamic", "({Never foo}) ->* dynamic");
}
void test_greatestClosure_contravariant_contravariant() {
expect(
greatestClosure(new FunctionType([
new FunctionType([unknownType], dynamicType, Nullability.legacy)
], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'((dynamic) →* dynamic) →* dynamic');
testGreatest("((UNKNOWN) ->* dynamic) ->* dynamic",
"((dynamic) ->* dynamic) ->* dynamic");
}
void test_greatestClosure_covariant() {
expect(
greatestClosure(new FunctionType([], unknownType, Nullability.legacy))
.leakingDebugToString(),
'() →* dynamic');
expect(
greatestClosure(new InterfaceType(
coreTypes.listClass, Nullability.legacy, [unknownType]))
.leakingDebugToString(),
'dart.core::List<dynamic>*');
testGreatest("() ->* UNKNOWN", "() ->* dynamic");
testGreatest("List<UNKNOWN>*", "List<dynamic>*");
}
void test_greatestClosure_function_multipleUnknown() {
expect(
greatestClosure(new FunctionType(
[unknownType, unknownType], unknownType, Nullability.legacy,
namedParameters: [
new NamedType('a', unknownType),
new NamedType('b', unknownType)
])).leakingDebugToString(),
'(Never, Never, {a: Never, b: Never}) →* dynamic');
testGreatest("(UNKNOWN, UNKNOWN, {UNKNOWN a, UNKNOWN b}) ->* UNKNOWN",
"(Never, Never, {Never a, Never b}) ->* dynamic");
}
void test_greatestClosure_simple() {
expect(greatestClosure(unknownType).leakingDebugToString(), 'dynamic');
testGreatest("UNKNOWN", "dynamic");
}
void test_leastClosure_contravariant() {
expect(
leastClosure(new FunctionType(
[unknownType], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'(dynamic) →* dynamic');
expect(
leastClosure(new FunctionType([], dynamicType, Nullability.legacy,
namedParameters: [new NamedType('foo', unknownType)]))
.leakingDebugToString(),
'({foo: dynamic}) →* dynamic');
testLeast("(UNKNOWN) ->* dynamic", "(dynamic) ->* dynamic");
testLeast("({UNKNOWN foo}) ->* dynamic", "({dynamic foo}) ->* dynamic");
}
void test_leastClosure_contravariant_contravariant() {
expect(
leastClosure(new FunctionType([
new FunctionType([unknownType], dynamicType, Nullability.legacy)
], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'((Never) →* dynamic) →* dynamic');
testLeast("((UNKNOWN) ->* dynamic) ->* dynamic",
"((Never) ->* dynamic) ->* dynamic");
}
void test_leastClosure_covariant() {
expect(
leastClosure(new FunctionType([], unknownType, Nullability.legacy))
.leakingDebugToString(),
'() →* Never');
expect(
leastClosure(new InterfaceType(
coreTypes.listClass, Nullability.legacy, [unknownType]))
.leakingDebugToString(),
'dart.core::List<Never>*');
testLeast("() ->* UNKNOWN", "() ->* Never");
testLeast("List<UNKNOWN>*", "List<Never>*");
}
void test_leastClosure_function_multipleUnknown() {
expect(
leastClosure(new FunctionType(
[unknownType, unknownType], unknownType, Nullability.legacy,
namedParameters: [
new NamedType('a', unknownType),
new NamedType('b', unknownType)
])).leakingDebugToString(),
'(dynamic, dynamic, {a: dynamic, b: dynamic}) →* Never');
testLeast("(UNKNOWN, UNKNOWN, {UNKNOWN a, UNKNOWN b}) ->* UNKNOWN",
"(dynamic, dynamic, {dynamic a, dynamic b}) ->* Never");
}
void test_leastClosure_simple() {
expect(leastClosure(unknownType).leakingDebugToString(), 'Never');
testLeast("UNKNOWN", "Never");
}
}
class _MockCoreTypes implements CoreTypes {
@override
final Class listClass = new Class(name: 'List');
@override
final Class objectClass = new Class(name: 'Object');
_MockCoreTypes() {
new Library(Uri.parse('dart:core'),
name: 'dart.core', classes: [listClass, objectClass]);
}
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

View file

@ -6,7 +6,7 @@ import 'package:front_end/src/fasta/type_inference/type_schema.dart';
import 'package:front_end/src/fasta/type_inference/type_schema_elimination.dart'
as typeSchemaElimination;
import 'package:kernel/ast.dart';
import 'package:kernel/core_types.dart';
import 'package:kernel/testing/type_parser_environment.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
@ -18,134 +18,77 @@ main() {
@reflectiveTest
class TypeSchemaEliminationTest {
static const DartType unknownType = const UnknownType();
CoreTypes coreTypes = new _MockCoreTypes();
DartType get dynamicType => const DynamicType();
DartType get objectType => coreTypes.objectLegacyRawType;
final Env env = new Env("");
final Map<String, DartType Function()> additionalTypes = {
"UNKNOWN": () => new UnknownType()
};
DartType greatestClosure(DartType schema) {
return typeSchemaElimination.greatestClosure(
schema, dynamicType, const NullType());
schema, new DynamicType(), new NullType());
}
DartType leastClosure(DartType schema) {
return typeSchemaElimination.leastClosure(
schema, dynamicType, const NullType());
schema, new DynamicType(), new NullType());
}
void testGreatest(String type, String expectedClosure) {
expect(
greatestClosure(env.parseType(type, additionalTypes: additionalTypes)),
env.parseType(expectedClosure, additionalTypes: additionalTypes));
}
void testLeast(String type, String expectedClosure) {
expect(leastClosure(env.parseType(type, additionalTypes: additionalTypes)),
env.parseType(expectedClosure, additionalTypes: additionalTypes));
}
void test_greatestClosure_contravariant() {
expect(
greatestClosure(new FunctionType(
[unknownType], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'(Null) →* dynamic');
expect(
greatestClosure(new FunctionType([], dynamicType, Nullability.legacy,
namedParameters: [new NamedType('foo', unknownType)]))
.leakingDebugToString(),
'({foo: Null}) →* dynamic');
testGreatest("(UNKNOWN) ->* dynamic", "(Null) ->* dynamic");
testGreatest("({UNKNOWN foo}) ->* dynamic", "({Null foo}) ->* dynamic");
}
void test_greatestClosure_contravariant_contravariant() {
expect(
greatestClosure(new FunctionType([
new FunctionType([unknownType], dynamicType, Nullability.legacy)
], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'((dynamic) →* dynamic) →* dynamic');
testGreatest("((UNKNOWN) ->* dynamic) ->* dynamic",
"((dynamic) ->* dynamic) ->* dynamic");
}
void test_greatestClosure_covariant() {
expect(
greatestClosure(new FunctionType([], unknownType, Nullability.legacy))
.leakingDebugToString(),
'() →* dynamic');
expect(
greatestClosure(new InterfaceType(
coreTypes.listClass, Nullability.legacy, [unknownType]))
.leakingDebugToString(),
'dart.core::List<dynamic>*');
testGreatest("() ->* UNKNOWN", "() ->* dynamic");
testGreatest("List<UNKNOWN>*", "List<dynamic>*");
}
void test_greatestClosure_function_multipleUnknown() {
expect(
greatestClosure(new FunctionType(
[unknownType, unknownType], unknownType, Nullability.legacy,
namedParameters: [
new NamedType('a', unknownType),
new NamedType('b', unknownType)
])).leakingDebugToString(),
'(Null, Null, {a: Null, b: Null}) →* dynamic');
testGreatest("(UNKNOWN, UNKNOWN, {UNKNOWN a, UNKNOWN b}) ->* UNKNOWN",
"(Null, Null, {Null a, Null b}) ->* dynamic");
}
void test_greatestClosure_simple() {
expect(greatestClosure(unknownType).leakingDebugToString(), 'dynamic');
testGreatest("UNKNOWN", "dynamic");
}
void test_leastClosure_contravariant() {
expect(
leastClosure(new FunctionType(
[unknownType], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'(dynamic) →* dynamic');
expect(
leastClosure(new FunctionType([], dynamicType, Nullability.legacy,
namedParameters: [new NamedType('foo', unknownType)]))
.leakingDebugToString(),
'({foo: dynamic}) →* dynamic');
testLeast("(UNKNOWN) ->* dynamic", "(dynamic) ->* dynamic");
testLeast("({UNKNOWN foo}) ->* dynamic", "({dynamic foo}) ->* dynamic");
}
void test_leastClosure_contravariant_contravariant() {
expect(
leastClosure(new FunctionType([
new FunctionType([unknownType], dynamicType, Nullability.legacy)
], dynamicType, Nullability.legacy))
.leakingDebugToString(),
'((Null) →* dynamic) →* dynamic');
testLeast("((UNKNOWN) ->* UNKNOWN) ->* dynamic",
"((Null) ->* dynamic) ->* dynamic");
}
void test_leastClosure_covariant() {
expect(
leastClosure(new FunctionType([], unknownType, Nullability.legacy))
.leakingDebugToString(),
'() →* Null');
expect(
leastClosure(new InterfaceType(
coreTypes.listClass, Nullability.legacy, [unknownType]))
.leakingDebugToString(),
'dart.core::List<Null>*');
testLeast("() ->* UNKNOWN", "() ->* Null");
testLeast("List<UNKNOWN>*", "List<Null>*");
}
void test_leastClosure_function_multipleUnknown() {
expect(
leastClosure(new FunctionType(
[unknownType, unknownType], unknownType, Nullability.legacy,
namedParameters: [
new NamedType('a', unknownType),
new NamedType('b', unknownType)
])).leakingDebugToString(),
'(dynamic, dynamic, {a: dynamic, b: dynamic}) →* Null');
testLeast("(UNKNOWN, UNKNOWN, {UNKNOWN a, UNKNOWN b}) ->* UNKNOWN",
"(dynamic, dynamic, {dynamic a, dynamic b}) ->* Null");
}
void test_leastClosure_simple() {
expect(leastClosure(unknownType).leakingDebugToString(), 'Null');
testLeast("UNKNOWN", "Null");
}
}
class _MockCoreTypes implements CoreTypes {
@override
final Class listClass = new Class(name: 'List');
@override
final Class objectClass = new Class(name: 'Object');
_MockCoreTypes() {
new Library(Uri.parse('dart:core'),
name: 'dart.core', classes: [listClass, objectClass]);
}
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

View file

@ -1784,6 +1784,7 @@ maintain
maintains
major
make
makers
makes
making
malformed

View file

@ -114,8 +114,10 @@ class Env {
coreTypes = new CoreTypes(component);
}
DartType parseType(String text) {
return _libraryEnvironment.parseType(text);
DartType parseType(String text,
{Map<String, DartType Function()> additionalTypes}) {
return _libraryEnvironment.parseType(text,
additionalTypes: additionalTypes);
}
void extendWithTypeParameters(String typeParameters) {
@ -154,13 +156,17 @@ class TypeParserEnvironment {
TypeParserEnvironment(this.uri, this.fileUri, [this._parent]);
Node _kernelFromParsedType(ParsedType type) {
Node node = type.accept(const _KernelFromParsedType(), this);
Node _kernelFromParsedType(ParsedType type,
{Map<String, DartType Function()> additionalTypes}) {
Node node = type.accept(
new _KernelFromParsedType(additionalTypes: additionalTypes), this);
return node;
}
DartType parseType(String text) {
return _kernelFromParsedType(type_parser.parse(text).single);
DartType parseType(String text,
{Map<String, DartType Function()> additionalTypes}) {
return _kernelFromParsedType(type_parser.parse(text).single,
additionalTypes: additionalTypes);
}
bool isObject(String name) => name == "Object" && "$uri" == "dart:core";
@ -209,7 +215,9 @@ class TypeParserEnvironment {
}
class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
const _KernelFromParsedType();
final Map<String, DartType Function()> additionalTypes; // Can be null.
const _KernelFromParsedType({this.additionalTypes});
DartType visitInterfaceType(
ParsedInterfaceType node, TypeParserEnvironment environment) {
@ -237,6 +245,8 @@ class _KernelFromParsedType implements Visitor<Node, TypeParserEnvironment> {
// Don't return a const object to ensure we test implementations that use
// identical.
return new NullType();
} else if (additionalTypes != null && additionalTypes.containsKey(name)) {
return additionalTypes[name].call();
}
TreeNode declaration = environment.lookupDeclaration(name);
List<ParsedType> arguments = node.arguments;