Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Side by Side Diff: pkg/analyzer/test/src/task/dart_test.dart

Issue 1145913002: In constant evaluation, handle annotation referring to non-const constructor. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/generated/constant.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 library test.src.task.dart_test; 5 library test.src.task.dart_test;
6 6
7 import 'package:analyzer/src/context/cache.dart'; 7 import 'package:analyzer/src/context/cache.dart';
8 import 'package:analyzer/src/generated/ast.dart'; 8 import 'package:analyzer/src/generated/ast.dart';
9 import 'package:analyzer/src/generated/constant.dart'; 9 import 'package:analyzer/src/generated/constant.dart';
10 import 'package:analyzer/src/generated/element.dart'; 10 import 'package:analyzer/src/generated/element.dart';
(...skipping 1294 matching lines...) Expand 10 before | Expand all | Expand 10 after
1305 expect(outputs[CONSTANT_VALUE], same(target)); 1305 expect(outputs[CONSTANT_VALUE], same(target));
1306 EvaluationResultImpl evaluationResult = 1306 EvaluationResultImpl evaluationResult =
1307 (annotation.elementAnnotation as ElementAnnotationImpl).evaluationRe sult; 1307 (annotation.elementAnnotation as ElementAnnotationImpl).evaluationRe sult;
1308 return evaluationResult; 1308 return evaluationResult;
1309 } 1309 }
1310 } 1310 }
1311 fail('Annotation not found'); 1311 fail('Annotation not found');
1312 return null; 1312 return null;
1313 } 1313 }
1314 1314
1315 test_annotation_non_const_constructor() {
1316 // Calling a non-const constructor from an annotation that is illegal, but
1317 // shouldn't crash analysis.
1318 Source source = newSource('/test.dart', '''
1319 class A {
1320 final int i;
1321 A(this.i);
1322 }
1323
1324 @A(5)
1325 class C {}
1326 ''');
1327 // First compute the resolved unit for the source.
1328 CompilationUnit unit = _resolveSource(source);
1329 // Compute the constant value of the annotation on C.
1330 EvaluationResultImpl evaluationResult =
1331 computeClassAnnotation(source, unit, 'C');
1332 // And check that it has no value stored in it.
1333 expect(evaluationResult, isNotNull);
1334 expect(evaluationResult.value, isNull);
1335 }
1336
1315 test_annotation_with_args() { 1337 test_annotation_with_args() {
1316 Source source = newSource('/test.dart', ''' 1338 Source source = newSource('/test.dart', '''
1317 const x = 1; 1339 const x = 1;
1318 @D(x) class C {} 1340 @D(x) class C {}
1319 class D { 1341 class D {
1320 const D(this.value); 1342 const D(this.value);
1321 final value; 1343 final value;
1322 } 1344 }
1323 '''); 1345 ''');
1324 // First compute the resolved unit for the source. 1346 // First compute the resolved unit for the source.
1325 LibrarySpecificUnit librarySpecificUnit = 1347 CompilationUnit unit = _resolveSource(source);
1326 new LibrarySpecificUnit(source, source);
1327 _computeResult(librarySpecificUnit, RESOLVED_UNIT1);
1328 CompilationUnit unit = outputs[RESOLVED_UNIT1];
1329 // Compute the constant value of the annotation on C. 1348 // Compute the constant value of the annotation on C.
1330 EvaluationResultImpl evaluationResult = 1349 EvaluationResultImpl evaluationResult =
1331 computeClassAnnotation(source, unit, 'C'); 1350 computeClassAnnotation(source, unit, 'C');
1332 // And check that it has the expected value. 1351 // And check that it has the expected value.
1333 expect(evaluationResult, isNotNull); 1352 expect(evaluationResult, isNotNull);
1334 expect(evaluationResult.value, isNotNull); 1353 expect(evaluationResult.value, isNotNull);
1335 expect(evaluationResult.value.type, isNotNull); 1354 expect(evaluationResult.value.type, isNotNull);
1336 expect(evaluationResult.value.type.name, 'D'); 1355 expect(evaluationResult.value.type.name, 'D');
1337 expect(evaluationResult.value.fields, contains('value')); 1356 expect(evaluationResult.value.fields, contains('value'));
1338 expect(evaluationResult.value.fields['value'].intValue, 1); 1357 expect(evaluationResult.value.fields['value'].intValue, 1);
1339 } 1358 }
1340 1359
1341 test_annotation_without_args() { 1360 test_annotation_without_args() {
1342 Source source = newSource('/test.dart', ''' 1361 Source source = newSource('/test.dart', '''
1343 const x = 1; 1362 const x = 1;
1344 @x class C {} 1363 @x class C {}
1345 '''); 1364 ''');
1346 // First compute the resolved unit for the source. 1365 // First compute the resolved unit for the source.
1347 LibrarySpecificUnit librarySpecificUnit = 1366 CompilationUnit unit = _resolveSource(source);
1348 new LibrarySpecificUnit(source, source);
1349 _computeResult(librarySpecificUnit, RESOLVED_UNIT1);
1350 CompilationUnit unit = outputs[RESOLVED_UNIT1];
1351 // Compute the constant value of the annotation on C. 1367 // Compute the constant value of the annotation on C.
1352 EvaluationResultImpl evaluationResult = 1368 EvaluationResultImpl evaluationResult =
1353 computeClassAnnotation(source, unit, 'C'); 1369 computeClassAnnotation(source, unit, 'C');
1354 // And check that it has the expected value. 1370 // And check that it has the expected value.
1355 expect(evaluationResult, isNotNull); 1371 expect(evaluationResult, isNotNull);
1356 expect(evaluationResult.value, isNotNull); 1372 expect(evaluationResult.value, isNotNull);
1357 expect(evaluationResult.value.intValue, 1); 1373 expect(evaluationResult.value.intValue, 1);
1358 } 1374 }
1359 1375
1360 test_circular_reference() { 1376 test_circular_reference() {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1455 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT); 1471 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT);
1456 } 1472 }
1457 1473
1458 PropertyInducingElement _findVariable( 1474 PropertyInducingElement _findVariable(
1459 CompilationUnit unit, String variableName) { 1475 CompilationUnit unit, String variableName) {
1460 // Find the element for the given constant. 1476 // Find the element for the given constant.
1461 return unit.element.topLevelVariables.firstWhere( 1477 return unit.element.topLevelVariables.firstWhere(
1462 (TopLevelVariableElement variable) => variable.name == variableName); 1478 (TopLevelVariableElement variable) => variable.name == variableName);
1463 } 1479 }
1464 1480
1465 CompilationUnit _resolveUnit(String content) { 1481 CompilationUnit _resolveSource(Source source) {
1466 Source source = newSource('/test.dart', content);
1467 // First compute the resolved unit for the source.
1468 LibrarySpecificUnit librarySpecificUnit = 1482 LibrarySpecificUnit librarySpecificUnit =
1469 new LibrarySpecificUnit(source, source); 1483 new LibrarySpecificUnit(source, source);
1470 _computeResult(librarySpecificUnit, RESOLVED_UNIT1); 1484 _computeResult(librarySpecificUnit, RESOLVED_UNIT1);
1471 CompilationUnit unit = outputs[RESOLVED_UNIT1]; 1485 CompilationUnit unit = outputs[RESOLVED_UNIT1];
1472 return unit; 1486 return unit;
1473 } 1487 }
1488
1489 CompilationUnit _resolveUnit(String content) =>
1490 _resolveSource(newSource('/test.dart', content));
1474 } 1491 }
1475 1492
1476 @reflectiveTest 1493 @reflectiveTest
1477 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest { 1494 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest {
1478 test_buildInputs() { 1495 test_buildInputs() {
1479 Map<String, TaskInput> inputs = 1496 Map<String, TaskInput> inputs =
1480 ContainingLibrariesTask.buildInputs(emptySource); 1497 ContainingLibrariesTask.buildInputs(emptySource);
1481 expect(inputs, isNotNull); 1498 expect(inputs, isNotNull);
1482 expect(inputs, isEmpty); 1499 expect(inputs, isEmpty);
1483 } 1500 }
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after
2457 /** 2474 /**
2458 * Fill [errorListener] with [result] errors in the current [task]. 2475 * Fill [errorListener] with [result] errors in the current [task].
2459 */ 2476 */
2460 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) { 2477 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) {
2461 List<AnalysisError> errors = task.outputs[result]; 2478 List<AnalysisError> errors = task.outputs[result];
2462 expect(errors, isNotNull, reason: result.name); 2479 expect(errors, isNotNull, reason: result.name);
2463 errorListener = new GatheringErrorListener(); 2480 errorListener = new GatheringErrorListener();
2464 errorListener.addAll(errors); 2481 errorListener.addAll(errors);
2465 } 2482 }
2466 } 2483 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/constant.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698