| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:expect/expect.dart'; | 5 import 'package:expect/expect.dart'; |
| 6 import "package:async_helper/async_helper.dart"; | 6 import "package:async_helper/async_helper.dart"; |
| 7 import 'package:compiler/src/types/types.dart' | 7 import 'package:compiler/src/types/types.dart' show MapTypeMask, TypeMask; |
| 8 show MapTypeMask, TypeMask; | |
| 9 | 8 |
| 10 import 'compiler_helper.dart'; | 9 import 'compiler_helper.dart'; |
| 11 import 'type_mask_test_helper.dart'; | 10 import 'type_mask_test_helper.dart'; |
| 12 | 11 |
| 13 | |
| 14 String generateTest(String mapAllocation) { | 12 String generateTest(String mapAllocation) { |
| 15 return """ | 13 return """ |
| 16 int anInt = 42; | 14 int anInt = 42; |
| 17 double aDouble = 42.5; | 15 double aDouble = 42.5; |
| 18 String aKey = 'aKey'; | 16 String aKey = 'aKey'; |
| 19 String anotherKey = 'anotherKey'; | 17 String anotherKey = 'anotherKey'; |
| 20 String presetKey = 'presetKey'; | 18 String presetKey = 'presetKey'; |
| 21 | 19 |
| 22 class A { | 20 class A { |
| 23 final field; | 21 final field; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 200 |
| 203 void main() { | 201 void main() { |
| 204 // Test empty literal map | 202 // Test empty literal map |
| 205 doTest('{}'); | 203 doTest('{}'); |
| 206 // Test preset map of <String,uint32> | 204 // Test preset map of <String,uint32> |
| 207 doTest('{presetKey : anInt}', "presetKey", "anInt"); | 205 doTest('{presetKey : anInt}', "presetKey", "anInt"); |
| 208 // Test preset map of <Double,uint32> | 206 // Test preset map of <Double,uint32> |
| 209 doTest('{aDouble : anInt}', "aDouble", "anInt"); | 207 doTest('{aDouble : anInt}', "aDouble", "anInt"); |
| 210 } | 208 } |
| 211 | 209 |
| 212 void doTest(String allocation, [String keyElement, | 210 void doTest(String allocation, [String keyElement, String valueElement]) { |
| 213 String valueElement]) { | |
| 214 Uri uri = new Uri(scheme: 'source'); | 211 Uri uri = new Uri(scheme: 'source'); |
| 215 var compiler = compilerFor(generateTest(allocation), uri, | 212 var compiler = compilerFor(generateTest(allocation), uri, |
| 216 expectedErrors: 0, expectedWarnings: 1); | 213 expectedErrors: 0, expectedWarnings: 1); |
| 217 var classWorld = compiler.world; | 214 var classWorld = compiler.world; |
| 218 asyncTest(() => compiler.run(uri).then((_) { | 215 asyncTest(() => compiler.run(uri).then((_) { |
| 219 var keyType, valueType; | 216 var keyType, valueType; |
| 220 var commonMasks = compiler.commonMasks; | 217 var commonMasks = compiler.commonMasks; |
| 221 var typesInferrer = compiler.globalInference.typesInferrer; | 218 var typesInferrer = compiler.globalInference.typesInferrer; |
| 222 var emptyType = new TypeMask.nonNullEmpty(); | 219 var emptyType = new TypeMask.nonNullEmpty(); |
| 223 var aKeyType = | 220 var aKeyType = |
| 224 typesInferrer.getTypeOfElement(findElement(compiler, 'aKey')); | 221 typesInferrer.getTypeOfElement(findElement(compiler, 'aKey')); |
| 225 if (keyElement != null) { | 222 if (keyElement != null) { |
| 226 keyType = | 223 keyType = |
| 227 typesInferrer.getTypeOfElement(findElement(compiler, keyElement)); | 224 typesInferrer.getTypeOfElement(findElement(compiler, keyElement)); |
| 228 } | 225 } |
| 229 if (valueElement != null) { | 226 if (valueElement != null) { |
| 230 valueType = | 227 valueType = typesInferrer |
| 231 typesInferrer.getTypeOfElement(findElement(compiler, valueElement)); | 228 .getTypeOfElement(findElement(compiler, valueElement)); |
| 232 } | 229 } |
| 233 if (keyType == null) keyType = emptyType; | 230 if (keyType == null) keyType = emptyType; |
| 234 if (valueType == null) valueType = emptyType; | 231 if (valueType == null) valueType = emptyType; |
| 235 | 232 |
| 236 checkType(String name, keyType, valueType) { | 233 checkType(String name, keyType, valueType) { |
| 237 var element = findElement(compiler, name); | 234 var element = findElement(compiler, name); |
| 238 MapTypeMask mask = typesInferrer.getTypeOfElement(element); | 235 MapTypeMask mask = typesInferrer.getTypeOfElement(element); |
| 239 Expect.equals(keyType, simplify(mask.keyType, compiler), name); | 236 Expect.equals(keyType, simplify(mask.keyType, compiler), name); |
| 240 Expect.equals(valueType, simplify(mask.valueType, compiler), name); | 237 Expect.equals(valueType, simplify(mask.valueType, compiler), name); |
| 241 } | 238 } |
| 242 | 239 |
| 243 K(TypeMask other) => simplify(keyType.union(other, classWorld), compiler); | 240 K(TypeMask other) => |
| 244 V(TypeMask other) => | 241 simplify(keyType.union(other, classWorld), compiler); |
| 245 simplify(valueType.union(other, classWorld), compiler).nullable(); | 242 V(TypeMask other) => |
| 243 simplify(valueType.union(other, classWorld), compiler).nullable(); |
| 246 | 244 |
| 247 checkType('mapInField', K(aKeyType), V(commonMasks.numType)); | 245 checkType('mapInField', K(aKeyType), V(commonMasks.numType)); |
| 248 checkType('mapPassedToMethod', K(aKeyType), V(commonMasks.numType)); | 246 checkType('mapPassedToMethod', K(aKeyType), V(commonMasks.numType)); |
| 249 checkType('mapReturnedFromMethod', K(aKeyType), V(commonMasks.numType)); | 247 checkType('mapReturnedFromMethod', K(aKeyType), V(commonMasks.numType)); |
| 250 checkType('mapUsedWithCascade', K(aKeyType), V(commonMasks.numType)); | 248 checkType('mapUsedWithCascade', K(aKeyType), V(commonMasks.numType)); |
| 251 checkType('mapUsedInClosure', K(aKeyType), V(commonMasks.numType)); | 249 checkType('mapUsedInClosure', K(aKeyType), V(commonMasks.numType)); |
| 252 checkType('mapPassedToSelector', K(aKeyType), V(commonMasks.numType)); | 250 checkType('mapPassedToSelector', K(aKeyType), V(commonMasks.numType)); |
| 253 checkType('mapReturnedFromSelector', K(aKeyType), V(commonMasks.numType)); | 251 checkType( |
| 254 checkType('mapUsedWithConstraint', K(aKeyType), V(commonMasks.uint31Type)); | 252 'mapReturnedFromSelector', K(aKeyType), V(commonMasks.numType)); |
| 255 checkType('mapEscapingFromSetter', K(aKeyType), V(commonMasks.numType)); | 253 checkType( |
| 256 checkType('mapUsedInLocal', K(aKeyType), V(commonMasks.numType)); | 254 'mapUsedWithConstraint', K(aKeyType), V(commonMasks.uint31Type)); |
| 257 checkType('mapEscapingInSetterValue', K(aKeyType), V(commonMasks.numType)); | 255 checkType('mapEscapingFromSetter', K(aKeyType), V(commonMasks.numType)); |
| 258 checkType('mapEscapingInIndex', K(aKeyType), V(commonMasks.numType)); | 256 checkType('mapUsedInLocal', K(aKeyType), V(commonMasks.numType)); |
| 259 checkType('mapEscapingInIndexSet', K(aKeyType), V(commonMasks.uint31Type)); | 257 checkType( |
| 260 checkType('mapEscapingTwiceInIndexSet', | 258 'mapEscapingInSetterValue', K(aKeyType), V(commonMasks.numType)); |
| 261 K(aKeyType), V(commonMasks.numType)); | 259 checkType('mapEscapingInIndex', K(aKeyType), V(commonMasks.numType)); |
| 262 checkType('mapSetInNonFinalField', K(aKeyType), V(commonMasks.numType)); | 260 checkType( |
| 261 'mapEscapingInIndexSet', K(aKeyType), V(commonMasks.uint31Type)); |
| 262 checkType( |
| 263 'mapEscapingTwiceInIndexSet', K(aKeyType), V(commonMasks.numType)); |
| 264 checkType('mapSetInNonFinalField', K(aKeyType), V(commonMasks.numType)); |
| 263 | 265 |
| 264 checkType('mapPassedToClosure', K(commonMasks.dynamicType), | 266 checkType('mapPassedToClosure', K(commonMasks.dynamicType), |
| 265 V(commonMasks.dynamicType)); | 267 V(commonMasks.dynamicType)); |
| 266 checkType('mapReturnedFromClosure', K(commonMasks.dynamicType), | 268 checkType('mapReturnedFromClosure', K(commonMasks.dynamicType), |
| 267 V(commonMasks.dynamicType)); | 269 V(commonMasks.dynamicType)); |
| 268 checkType('mapUsedWithNonOkSelector', K(commonMasks.dynamicType), | 270 checkType('mapUsedWithNonOkSelector', K(commonMasks.dynamicType), |
| 269 V(commonMasks.dynamicType)); | 271 V(commonMasks.dynamicType)); |
| 270 checkType('mapPassedAsOptionalParameter', K(aKeyType), | 272 checkType('mapPassedAsOptionalParameter', K(aKeyType), |
| 271 V(commonMasks.numType)); | 273 V(commonMasks.numType)); |
| 272 checkType('mapPassedAsNamedParameter', K(aKeyType), | 274 checkType( |
| 273 V(commonMasks.numType)); | 275 'mapPassedAsNamedParameter', K(aKeyType), V(commonMasks.numType)); |
| 274 checkType('mapStoredInList', K(aKeyType), | 276 checkType('mapStoredInList', K(aKeyType), V(commonMasks.uint31Type)); |
| 275 V(commonMasks.uint31Type)); | 277 checkType('mapStoredInListButEscapes', K(commonMasks.dynamicType), |
| 276 checkType('mapStoredInListButEscapes', K(commonMasks.dynamicType), | 278 V(commonMasks.dynamicType)); |
| 277 V(commonMasks.dynamicType)); | 279 checkType('mapStoredInMap', K(aKeyType), V(commonMasks.uint31Type)); |
| 278 checkType('mapStoredInMap', K(aKeyType), V(commonMasks.uint31Type)); | 280 checkType('mapStoredInMapButEscapes', K(commonMasks.dynamicType), |
| 279 checkType('mapStoredInMapButEscapes', K(commonMasks.dynamicType), | 281 V(commonMasks.dynamicType)); |
| 280 V(commonMasks.dynamicType)); | |
| 281 | 282 |
| 282 checkType('mapUnset', K(emptyType), V(emptyType)); | 283 checkType('mapUnset', K(emptyType), V(emptyType)); |
| 283 checkType('mapOnlySetWithConstraint', K(aKeyType), V(emptyType)); | 284 checkType('mapOnlySetWithConstraint', K(aKeyType), V(emptyType)); |
| 284 })); | 285 })); |
| 285 } | 286 } |
| OLD | NEW |