Skip to content

Commit

Permalink
Elements. Migrate many ID tests.
Browse files Browse the repository at this point in the history
Change-Id: Ib29a26f5cce9c7c358aa4fab02373cf9128f6774
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396700
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 20, 2024
1 parent c42b74d commit cdd2700
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 88 deletions.
12 changes: 0 additions & 12 deletions pkg/analyzer/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,7 @@ test/generated/simple_resolver_test.dart
test/generated/static_type_analyzer_test.dart
test/generated/type_system_test.dart
test/id_tests/assigned_variables_test.dart
test/id_tests/constant_test.dart
test/id_tests/definite_assignment_test.dart
test/id_tests/definite_unassignment_test.dart
test/id_tests/exhaustiveness_test.dart
test/id_tests/inferred_type_arguments_test.dart
test/id_tests/inferred_variable_types_test.dart
test/id_tests/metadata_evaluate_test.dart
test/id_tests/metadata_test.dart
test/id_tests/nullability_test.dart
test/id_tests/reachability_test.dart
test/id_tests/type_constraint_generation_test.dart
test/id_tests/type_promotion_test.dart
test/id_tests/why_not_promoted_test.dart
test/src/dart/analysis/driver_caching_test.dart
test/src/dart/analysis/driver_test.dart
test/src/dart/analysis/index_test.dart
Expand Down
11 changes: 11 additions & 0 deletions pkg/analyzer/lib/dart/constant/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
library;

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:meta/meta.dart';

/// A representation of the value of a compile-time constant expression.
///
Expand Down Expand Up @@ -102,6 +104,15 @@ abstract class DartObject {
/// * the value of the object being represented is `null`.
ExecutableElement? toFunctionValue();

/// Return an element corresponding to the value of the object being
/// represented, or `null`
/// if
/// * this object is not of a function type,
/// * the value of the object being represented is not known, or
/// * the value of the object being represented is `null`.
@experimental
ExecutableElement2? toFunctionValue2();

/// Return an integer corresponding to the value of the object being
/// represented, or `null` if
/// * this object is not of type 'int',
Expand Down
7 changes: 7 additions & 0 deletions pkg/analyzer/lib/src/dart/constant/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/error/error.dart';
Expand All @@ -23,6 +24,7 @@ import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/extensions.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:meta/meta.dart';

/// The state of an object representing a boolean value.
Expand Down Expand Up @@ -897,6 +899,11 @@ class DartObjectImpl implements DartObject, Constant {
return state is FunctionState ? state.element : null;
}

@override
ExecutableElement2? toFunctionValue2() {
return toFunctionValue().asElement2;
}

@override
int? toIntValue() {
var state = this.state;
Expand Down
16 changes: 8 additions & 8 deletions pkg/analyzer/test/id_tests/constant_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/dart/analysis/experiments.dart';
Expand Down Expand Up @@ -58,8 +58,8 @@ class ConstantsDataComputer extends DataComputer<String> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<String>> actualMap) {
ConstantsDataExtractor(unit.declaredElement!.source.uri, actualMap)
.run(unit);
var unitUri = unit.declaredFragment!.source.uri;
ConstantsDataExtractor(unitUri, actualMap).run(unit);
}
}

Expand All @@ -69,9 +69,9 @@ class ConstantsDataExtractor extends AstDataExtractor<String> {
@override
String? computeNodeValue(Id id, AstNode node) {
if (node is Identifier) {
var element = node.staticElement;
if (element is PropertyAccessorElement && element.isSynthetic) {
var variable = element.variable2!;
var element = node.element;
if (element is PropertyAccessorElement2 && element.isSynthetic) {
var variable = element.variable3!;
if (!variable.isSynthetic && variable.isConst) {
var value = variable.computeConstantValue();
if (value != null) return _stringify(value);
Expand Down Expand Up @@ -116,8 +116,8 @@ class ConstantsDataExtractor extends AstDataExtractor<String> {
}
// TODO(paulberry): Support object constants.
} else if (type is FunctionType) {
var element = value.toFunctionValue()!;
return 'Function(${element.name},type=${_stringifyType(value.type!)})';
var element = value.toFunctionValue2()!;
return 'Function(${element.name3},type=${_stringifyType(value.type!)})';
}
throw UnimplementedError('_stringify for type $type');
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/analyzer/test/id_tests/definite_assignment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:io';
import 'package:_fe_analyzer_shared/src/testing/id.dart' show ActualData, Id;
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/dart/analysis/testing_data.dart';
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
Expand Down Expand Up @@ -50,11 +50,9 @@ class _DefiniteAssignmentDataComputer extends DataComputer<String> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<String>> actualMap) {
var unitElement = unit.declaredElement!;
var flowResult = testingData.uriToFlowAnalysisData[unitElement.source.uri]!;
_DefiniteAssignmentDataExtractor(
unitElement.source.uri, actualMap, flowResult)
.run(unit);
var unitUri = unit.declaredFragment!.source.uri;
var flowResult = testingData.uriToFlowAnalysisData[unitUri]!;
_DefiniteAssignmentDataExtractor(unitUri, actualMap, flowResult).run(unit);
}
}

Expand All @@ -67,8 +65,9 @@ class _DefiniteAssignmentDataExtractor extends AstDataExtractor<String> {
@override
String? computeNodeValue(Id id, AstNode node) {
if (node is SimpleIdentifier && node.inGetterContext()) {
var element = node.staticElement;
if (element is LocalVariableElement || element is ParameterElement) {
var element = node.element;
if (element is LocalVariableElement2 ||
element is FormalParameterElement) {
if (_flowResult.notDefinitelyAssigned.contains(node)) {
return 'unassigned';
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/analyzer/test/id_tests/definite_unassignment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:io';
import 'package:_fe_analyzer_shared/src/testing/id.dart' show ActualData, Id;
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/dart/analysis/testing_data.dart';
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
Expand Down Expand Up @@ -50,10 +50,9 @@ class _DefiniteAssignmentDataComputer extends DataComputer<String> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<String>> actualMap) {
var unitElement = unit.declaredElement!;
var flowResult = testingData.uriToFlowAnalysisData[unitElement.source.uri]!;
_DefiniteUnassignmentDataExtractor(
unitElement.source.uri, actualMap, flowResult)
var unitUri = unit.declaredFragment!.source.uri;
var flowResult = testingData.uriToFlowAnalysisData[unitUri]!;
_DefiniteUnassignmentDataExtractor(unitUri, actualMap, flowResult)
.run(unit);
}
}
Expand All @@ -67,8 +66,9 @@ class _DefiniteUnassignmentDataExtractor extends AstDataExtractor<String> {
@override
String? computeNodeValue(Id id, AstNode node) {
if (node is SimpleIdentifier && node.inGetterContext()) {
var element = node.staticElement;
if (element is LocalVariableElement || element is ParameterElement) {
var element = node.element;
if (element is LocalVariableElement2 ||
element is FormalParameterElement) {
if (_flowResult.definitelyUnassigned.contains(node)) {
return 'unassigned';
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/analyzer/test/id_tests/exhaustiveness_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ class _ExhaustivenessDataComputer extends DataComputer<Features> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<Features>> actualMap) {
var unitElement = unit.declaredElement!;
var exhaustivenessData =
testingData.uriToExhaustivenessData[unitElement.source.uri]!;
_ExhaustivenessDataExtractor(
unitElement.source.uri, actualMap, exhaustivenessData)
var unitUri = unit.declaredFragment!.source.uri;
var exhaustivenessData = testingData.uriToExhaustivenessData[unitUri]!;
_ExhaustivenessDataExtractor(unitUri, actualMap, exhaustivenessData)
.run(unit);
}
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/analyzer/test/id_tests/inferred_type_arguments_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class _InferredTypeArgumentsDataComputer extends DataComputer<List<DartType>> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<List<DartType>>> actualMap) {
_InferredTypeArgumentsDataExtractor(
unit.declaredElement!.source.uri, actualMap)
.run(unit);
var unitUri = unit.declaredFragment!.source.uri;
_InferredTypeArgumentsDataExtractor(unitUri, actualMap).run(unit);
}
}

Expand Down
13 changes: 6 additions & 7 deletions pkg/analyzer/test/id_tests/inferred_variable_types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class _InferredVariableTypesDataComputer extends DataComputer<DartType> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<DartType>> actualMap) {
_InferredVariableTypesDataExtractor(
unit.declaredElement!.source.uri, actualMap)
.run(unit);
var unitUri = unit.declaredFragment!.source.uri;
_InferredVariableTypesDataExtractor(unitUri, actualMap).run(unit);
}
}

Expand All @@ -50,23 +49,23 @@ class _InferredVariableTypesDataExtractor extends AstDataExtractor<DartType> {
@override
DartType? computeNodeValue(Id id, AstNode node) {
if (node is VariableDeclaration) {
var element = node.declaredElement!;
var element = node.declaredFragment!.element;
if (element.hasImplicitType) {
return element.type;
}
} else if (node is FormalParameter) {
var element = node.declaredElement!;
var element = node.declaredFragment!.element;
if (element.hasImplicitType) {
return element.type;
}
} else if (node is FunctionDeclarationStatement) {
var element = node.functionDeclaration.declaredElement!;
var element = node.functionDeclaration.declaredFragment!.element;
if (element.hasImplicitReturnType) {
return element.returnType;
}
} else if (node is FunctionExpression &&
node.parent is! FunctionDeclaration) {
var element = node.declaredElement!;
var element = node.declaredFragment!.element;
if (element.hasImplicitReturnType) {
return element.returnType;
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/analyzer/test/id_tests/metadata_evaluate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:_fe_analyzer_shared/src/testing/id.dart';
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
import 'package:_fe_analyzer_shared/src/testing/metadata_helper.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/analysis/testing_data.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
Expand Down Expand Up @@ -42,8 +42,8 @@ class _MetadataDataComputer extends DataComputer<String> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<String>> actualMap) {
_MetadataDataExtractor(unit.declaredElement!.source.uri, actualMap)
.run(unit);
var unitUri = unit.declaredFragment!.source.uri;
_MetadataDataExtractor(unitUri, actualMap).run(unit);
}
}

Expand All @@ -55,10 +55,10 @@ class _MetadataDataExtractor extends AstDataExtractor<String> {
@override
String? computeNodeValue(Id id, AstNode node) {
if (node is Declaration) {
Element? element = node.declaredElement;
if (element != null) {
var element = node.declaredFragment?.element;
if (element case Annotatable annotatable) {
List<String> list = [];
for (ElementAnnotation annotation in element.metadata) {
for (var annotation in annotatable.metadata2.annotations) {
if (annotation is ElementAnnotationImpl) {
var resolved = parseAnnotation(annotation);
list.addAll(evaluationToText(resolved,
Expand Down
12 changes: 6 additions & 6 deletions pkg/analyzer/test/id_tests/metadata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:_fe_analyzer_shared/src/testing/id.dart';
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
import 'package:_fe_analyzer_shared/src/testing/metadata_helper.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/analysis/testing_data.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
Expand Down Expand Up @@ -42,8 +42,8 @@ class _MetadataDataComputer extends DataComputer<String> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<String>> actualMap) {
_MetadataDataExtractor(unit.declaredElement!.source.uri, actualMap)
.run(unit);
var unitUri = unit.declaredFragment!.source.uri;
_MetadataDataExtractor(unitUri, actualMap).run(unit);
}
}

Expand All @@ -55,10 +55,10 @@ class _MetadataDataExtractor extends AstDataExtractor<String> {
@override
String? computeNodeValue(Id id, AstNode node) {
if (node is Declaration) {
Element? element = node.declaredElement;
if (element != null) {
var element = node.declaredFragment?.element;
if (element case Annotatable annotatable) {
List<String> list = [];
for (ElementAnnotation annotation in element.metadata) {
for (var annotation in annotatable.metadata2.annotations) {
if (annotation is ElementAnnotationImpl) {
var unresolved =
parseAnnotation(annotation, delayLookupForTesting: true);
Expand Down
17 changes: 10 additions & 7 deletions pkg/analyzer/test/id_tests/nullability_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:io';
import 'package:_fe_analyzer_shared/src/testing/id.dart' show ActualData, Id;
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_system.dart';
import 'package:analyzer/src/dart/analysis/testing_data.dart';
Expand Down Expand Up @@ -37,10 +37,12 @@ class _NullabilityDataComputer extends DataComputer<String> {
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<String>> actualMap) {
var unitElement = unit.declaredElement!;
var unitElement = unit.declaredFragment!;
_NullabilityDataExtractor(
unitElement.source.uri, actualMap, unitElement.library.typeSystem)
.run(unit);
unitElement.source.uri,
actualMap,
unitElement.element.typeSystem,
).run(unit);
}
}

Expand All @@ -54,10 +56,11 @@ class _NullabilityDataExtractor extends AstDataExtractor<String> {
if (node is SimpleIdentifier &&
node.inGetterContext() &&
!node.inDeclarationContext()) {
var element = node.staticElement;
if (element is LocalVariableElement || element is ParameterElement) {
var element = node.element;
if (element is LocalVariableElement2 ||
element is FormalParameterElement) {
var promotedType = _readType(node);
var declaredType = (element as VariableElement).type;
var declaredType = (element as VariableElement2).type;
var isPromoted = promotedType != declaredType;
if (isPromoted &&
_typeSystem.isPotentiallyNullable(declaredType) &&
Expand Down
7 changes: 3 additions & 4 deletions pkg/analyzer/test/id_tests/reachability_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ class _ReachabilityDataComputer
@override
void computeUnitData(TestingData testingData, CompilationUnit unit,
Map<Id, ActualData<Set<_ReachabilityAssertion>>> actualMap) {
var unitElement = unit.declaredElement!;
var flowResult = testingData.uriToFlowAnalysisData[unitElement.source.uri]!;
_ReachabilityDataExtractor(unitElement.source.uri, actualMap, flowResult)
.run(unit);
var unitUri = unit.declaredFragment!.source.uri;
var flowResult = testingData.uriToFlowAnalysisData[unitUri]!;
_ReachabilityDataExtractor(unitUri, actualMap, flowResult).run(unit);
}
}

Expand Down
Loading

0 comments on commit cdd2700

Please sign in to comment.