Skip to content

Commit

Permalink
Augment. Use nullable PropertyAccessorElement.variable2
Browse files Browse the repository at this point in the history
As much as I don't like the scale of changes, there is no valid variable in these cases. So, we express this explicitly, without trying to pretend that there is on. Or crashing as we did without this CL.

Change-Id: I74cef1d3d9d3cba6985d83b98be361cca09170f9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/355300
Reviewed-by: Phil Quitslund <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Mar 4, 2024
1 parent 690d49c commit ebb5c7d
Show file tree
Hide file tree
Showing 69 changed files with 374 additions and 182 deletions.
5 changes: 4 additions & 1 deletion pkg/analysis_server/lib/src/cider/rename.dart
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,10 @@ class CiderRenameComputer {
return null;
}
if (element is PropertyAccessorElement) {
element = element.variable;
element = element.variable2;
if (element == null) {
return null;
}
}
if (!_canRenameElement(element)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class DartUnitHighlightsComputer {
}
if (element is PropertyAccessorElement) {
var accessor = element;
var variable = accessor.variable;
var variable = accessor.variable2;
if (variable is TopLevelVariableElement) {
type = accessor.isGetter
? HighlightRegionType.TOP_LEVEL_GETTER_REFERENCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor<void> {
if (canonicalElement is FieldFormalParameterElement) {
canonicalElement = canonicalElement.field;
} else if (canonicalElement is PropertyAccessorElement) {
canonicalElement = canonicalElement.variable;
canonicalElement = canonicalElement.variable2;
}
return canonicalElement?.declaration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SearchFindElementReferencesHandler extends LegacyHandler {
element = element.field;
}
if (element is PropertyAccessorElement) {
element = element.variable;
element = element.variable2;
}
// respond
var searchId = (server.nextSearchId++).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ class DefinitionHandler extends LspMessageHandler<TextDocumentPositionParams,

/// Get the location of the code (excluding leading doc comments) for this element.
Future<protocol.Location?> _getCodeLocation(Element element) async {
var codeElement = element;
Element? codeElement = element;
// For synthetic getters created for fields, we need to access the associated
// variable to get the codeOffset/codeLength.
if (codeElement.isSynthetic && codeElement is PropertyAccessorElementImpl) {
codeElement = codeElement.variable;
if (codeElement is PropertyAccessorElementImpl && codeElement.isSynthetic) {
codeElement = codeElement.variable2!;
}

// Read the main codeOffset from the element. This may include doc comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class DocumentColorPresentationHandler extends SharedMessageHandler<
? parent.staticElement
: node.staticElement;
final target = staticElement is PropertyAccessorElement
? staticElement.variable
? staticElement.variable2
: staticElement;

final existingIsConst = target is ConstVariableElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class TypeDefinitionHandler extends SharedMessageHandler<TypeDefinitionParams,
} else if (node.inSetterContext()) {
final writeElement = node.writeElement;
if (writeElement is PropertyAccessorElement) {
return writeElement.variable.type;
return writeElement.variable2?.type;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1656,5 +1656,12 @@ extension on List<ExecutableElement> {

extension on PropertyAccessorElement {
/// Whether this accessor is an accessor for a constant variable.
bool get isConst => isSynthetic && variable.isConst;
bool get isConst {
if (isSynthetic) {
if (variable2 case var variable?) {
return variable.isConst;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ class FeatureComputer {
} else if (element is FieldElement && element.isEnumConstant) {
return protocol.ElementKind.ENUM_CONSTANT;
} else if (element is PropertyAccessorElement) {
element = element.variable;
var variable = element.variable2;
if (variable == null) {
return protocol.ElementKind.UNKNOWN;
}
element = variable;
}
var kind = element.kind;
if (kind == ElementKind.CONSTRUCTOR) {
Expand Down Expand Up @@ -314,11 +318,11 @@ class FeatureComputer {
return 1.0;
} else if (element is TopLevelVariableElement && element.isConst) {
return 1.0;
} else if (element is PropertyAccessorElement &&
element.isSynthetic &&
element.variable.isStatic &&
element.variable.isConst) {
return 1.0;
} else if (element is PropertyAccessorElement && element.isSynthetic) {
var variable = element.variable2;
if (variable != null && variable.isStatic && variable.isConst) {
return 1.0;
}
}
return 0.0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor<void> {

@override
void visitPropertyAccessorElement(PropertyAccessorElement element) {
var variable = element.variable2;
if (opType.includeReturnValueSuggestions ||
(opType.includeAnnotationSuggestions && element.variable.isConst)) {
(opType.includeAnnotationSuggestions &&
variable != null &&
variable.isConst)) {
var parent = element.enclosingElement;
if (parent is InterfaceElement || parent is ExtensionElement) {
builder.suggestAccessor(element, inheritanceDistance: 0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class MemberSuggestionBuilder {
{required PropertyAccessorElement accessor,
required double inheritanceDistance}) {
if (accessor.isAccessibleIn(request.libraryElement)) {
var member = accessor.isSynthetic ? accessor.variable : accessor;
var member =
accessor.isSynthetic ? accessor.variable2 ?? accessor : accessor;
if (_shouldAddSuggestion(member)) {
builder.suggestAccessor(accessor,
inheritanceDistance: inheritanceDistance);
Expand Down Expand Up @@ -269,7 +270,7 @@ class SuggestionBuilder {
// non-final fields induce a setter, so we don't add a suggestion for a
// synthetic setter.
if (accessor.isGetter) {
var variable = accessor.variable;
var variable = accessor.variable2;
if (variable is FieldElement) {
suggestField(variable, inheritanceDistance: inheritanceDistance);
}
Expand Down Expand Up @@ -1145,7 +1146,7 @@ class SuggestionBuilder {
// non-final fields induce a setter, so we don't add a suggestion for a
// synthetic setter.
if (accessor.isGetter) {
var variable = accessor.variable;
var variable = accessor.variable2;
if (variable is TopLevelVariableElement) {
suggestTopLevelVariable(variable);
}
Expand Down
43 changes: 23 additions & 20 deletions pkg/analysis_server/lib/src/services/correction/dart/add_late.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AddLate extends ResolvedCorrectionProducer {
await _insertAt(builder, variableList.variables[0].offset);
// TODO(brianwilkerson): Consider converting this into an assist and
// expand it to support converting `var` to `late` as well as
// working anywhere a non-late local variable or field is selected.
// working anywhere a non-late local variableElement or field is selected.
// } else if (keyword.type == Keyword.VAR) {
// builder.addFileEdit(file, (builder) {
// builder.addSimpleReplacement(range.token(keyword), 'late');
Expand All @@ -56,26 +56,29 @@ class AddLate extends ResolvedCorrectionProducer {
if (getter is PropertyAccessorElement &&
getter.isGetter &&
getter.isSynthetic &&
!getter.variable.isSynthetic &&
!getter.variable.isLate &&
getter.variable.setter == null &&
getter.enclosingElement is InterfaceElement) {
var declarationResult =
await sessionHelper.getElementDeclaration(getter.variable);
if (declarationResult == null) {
return;
}
var variable = declarationResult.node;
var variableList = variable.parent;
if (variable is VariableDeclaration &&
variableList is VariableDeclarationList &&
variableList.parent is FieldDeclaration) {
var keywordToken = variableList.keyword;
if (variableList.variables.length == 1 &&
keywordToken != null &&
keywordToken.keyword == Keyword.FINAL) {
await _insertAt(builder, keywordToken.offset,
source: declarationResult.element.source);
var variableElement = getter.variable2;
if (variableElement != null &&
!variableElement.isSynthetic &&
!variableElement.isLate &&
variableElement.setter == null) {
var declarationResult =
await sessionHelper.getElementDeclaration(variableElement);
if (declarationResult == null) {
return;
}
var variableNode = declarationResult.node;
var variableList = variableNode.parent;
if (variableNode is VariableDeclaration &&
variableList is VariableDeclarationList &&
variableList.parent is FieldDeclaration) {
var keywordToken = variableList.keyword;
if (variableList.variables.length == 1 &&
keywordToken != null &&
keywordToken.keyword == Keyword.FINAL) {
await _insertAt(builder, keywordToken.offset,
source: declarationResult.element.source);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class _FieldFinder extends RecursiveAstVisitor<void> {
if (node.inSetterContext()) {
var element = node.writeOrReadElement;
if (element is PropertyAccessorElement) {
var field = element.variable;
var field = element.variable2;
if (field is FieldElement) {
fieldsAssignedInConstructors.add(field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class _FieldFinder extends RecursiveAstVisitor<void> {
if (node.inSetterContext()) {
var element = node.writeOrReadElement;
if (element is PropertyAccessorElement) {
var field = element.variable;
var field = element.variable2;
if (field is FieldElement) {
fieldsAssignedInConstructors.add(field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ class ImportLibrary extends MultiCorrectionProducer {
continue;
}
if (element is PropertyAccessorElement) {
element = element.variable;
element = element.variable2;
if (element == null) {
continue;
}
}
if (!kinds.contains(element.kind)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class MakeFieldNotFinal extends ResolvedCorrectionProducer {
}

// The variable must be not synthetic, and have no setter yet.
var variable = getter.variable;
var variable = getter.variable2;
if (variable == null) {
return;
}
if (variable.isSynthetic || variable.setter != null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
if (staticElement == element) {
references.add(node);
} else if (staticElement is PropertyAccessorElement) {
if (staticElement.variable == element) {
if (staticElement.variable2 == element) {
references.add(node);
}
} else if (staticElement is FieldFormalParameterElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ class _WidgetDescriptionComputer {
} else if (valueExpression is Identifier) {
var element = valueExpression.staticElement;
if (element is PropertyAccessorElement && element.isGetter) {
var field = element.variable;
var field = element.variable2;
if (field is FieldElement && field.isStatic) {
var enclosingClass = field.enclosingElement as InterfaceElement;
if (field.isEnumConstant ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ConvertGetterToMethodRefactoringImpl extends RefactoringImpl
await _updateElementReferences(element);
}
// method
var field = element.variable;
var field = element.variable2;
if (field is FieldElement &&
(field.enclosingElement is InterfaceElement ||
field.enclosingElement is ExtensionElement)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,10 @@ class _ParametersCollector extends RecursiveAstVisitor<void> {
}
}
} else if (element is PropertyAccessorElement) {
var field = element.variable;
var field = element.variable2;
if (field == null) {
return;
}
if (_isMemberOfEnclosingClass(field)) {
if (node.inSetterContext()) {
status.addError("Write to '$elementName' cannot be extracted.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ abstract class RenameRefactoring implements Refactoring {
var session = resolvedUnit.session;
var sessionHelper = AnalysisSessionHelper(session);
if (element is PropertyAccessorElement) {
element = element.variable;
element = element.variable2;
if (element == null) {
return null;
}
}
var enclosingElement = element.enclosingElement;
if (enclosingElement is CompilationUnitElement) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analysis_server/lib/src/services/search/hierarchy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ List<Element> getMembers(InterfaceElement clazz) {
Element getSyntheticAccessorVariable(Element element) {
if (element is PropertyAccessorElement) {
if (element.isSynthetic) {
return element.variable;
return element.variable2 ?? element;
}
}
return element;
Expand Down
6 changes: 5 additions & 1 deletion pkg/analysis_server/lib/src/utilities/import_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ class _ElementRecorder {
LibraryImportElement? import) {
if (referencedElement is PropertyAccessorElement &&
referencedElement.isSynthetic) {
referencedElement = referencedElement.variable;
var variable = referencedElement.variable2;
if (variable == null) {
return;
}
referencedElement = variable;
}
if (_isBeingMoved(referenceOffset)) {
var imports =
Expand Down
4 changes: 4 additions & 0 deletions pkg/analyzer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Deprecated `InterfaceElement.lookUpGetter`, `InterfaceElement.lookUpMethod`,
and `InterfaceElement.lookUpSetter`.
* Fixed `GeneralizingAstVisitor.visitNamedType` to invoke `visitTypeAnnotation`.
* Deprecated `PropertyInducingElement get variable` in `PropertyAccessorElement`,
use `PropertyInducingElement? get variable2` instead.
The reason for this is that when the property accessor is an augmentation
without the corresponding declaration, there is no corresponding variable.

## 6.4.1
* Patch for crash in ffi_verifier.
Expand Down
10 changes: 10 additions & 0 deletions pkg/analyzer/lib/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2341,7 +2341,17 @@ abstract class PropertyAccessorElement implements ExecutableElement {
///
/// If this accessor was explicitly defined (is not synthetic) then the
/// variable associated with it will be synthetic.
@Deprecated('Use variable2')
PropertyInducingElement get variable;

/// The field or top-level variable associated with this accessor.
///
/// If this accessor was explicitly defined (is not synthetic) then the
/// variable associated with it will be synthetic.
///
/// If this accessor is an augmentation, and [augmentationTarget] is `null`,
/// the variable is `null`.
PropertyInducingElement? get variable2;
}

/// A variable that has an associated getter and possibly a setter. Note that
Expand Down
4 changes: 3 additions & 1 deletion pkg/analyzer/lib/src/dart/analysis/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ class IndexElementInfo {
kind = accessor.isGetter
? IndexSyntheticElementKind.getter
: IndexSyntheticElementKind.setter;
element = accessor.variable;
if (accessor.variable2 case var variable?) {
element = variable;
}
}
} else if (element is MethodElement) {
Element enclosing = element.enclosingElement;
Expand Down
Loading

0 comments on commit ebb5c7d

Please sign in to comment.