From 08fb2161f73c864e2c813b37137b32e13a122dc9 Mon Sep 17 00:00:00 2001 From: John Messerly Date: Mon, 18 May 2015 13:41:25 -0700 Subject: [PATCH] fixes #184, type literals now use emitTypeName this fixes `dart.dynamic` as well as other cases covered by emitTypeName R=vsm@google.com Review URL: https://codereview.chromium.org/1142713004 --- pkg/dev_compiler/lib/src/codegen/js_codegen.dart | 7 +++++++ pkg/dev_compiler/lib/src/utils.dart | 9 ++++++--- pkg/dev_compiler/test/codegen/expect/misc.js | 15 +++++++++++++++ pkg/dev_compiler/test/codegen/misc.dart | 12 ++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/pkg/dev_compiler/lib/src/codegen/js_codegen.dart b/pkg/dev_compiler/lib/src/codegen/js_codegen.dart index 8af06b9dfb02..3ba321bf2cf8 100644 --- a/pkg/dev_compiler/lib/src/codegen/js_codegen.dart +++ b/pkg/dev_compiler/lib/src/codegen/js_codegen.dart @@ -991,6 +991,13 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor { var name = element.name; + // type literal + if (element is ClassElement || + element is DynamicElementImpl || + element is FunctionTypeAliasElement) { + return _emitTypeName(fillDynamicTypeArgs(element.type, types)); + } + // library member if (element.enclosingElement is CompilationUnitElement) { return _maybeQualifiedName( diff --git a/pkg/dev_compiler/lib/src/utils.dart b/pkg/dev_compiler/lib/src/utils.dart index ba0e0354a79b..4c7339520c67 100644 --- a/pkg/dev_compiler/lib/src/utils.dart +++ b/pkg/dev_compiler/lib/src/utils.dart @@ -375,9 +375,12 @@ Object getConstantField( return (f == null || f.type != expectedType) ? null : f.value; } -ParameterizedType fillDynamicTypeArgs(ParameterizedType t, TypeProvider types) { - var dyn = new List.filled(t.typeArguments.length, types.dynamicType); - return t.substitute2(dyn, t.typeArguments); +DartType fillDynamicTypeArgs(DartType t, TypeProvider types) { + if (t is ParameterizedType) { + var dyn = new List.filled(t.typeArguments.length, types.dynamicType); + return t.substitute2(dyn, t.typeArguments); + } + return t; } /// Similar to [SimpleIdentifier] inGetterContext, inSetterContext, and diff --git a/pkg/dev_compiler/test/codegen/expect/misc.js b/pkg/dev_compiler/test/codegen/expect/misc.js index 9c4419fb6307..e46d97eef901 100644 --- a/pkg/dev_compiler/test/codegen/expect/misc.js +++ b/pkg/dev_compiler/test/codegen/expect/misc.js @@ -7,13 +7,28 @@ var core = dart.import(core); } } let UNINITIALIZED = dart.const(new _Uninitialized()); + let Generic$ = dart.generic(function(T) { + class Generic extends core.Object { + get type() { + return Generic$(); + } + } + return Generic; + }); + let Generic = Generic$(); // Function main: () → dynamic function main() { core.print(dart.toString(1)); core.print(dart.toString(1.0)); core.print(dart.toString(1.1)); + let x = 42; + core.print(dart.equals(x, dart.dynamic)); + core.print(dart.equals(x, Generic)); + core.print(new (Generic$(core.int))().type); } // Exports: exports.UNINITIALIZED = UNINITIALIZED; + exports.Generic$ = Generic$; + exports.Generic = Generic; exports.main = main; })(misc, core); diff --git a/pkg/dev_compiler/test/codegen/misc.dart b/pkg/dev_compiler/test/codegen/misc.dart index 8081cce4421a..4bde2571312e 100644 --- a/pkg/dev_compiler/test/codegen/misc.dart +++ b/pkg/dev_compiler/test/codegen/misc.dart @@ -7,9 +7,21 @@ const UNINITIALIZED = const _Uninitialized(); class _Uninitialized { const _Uninitialized(); } +class Generic { + Type get type => Generic; +} + main() { // Number literals in call expressions. print(1.toString()); print(1.0.toString()); print(1.1.toString()); + + // Type literals, #184 + dynamic x = 42; + print(x == dynamic); + print(x == Generic); + + // Should be Generic + print(new Generic().type); }