Skip to content

Commit

Permalink
Elements. Migrate AssignmentVerifier.
Browse files Browse the repository at this point in the history
Change-Id: I45a3c4df63250c611f011d61023ed74485145b78
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395780
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 18, 2024
1 parent b80fdcb commit 1293500
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class EditableArgumentsHandler

/// Returns the name of an enum constant prefixed with the enum name.
String? _qualifiedEnumConstant(FieldElement2 enumConstant) {
var enumName = enumConstant.enclosingElement2?.name3;
var enumName = enumConstant.enclosingElement2.name3;
var name = enumConstant.name3;
return enumName != null && name != null ? '$enumName.$name' : null;
}
Expand Down
1 change: 0 additions & 1 deletion pkg/analyzer/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ lib/src/dart/resolver/typed_literal_resolver.dart
lib/src/dart/resolver/variable_declaration_resolver.dart
lib/src/dart/resolver/yield_statement_resolver.dart
lib/src/diagnostic/diagnostic_factory.dart
lib/src/error/assignment_verifier.dart
lib/src/error/best_practices_verifier.dart
lib/src/error/correct_override.dart
lib/src/error/dead_code_verifier.dart
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/lib/dart/element/element2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,9 @@ abstract class FieldElement2 implements PropertyInducingElement2 {
@override
FieldElement2 get baseElement;

@override
InstanceElement2 get enclosingElement2;

@override
FieldFragment get firstFragment;

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4189,7 +4189,7 @@ class FieldElementImpl2 extends PropertyInducingElementImpl2
FieldElement2 get baseElement => this;

@override
Element2? get enclosingElement2 =>
InstanceElement2 get enclosingElement2 =>
(firstFragment._enclosingElement3 as InstanceFragment).element;

@override
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/element/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class FieldMember extends VariableMember
String get displayName => declaration.displayName;

@override
Element2? get enclosingElement2 => _element2.enclosingElement2;
InstanceElement2 get enclosingElement2 => _element2.enclosingElement2;

@override
Element get enclosingElement3 => declaration.enclosingElement3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/syntactic_entity.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_provider.dart';
import 'package:analyzer/error/listener.dart';
Expand Down Expand Up @@ -466,8 +467,14 @@ enum ExtensionResolutionError implements ExtensionResolutionResult {
@override
ExecutableElement? get getter => null;

@override
ExecutableElement2? get getter2 => null;

@override
ExecutableElement? get setter => null;

@override
ExecutableElement2? get setter2 => null;
}

/// The result of attempting to resolve an identifier to elements, where the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ class PropertyElementResolver with ScopeHelpers {

AssignmentVerifier(errorReporter).verify(
node: node,
requested: writeElementRequested,
recovery: writeElementRecovery,
requested: writeElementRequested.asElement2,
recovery: writeElementRecovery.asElement2,
receiverType: null,
);
}
Expand Down Expand Up @@ -528,7 +528,7 @@ class PropertyElementResolver with ScopeHelpers {
AssignmentVerifier(errorReporter).verify(
node: propertyName,
requested: null,
recovery: result.getter,
recovery: result.getter2,
receiverType: targetType,
);
}
Expand Down Expand Up @@ -736,7 +736,7 @@ class PropertyElementResolver with ScopeHelpers {
AssignmentVerifier(errorReporter).verify(
node: propertyName,
requested: null,
recovery: writeElementRecovery,
recovery: writeElementRecovery.asElement2,
receiverType: typeReference.thisType,
);
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/lib/src/dart/resolver/resolution_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

/// The result of attempting to resolve an identifier to elements.
class ResolutionResult extends SimpleResolutionResult {
Expand Down Expand Up @@ -57,4 +59,8 @@ class SimpleResolutionResult {
final ExecutableElement? setter;

const SimpleResolutionResult({this.getter, this.setter});

ExecutableElement2? get getter2 => getter.asElement2;

ExecutableElement2? get setter2 => setter.asElement2;
}
56 changes: 34 additions & 22 deletions pkg/analyzer/lib/src/error/assignment_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

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/error/listener.dart';
import 'package:analyzer/src/dart/element/element.dart';
Expand All @@ -29,12 +29,12 @@ class AssignmentVerifier {
/// [CompileTimeErrorCode.UNDEFINED_IDENTIFIER].
void verify({
required SimpleIdentifier node,
required Element? requested,
required Element? recovery,
required Element2? requested,
required Element2? recovery,
required DartType? receiverType,
}) {
if (requested != null) {
if (requested is VariableElement) {
if (requested is VariableElement2) {
if (requested.isConst) {
_errorReporter.atNode(
node,
Expand All @@ -45,54 +45,66 @@ class AssignmentVerifier {
return;
}

if (recovery is DynamicElementImpl ||
recovery is InterfaceElement ||
recovery is TypeAliasElement ||
recovery is TypeParameterElement) {
if (recovery is DynamicElementImpl2 ||
recovery is InterfaceElement2 ||
recovery is TypeAliasElement2 ||
recovery is TypeParameterElement2) {
_errorReporter.atNode(
node,
CompileTimeErrorCode.ASSIGNMENT_TO_TYPE,
);
} else if (recovery is FunctionElement) {
} else if (recovery is LocalFunctionElement ||
recovery is TopLevelFunctionElement) {
_errorReporter.atNode(
node,
CompileTimeErrorCode.ASSIGNMENT_TO_FUNCTION,
);
} else if (recovery is MethodElement) {
} else if (recovery is MethodElement2) {
_errorReporter.atNode(
node,
CompileTimeErrorCode.ASSIGNMENT_TO_METHOD,
);
} else if (recovery is PrefixElement) {
_errorReporter.atNode(
node,
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
arguments: [recovery.name],
);
} else if (recovery is PropertyAccessorElement && recovery.isGetter) {
var variable = recovery.variable2;
} else if (recovery is PrefixElement2) {
if (recovery.name3 case var prefixName?) {
_errorReporter.atNode(
node,
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
arguments: [prefixName],
);
}
} else if (recovery is GetterElement) {
var variable = recovery.variable3;
if (variable == null) {
return;
}

var variableName = variable.name3;
if (variableName == null) {
return;
}

if (variable.isConst) {
_errorReporter.atNode(
node,
CompileTimeErrorCode.ASSIGNMENT_TO_CONST,
);
} else if (variable is FieldElement && variable.isSynthetic) {
} else if (variable is FieldElement2 && variable.isSynthetic) {
_errorReporter.atNode(
node,
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL_NO_SETTER,
arguments: [variable.name, variable.enclosingElement3.displayName],
arguments: [
variableName,
variable.enclosingElement2.displayName,
],
);
} else {
_errorReporter.atNode(
node,
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL,
arguments: [variable.name],
arguments: [variableName],
);
}
} else if (recovery is MultiplyDefinedElementImpl) {
} else if (recovery is MultiplyDefinedElementImpl2) {
// Will be reported in ErrorVerifier.
} else {
if (node.isSynthetic) {
Expand Down

0 comments on commit 1293500

Please sign in to comment.