diff --git a/pkg/analysis_server/lib/src/cider/rename.dart b/pkg/analysis_server/lib/src/cider/rename.dart index d7816bcb4b97..ac9adb046be7 100644 --- a/pkg/analysis_server/lib/src/cider/rename.dart +++ b/pkg/analysis_server/lib/src/cider/rename.dart @@ -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; diff --git a/pkg/analysis_server/lib/src/computer/computer_highlights.dart b/pkg/analysis_server/lib/src/computer/computer_highlights.dart index b78a467b0cda..58b8a4ea129f 100644 --- a/pkg/analysis_server/lib/src/computer/computer_highlights.dart +++ b/pkg/analysis_server/lib/src/computer/computer_highlights.dart @@ -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 diff --git a/pkg/analysis_server/lib/src/domains/analysis/occurrences_dart.dart b/pkg/analysis_server/lib/src/domains/analysis/occurrences_dart.dart index 09c53537a0c9..4380acb0b2f2 100644 --- a/pkg/analysis_server/lib/src/domains/analysis/occurrences_dart.dart +++ b/pkg/analysis_server/lib/src/domains/analysis/occurrences_dart.dart @@ -199,7 +199,7 @@ class _DartUnitOccurrencesComputerVisitor extends RecursiveAstVisitor { if (canonicalElement is FieldFormalParameterElement) { canonicalElement = canonicalElement.field; } else if (canonicalElement is PropertyAccessorElement) { - canonicalElement = canonicalElement.variable; + canonicalElement = canonicalElement.variable2; } return canonicalElement?.declaration; } diff --git a/pkg/analysis_server/lib/src/handler/legacy/search_find_element_references.dart b/pkg/analysis_server/lib/src/handler/legacy/search_find_element_references.dart index 453f5f177eba..ae66b2c98c44 100644 --- a/pkg/analysis_server/lib/src/handler/legacy/search_find_element_references.dart +++ b/pkg/analysis_server/lib/src/handler/legacy/search_find_element_references.dart @@ -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(); diff --git a/pkg/analysis_server/lib/src/lsp/handlers/handler_definition.dart b/pkg/analysis_server/lib/src/lsp/handlers/handler_definition.dart index c0d52834f2b1..d114a17f6232 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/handler_definition.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/handler_definition.dart @@ -174,11 +174,11 @@ class DefinitionHandler extends LspMessageHandler _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 diff --git a/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart b/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart index 9c99e864899b..b25cb63c7287 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart @@ -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; diff --git a/pkg/analysis_server/lib/src/lsp/handlers/handler_type_definition.dart b/pkg/analysis_server/lib/src/lsp/handlers/handler_type_definition.dart index 099eb6aee7ae..0c43b8c914a9 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/handler_type_definition.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/handler_type_definition.dart @@ -184,7 +184,7 @@ class TypeDefinitionHandler extends SharedMessageHandler { 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; + } } diff --git a/pkg/analysis_server/lib/src/services/completion/dart/feature_computer.dart b/pkg/analysis_server/lib/src/services/completion/dart/feature_computer.dart index 2481447553bc..fe716374e0ef 100644 --- a/pkg/analysis_server/lib/src/services/completion/dart/feature_computer.dart +++ b/pkg/analysis_server/lib/src/services/completion/dart/feature_computer.dart @@ -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) { @@ -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; } diff --git a/pkg/analysis_server/lib/src/services/completion/dart/local_library_contributor.dart b/pkg/analysis_server/lib/src/services/completion/dart/local_library_contributor.dart index 67f8150d88da..a3b70d6f071b 100644 --- a/pkg/analysis_server/lib/src/services/completion/dart/local_library_contributor.dart +++ b/pkg/analysis_server/lib/src/services/completion/dart/local_library_contributor.dart @@ -124,8 +124,11 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor { @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); diff --git a/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart b/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart index e992a49ec832..47918c996f51 100644 --- a/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart +++ b/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart @@ -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); @@ -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); } @@ -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); } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart b/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart index 334f2717ec84..315a3ae995d9 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/add_late.dart @@ -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'); @@ -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); + } } } } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart index 91f766c64102..67caa5cbac36 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart @@ -286,7 +286,7 @@ class _FieldFinder extends RecursiveAstVisitor { 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); } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart index 4e7863d4316c..57eaa84cf90a 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart @@ -286,7 +286,7 @@ class _FieldFinder extends RecursiveAstVisitor { 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); } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart index 6c42d6b0b40d..cfcbea165e09 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart @@ -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; diff --git a/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart b/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart index f3d05b93aa48..41fe01a18c44 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/make_field_not_final.dart @@ -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; } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart index b3f8b17cb7f7..e6fde817064f 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused.dart @@ -278,7 +278,7 @@ class _ElementReferenceCollector extends RecursiveAstVisitor { 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) { diff --git a/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart b/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart index 208e333d4b5e..a708fc3582ef 100644 --- a/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart +++ b/pkg/analysis_server/lib/src/services/flutter/widget_descriptions.dart @@ -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 || diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart index 94442fc239a6..3431857d174d 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/convert_getter_to_method.dart @@ -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)) { diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart index 78a3b3c7bc2f..2a629f00522a 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/extract_widget.dart @@ -629,7 +629,10 @@ class _ParametersCollector extends RecursiveAstVisitor { } } } 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."); diff --git a/pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring.dart b/pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring.dart index c88de5e8b564..f533741899ec 100644 --- a/pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring.dart +++ b/pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring.dart @@ -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) { diff --git a/pkg/analysis_server/lib/src/services/search/hierarchy.dart b/pkg/analysis_server/lib/src/services/search/hierarchy.dart index 186bf071d864..84c0e8121639 100644 --- a/pkg/analysis_server/lib/src/services/search/hierarchy.dart +++ b/pkg/analysis_server/lib/src/services/search/hierarchy.dart @@ -191,7 +191,7 @@ List getMembers(InterfaceElement clazz) { Element getSyntheticAccessorVariable(Element element) { if (element is PropertyAccessorElement) { if (element.isSynthetic) { - return element.variable; + return element.variable2 ?? element; } } return element; diff --git a/pkg/analysis_server/lib/src/utilities/import_analyzer.dart b/pkg/analysis_server/lib/src/utilities/import_analyzer.dart index 84acdc36c052..446139134e19 100644 --- a/pkg/analysis_server/lib/src/utilities/import_analyzer.dart +++ b/pkg/analysis_server/lib/src/utilities/import_analyzer.dart @@ -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 = diff --git a/pkg/analyzer/CHANGELOG.md b/pkg/analyzer/CHANGELOG.md index b3cb0d74810c..e148c576efa6 100644 --- a/pkg/analyzer/CHANGELOG.md +++ b/pkg/analyzer/CHANGELOG.md @@ -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. diff --git a/pkg/analyzer/lib/dart/element/element.dart b/pkg/analyzer/lib/dart/element/element.dart index b3f608d1321e..26515568101b 100644 --- a/pkg/analyzer/lib/dart/element/element.dart +++ b/pkg/analyzer/lib/dart/element/element.dart @@ -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 diff --git a/pkg/analyzer/lib/src/dart/analysis/index.dart b/pkg/analyzer/lib/src/dart/analysis/index.dart index ec173e216d49..3a112ad90f08 100644 --- a/pkg/analyzer/lib/src/dart/analysis/index.dart +++ b/pkg/analyzer/lib/src/dart/analysis/index.dart @@ -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; diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart index 5f332612af36..0a38e5b322e7 100644 --- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart +++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart @@ -166,10 +166,11 @@ class ConstantEvaluationEngine { if (element is PropertyAccessorElement) { // The annotation is a reference to a compile-time constant variable. // Just copy the evaluation result. - VariableElementImpl variableElement = - element.variable.declaration as VariableElementImpl; - if (variableElement.evaluationResult != null) { - constant.evaluationResult = variableElement.evaluationResult; + var variableElement = + element.variable2?.declaration as VariableElementImpl?; + var evaluationResult = variableElement?.evaluationResult; + if (evaluationResult != null) { + constant.evaluationResult = evaluationResult; } else { // This could happen in the event that the annotation refers to a // non-constant. The error is detected elsewhere, so just silently @@ -301,7 +302,9 @@ class ConstantEvaluationEngine { if (element is PropertyAccessorElement) { // The annotation is a reference to a compile-time constant variable, // so it depends on the variable. - callback(element.variable.declaration); + if (element.variable2 case var variable?) { + callback(variable.declaration); + } } else if (element is ConstructorElement) { // The annotation is a constructor invocation, so it depends on the // constructor. @@ -1688,8 +1691,9 @@ class ConstantVisitor extends UnifyingAstVisitor { }) { var errorNode2 = evaluationEngine.configuration.errorNode(errorNode); element = element?.declaration; + var variableElement = - element is PropertyAccessorElement ? element.variable : element; + element is PropertyAccessorElement ? element.variable2 : element; // TODO(srawlins): Remove this check when [FunctionReference]s are inserted // for generic function instantiation for pre-constructor-references code. @@ -2654,7 +2658,16 @@ class _InstanceCreationEvaluator { _fieldMap[fieldName] = evaluationResult; var getter = definingType.getGetter(fieldName); if (getter != null) { - var field = getter.variable; + var field = getter.variable2; + if (field == null) { + return _InitializersEvaluationResult( + InvalidConstant.forElement( + getter, + CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, + ), + evaluationIsComplete: true, + ); + } if (!typeSystem.runtimeTypeMatch(evaluationResult, field.type)) { // Mark the type mismatch error as a runtime exception if the // initializer is statically assignable to the field. diff --git a/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart b/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart index d8990f40681e..cb9bf92e8d4a 100644 --- a/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart +++ b/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart @@ -260,7 +260,10 @@ class _Collector { return; } if (element is PropertyAccessorElement && element.isGetter) { - var variable = element.variable; + var variable = element.variable2; + if (variable == null) { + return; + } if (!variable.isConst) { nodes.add(node); } @@ -313,7 +316,10 @@ class _Collector { var element = node.propertyName.staticElement; if (element is PropertyAccessorElement && element.isGetter) { - var variable = element.variable; + var variable = element.variable2; + if (variable == null) { + return; + } if (!variable.isConst) { nodes.add(node.propertyName); } diff --git a/pkg/analyzer/lib/src/dart/constant/utilities.dart b/pkg/analyzer/lib/src/dart/constant/utilities.dart index 41701106ebe2..9089f6055254 100644 --- a/pkg/analyzer/lib/src/dart/constant/utilities.dart +++ b/pkg/analyzer/lib/src/dart/constant/utilities.dart @@ -246,7 +246,7 @@ class ReferenceFinder extends RecursiveAstVisitor { void visitSimpleIdentifier(SimpleIdentifier node) { var staticElement = node.staticElement; var element = staticElement is PropertyAccessorElement - ? staticElement.variable + ? staticElement.variable2 : staticElement; if (element is VariableElement && element.isConst) { _callback(element); diff --git a/pkg/analyzer/lib/src/dart/element/display_string_builder.dart b/pkg/analyzer/lib/src/dart/element/display_string_builder.dart index 0efc39858d94..19c95eafb5f4 100644 --- a/pkg/analyzer/lib/src/dart/element/display_string_builder.dart +++ b/pkg/analyzer/lib/src/dart/element/display_string_builder.dart @@ -95,6 +95,18 @@ class ElementDisplayStringBuilder { _writeTypesIfNotEmpty(' implements ', element.interfaces); } + // void writePropertyAccessor(PropertyAccessorElement element) { + // var variable = element.variable2; + // if (variable == null) { + // // builder.; + // } + // + // writeExecutableElement( + // element, + // (element.isGetter ? 'get ' : 'set ') + variable2.displayName, + // ); + // } + void writeExecutableElement(ExecutableElement element, String name) { if (element.isAugmentation) { _write('augment '); diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 871a04c15f52..36c8077f02dd 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -5965,7 +5965,7 @@ class ParameterElementImpl_ofImplicitSetter extends ParameterElementImpl { ParameterElementImpl_ofImplicitSetter(this.setter) : super( - name: considerCanonicalizeString('_${setter.variable.name}'), + name: considerCanonicalizeString('_${setter.variable2.name}'), nameOffset: -1, parameterKind: ParameterKind.REQUIRED, ) { @@ -5975,7 +5975,7 @@ class ParameterElementImpl_ofImplicitSetter extends ParameterElementImpl { @override bool get inheritsCovariant { - var variable = setter.variable; + var variable = setter.variable2; if (variable is FieldElementImpl) { return variable.inheritsCovariant; } @@ -5984,7 +5984,7 @@ class ParameterElementImpl_ofImplicitSetter extends ParameterElementImpl { @override set inheritsCovariant(bool value) { - var variable = setter.variable; + var variable = setter.variable2; if (variable is FieldElementImpl) { variable.inheritsCovariant = value; } @@ -6000,7 +6000,7 @@ class ParameterElementImpl_ofImplicitSetter extends ParameterElementImpl { @override bool get isExplicitlyCovariant { - var variable = setter.variable; + var variable = setter.variable2; if (variable is FieldElementImpl) { return variable.isCovariant; } @@ -6009,11 +6009,11 @@ class ParameterElementImpl_ofImplicitSetter extends ParameterElementImpl { @override Element get nonSynthetic { - return setter.variable; + return setter.variable2; } @override - DartType get type => setter.variable.type; + DartType get type => setter.variable2.type; @override set type(DartType type) { @@ -6166,7 +6166,7 @@ class PrefixElementImpl extends _ExistingElementImpl implements PrefixElement { class PropertyAccessorElementImpl extends ExecutableElementImpl with AugmentableElement implements PropertyAccessorElement { - late PropertyInducingElementImpl _variable; + PropertyInducingElementImpl? _variable; /// If this method is a synthetic element which is based on another method /// with some modifications (such as making some parameters covariant), @@ -6179,11 +6179,11 @@ class PropertyAccessorElementImpl extends ExecutableElementImpl /// Initialize a newly created synthetic property accessor element to be /// associated with the given [variable]. - PropertyAccessorElementImpl.forVariable(this._variable, + PropertyAccessorElementImpl.forVariable(PropertyInducingElementImpl variable, {Reference? reference}) - : super(_variable.name, -1, reference: reference) { - isAbstract = variable is FieldElementImpl && - (variable as FieldElementImpl).isAbstract; + : _variable = variable, + super(variable.name, -1, reference: reference) { + isAbstract = variable is FieldElementImpl && variable.isAbstract; isStatic = variable.isStatic; isSynthetic = true; } @@ -6193,7 +6193,7 @@ class PropertyAccessorElementImpl extends ExecutableElementImpl if (isGetter) { return null; } - return variable.getter; + return variable2?.getter; } @override @@ -6201,7 +6201,7 @@ class PropertyAccessorElementImpl extends ExecutableElementImpl if (isSetter) { return null; } - return variable.setter; + return variable2?.setter; } @override @@ -6261,13 +6261,19 @@ class PropertyAccessorElementImpl extends ExecutableElementImpl return super.name; } + @Deprecated('Use variable2') @override PropertyInducingElementImpl get variable { + return variable2!; + } + + @override + PropertyInducingElementImpl? get variable2 { linkedData?.read(this); return _variable; } - set variable(PropertyInducingElementImpl value) { + set variable2(PropertyInducingElementImpl? value) { _variable = value; } @@ -6279,7 +6285,7 @@ class PropertyAccessorElementImpl extends ExecutableElementImpl void appendTo(ElementDisplayStringBuilder builder) { builder.writeExecutableElement( this, - (isGetter ? 'get ' : 'set ') + variable.displayName, + (isGetter ? 'get ' : 'set ') + displayName, ); } } @@ -6297,26 +6303,25 @@ class PropertyAccessorElementImpl_ImplicitGetter } @override - Element get enclosingElement => variable.enclosingElement; + Element get enclosingElement => variable2.enclosingElement; @override - bool get hasImplicitReturnType => variable.hasImplicitType; + bool get hasImplicitReturnType => variable2.hasImplicitType; @override bool get isGetter => true; @override Element get nonSynthetic { - final variable = this.variable; - if (!variable.isSynthetic) { - return variable; + if (!variable2.isSynthetic) { + return variable2; } assert(enclosingElement is EnumElementImpl); return enclosingElement; } @override - DartType get returnType => variable.type; + DartType get returnType => variable2.type; @override set returnType(DartType returnType) { @@ -6324,7 +6329,7 @@ class PropertyAccessorElementImpl_ImplicitGetter } @override - Version? get sinceSdkVersion => variable.sinceSdkVersion; + Version? get sinceSdkVersion => variable2.sinceSdkVersion; @override FunctionType get type { @@ -6340,6 +6345,9 @@ class PropertyAccessorElementImpl_ImplicitGetter set type(FunctionType type) { assert(false); // Should never be called. } + + @override + PropertyInducingElementImpl get variable2 => super.variable2!; } /// Implicit setter for a [PropertyInducingElementImpl]. @@ -6354,13 +6362,13 @@ class PropertyAccessorElementImpl_ImplicitSetter } @override - Element get enclosingElement => variable.enclosingElement; + Element get enclosingElement => variable2.enclosingElement; @override bool get isSetter => true; @override - Element get nonSynthetic => variable; + Element get nonSynthetic => variable2; @override List get parameters { @@ -6382,7 +6390,7 @@ class PropertyAccessorElementImpl_ImplicitSetter } @override - Version? get sinceSdkVersion => variable.sinceSdkVersion; + Version? get sinceSdkVersion => variable2.sinceSdkVersion; @override FunctionType get type { @@ -6398,6 +6406,9 @@ class PropertyAccessorElementImpl_ImplicitSetter set type(FunctionType type) { assert(false); // Should never be called. } + + @override + PropertyInducingElementImpl get variable2 => super.variable2!; } /// A concrete implementation of a [PropertyInducingElement]. diff --git a/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart b/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart index 2d25de12d749..f69c82efc8d2 100644 --- a/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart +++ b/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart @@ -969,13 +969,13 @@ class InheritanceManager3 { result.prototype = executable; result.returnType = executable.returnType; - var field = executable.variable; + var field = executable.variable2!; var resultField = FieldElementImpl(field.name, -1); resultField.enclosingElement = class_; resultField.getter = field.getter; resultField.setter = executable; resultField.type = executable.parameters[0].type; - result.variable = resultField; + result.variable2 = resultField; return result; } @@ -1049,7 +1049,7 @@ class InheritanceManager3 { field.setter = result; field.type = result.parameters[0].type; } - result.variable = field; + result.variable2 = field; return result; } diff --git a/pkg/analyzer/lib/src/dart/element/member.dart b/pkg/analyzer/lib/src/dart/element/member.dart index 4b72f12a0015..585fc99119de 100644 --- a/pkg/analyzer/lib/src/dart/element/member.dart +++ b/pkg/analyzer/lib/src/dart/element/member.dart @@ -1036,8 +1036,13 @@ class PropertyAccessorMember extends ExecutableMember @override PropertyInducingElement get variable { + return variable2!; + } + + @override + PropertyInducingElement? get variable2 { // TODO(scheglov): revisit - PropertyInducingElement variable = declaration.variable; + var variable = declaration.variable2; if (variable is FieldElement) { return FieldMember(variable, augmentationSubstitution, _substitution); } else if (variable is TopLevelVariableElement) { diff --git a/pkg/analyzer/lib/src/dart/micro/utils.dart b/pkg/analyzer/lib/src/dart/micro/utils.dart index 6b9b85ea52e6..c1cefd058f0f 100644 --- a/pkg/analyzer/lib/src/dart/micro/utils.dart +++ b/pkg/analyzer/lib/src/dart/micro/utils.dart @@ -220,7 +220,7 @@ class ReferencesCollector extends GeneralizingAstVisitor { node.writeElement is PropertyAccessorElement) { var kind = MatchKind.WRITE; var property = node.writeElement as PropertyAccessorElement; - if (property.variable == element || property == element) { + if (property.variable2 == element || property == element) { if (node.leftHandSide is SimpleIdentifier) { references.add(MatchInfo( node.leftHandSide.offset, node.leftHandSide.length, kind)); @@ -238,7 +238,7 @@ class ReferencesCollector extends GeneralizingAstVisitor { if (node.readElement != null && node.readElement is PropertyAccessorElement) { var property = node.readElement as PropertyAccessorElement; - if (property.variable == element) { + if (property.variable2 == element) { references.add(MatchInfo(node.rightHandSide.offset, node.rightHandSide.length, MatchKind.READ)); } @@ -378,7 +378,7 @@ class ReferencesCollector extends GeneralizingAstVisitor { var e = node.staticElement; if (e == element) { references.add(MatchInfo(node.offset, node.length, MatchKind.REFERENCE)); - } else if (e is PropertyAccessorElement && e.variable == element) { + } else if (e is PropertyAccessorElement && e.variable2 == element) { bool inGetterContext = node.inGetterContext(); bool inSetterContext = node.inSetterContext(); MatchKind kind; diff --git a/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart index 68a9d7b2bb0e..60db64535be1 100644 --- a/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart +++ b/pkg/analyzer/lib/src/dart/resolver/annotation_resolver.dart @@ -324,7 +324,11 @@ class AnnotationResolver { Annotation annotation, PropertyAccessorElement accessorElement) { // The accessor should be synthetic, the variable should be constant, and // there should be no arguments. - VariableElement variableElement = accessorElement.variable; + var variableElement = accessorElement.variable2; + if (variableElement == null) { + return; + } + if (!accessorElement.isSynthetic || !variableElement.isConst || annotation.arguments != null) { diff --git a/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart b/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart index fae48ab30851..ad5974458e5c 100644 --- a/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart +++ b/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart @@ -545,7 +545,7 @@ class TypeSystemOperations @override bool isPropertyPromotable(Object property) { if (property is! PropertyAccessorElement) return false; - var field = property.variable; + var field = property.variable2; if (field is! FieldElement) return false; return field.isPromotable; } @@ -768,7 +768,7 @@ class TypeSystemOperations if (property is! PropertyAccessorElement) { return PropertyNonPromotabilityReason.isNotField; } - var field = property.variable; + var field = property.variable2; if (field is! FieldElement) { return PropertyNonPromotabilityReason.isNotField; } diff --git a/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart index 3f4209896846..4bbf98791562 100644 --- a/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart +++ b/pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart @@ -93,7 +93,11 @@ class FunctionReferenceResolver { if (prefixElement is VariableElement) { prefixType = prefixElement.type; } else if (prefixElement is PropertyAccessorElement) { - prefixType = prefixElement.variable.type; + var variable = prefixElement.variable2; + if (variable == null) { + return false; + } + prefixType = variable.type; } if (prefixType is DynamicType) { @@ -512,7 +516,12 @@ class FunctionReferenceResolver { if (targetElement is VariableElement) { targetType = targetElement.type; } else if (targetElement is PropertyAccessorElement) { - targetType = targetElement.variable.type; + var variable = targetElement.variable2; + if (variable == null) { + node.staticType = InvalidTypeImpl.instance; + return; + } + targetType = variable.type; } else { // TODO(srawlins): Can we get here? node.staticType = DynamicTypeImpl.instance; @@ -688,7 +697,13 @@ class FunctionReferenceResolver { if (method is PropertyAccessorElement) { function.staticElement = method; function.staticType = method.returnType; - _resolve(node: node, rawType: method.variable.type); + var variable = method.variable2; + if (variable != null) { + _resolve(node: node, rawType: variable.type); + } else { + function.staticType = InvalidTypeImpl.instance; + node.staticType = InvalidTypeImpl.instance; + } return; } @@ -745,8 +760,13 @@ class FunctionReferenceResolver { return; } else if (element is PropertyAccessorElement) { function.staticElement = element; - function.staticType = element.variable.type; - var callMethod = _getCallMethod(node, element.variable.type); + var variable = element.variable2; + if (variable == null) { + function.staticType = InvalidTypeImpl.instance; + return; + } + function.staticType = variable.type; + var callMethod = _getCallMethod(node, variable.type); if (callMethod is MethodElement) { _resolveAsImplicitCallReference(node, callMethod); return; diff --git a/pkg/analyzer/lib/src/dart/resolver/simple_identifier_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/simple_identifier_resolver.dart index 524dcfb716d4..086cdacfbc44 100644 --- a/pkg/analyzer/lib/src/dart/resolver/simple_identifier_resolver.dart +++ b/pkg/analyzer/lib/src/dart/resolver/simple_identifier_resolver.dart @@ -59,7 +59,7 @@ class SimpleIdentifierResolver with ScopeHelpers { if (parameterTypes.isNotEmpty) { return parameterTypes[0]; } - var getter = accessor.variable.getter; + var getter = accessor.variable2?.getter; if (getter != null) { functionType = getter.type; return functionType.returnType; diff --git a/pkg/analyzer/lib/src/error/assignment_verifier.dart b/pkg/analyzer/lib/src/error/assignment_verifier.dart index 75aad8b74973..c77e90989df3 100644 --- a/pkg/analyzer/lib/src/error/assignment_verifier.dart +++ b/pkg/analyzer/lib/src/error/assignment_verifier.dart @@ -70,7 +70,10 @@ class AssignmentVerifier { arguments: [recovery.name], ); } else if (recovery is PropertyAccessorElement && recovery.isGetter) { - var variable = recovery.variable; + var variable = recovery.variable2; + if (variable == null) { + return; + } if (variable.isConst) { _errorReporter.atNode( node, diff --git a/pkg/analyzer/lib/src/error/best_practices_verifier.dart b/pkg/analyzer/lib/src/error/best_practices_verifier.dart index 76c30327b62b..63b1153b842a 100644 --- a/pkg/analyzer/lib/src/error/best_practices_verifier.dart +++ b/pkg/analyzer/lib/src/error/best_practices_verifier.dart @@ -1525,7 +1525,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor { } } if (element is PropertyAccessorElement && element.isSynthetic) { - element = element.variable; + element = element.variable2; } if (element != null && element.hasOrInheritsDoNotStore) { @@ -1587,7 +1587,10 @@ class BestPracticesVerifier extends RecursiveAstVisitor { static bool _hasNonVirtualAnnotation(ExecutableElement element) { if (element is PropertyAccessorElement && element.isSynthetic) { - return element.variable.hasNonVirtual; + var variable = element.variable2; + if (variable != null && variable.hasNonVirtual) { + return true; + } } return element.hasNonVirtual; } @@ -1918,17 +1921,28 @@ class _InvalidAccessVerifier { if (element.hasInternal) { return true; } - if (element is PropertyAccessorElement && element.variable.hasInternal) { - return true; + if (element is PropertyAccessorElement) { + var variable = element.variable2; + if (variable == null) { + return false; + } + if (variable.hasInternal) { + return true; + } } return false; } bool _hasProtected(Element element) { if (element is PropertyAccessorElement && - element.enclosingElement is InterfaceElement && - (element.hasProtected || element.variable.hasProtected)) { - return true; + element.enclosingElement is InterfaceElement) { + if (element.hasProtected) { + return true; + } + var variable = element.variable2; + if (variable != null && variable.hasProtected) { + return true; + } } if (element is MethodElement && element.enclosingElement is InterfaceElement && @@ -1953,9 +1967,9 @@ class _InvalidAccessVerifier { return true; } - if (element is PropertyAccessorElement && - element.variable.hasVisibleForOverriding) { - return true; + if (element is PropertyAccessorElement) { + var variable = element.variable2; + return variable != null && variable.hasVisibleForOverriding; } return false; @@ -1968,9 +1982,11 @@ class _InvalidAccessVerifier { if (element.hasVisibleForTemplate) { return true; } - if (element is PropertyAccessorElement && - element.variable.hasVisibleForTemplate) { - return true; + if (element is PropertyAccessorElement) { + var variable = element.variable2; + if (variable != null && variable.hasVisibleForTemplate) { + return true; + } } final enclosingElement = element.enclosingElement; if (_hasVisibleForTemplate(enclosingElement)) { @@ -1983,9 +1999,9 @@ class _InvalidAccessVerifier { if (element.hasVisibleForTesting) { return true; } - if (element is PropertyAccessorElement && - element.variable.hasVisibleForTesting) { - return true; + if (element is PropertyAccessorElement) { + var variable = element.variable2; + return variable != null && variable.hasVisibleForTesting; } return false; } @@ -1994,9 +2010,11 @@ class _InvalidAccessVerifier { if (element.hasVisibleOutsideTemplate) { return true; } - if (element is PropertyAccessorElement && - element.variable.hasVisibleOutsideTemplate) { - return true; + if (element is PropertyAccessorElement) { + var variable = element.variable2; + if (variable != null && variable.hasVisibleOutsideTemplate) { + return true; + } } final enclosingElement = element.enclosingElement; if (enclosingElement != null && diff --git a/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart b/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart index cd6f32f32f9d..3da477a3f118 100644 --- a/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart +++ b/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart @@ -239,7 +239,11 @@ abstract class BaseDeprecatedMemberUseVerifier { {required bool strictCasts}) { // Implicit getters/setters. if (element.isSynthetic && element is PropertyAccessorElement) { - element = element.variable; + var variable = element.variable2; + if (variable == null) { + return null; + } + element = variable; } var annotation = element.metadata.firstWhereOrNull((e) => e.isDeprecated); if (annotation == null || annotation.element is PropertyAccessorElement) { @@ -266,8 +270,8 @@ abstract class BaseDeprecatedMemberUseVerifier { if (element is PropertyAccessorElement && element.isSynthetic) { // TODO(brianwilkerson): Why isn't this the implementation for PropertyAccessorElement? - Element variable = element.variable; - return variable.hasDeprecated; + var variable = element.variable2; + return variable != null && variable.hasDeprecated; } return element.hasDeprecated; } diff --git a/pkg/analyzer/lib/src/error/imports_verifier.dart b/pkg/analyzer/lib/src/error/imports_verifier.dart index 892a81f2b435..a3fbc77ed11d 100644 --- a/pkg/analyzer/lib/src/error/imports_verifier.dart +++ b/pkg/analyzer/lib/src/error/imports_verifier.dart @@ -684,7 +684,8 @@ class ImportsVerifier { if (element is PropertyAccessorElement) { // If the getter or setter of a variable is used, then the variable (the // shown name) is used. - if (hasElement(identifier, element.variable)) { + var variable = element.variable2; + if (variable != null && hasElement(identifier, variable)) { identifiers.remove(identifier); break; } diff --git a/pkg/analyzer/lib/src/error/inheritance_override.dart b/pkg/analyzer/lib/src/error/inheritance_override.dart index a802a4076ea2..8a16b8dfa400 100644 --- a/pkg/analyzer/lib/src/error/inheritance_override.dart +++ b/pkg/analyzer/lib/src/error/inheritance_override.dart @@ -920,7 +920,7 @@ class _ClassVerifier { continue; } if (accessor.hasMustBeOverridden || - accessor.variable.hasMustBeOverridden) { + (accessor.variable2?.hasMustBeOverridden ?? false)) { final PropertyAccessorElement? accessorDeclaration; if (accessor.isGetter) { accessorDeclaration = classElement.getGetter(accessor.name); diff --git a/pkg/analyzer/lib/src/error/unused_local_elements_verifier.dart b/pkg/analyzer/lib/src/error/unused_local_elements_verifier.dart index d89cff6dffe7..22714e90da44 100644 --- a/pkg/analyzer/lib/src/error/unused_local_elements_verifier.dart +++ b/pkg/analyzer/lib/src/error/unused_local_elements_verifier.dart @@ -16,6 +16,7 @@ import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/dart/element/inheritance_manager3.dart'; import 'package:analyzer/src/dart/element/member.dart' show ExecutableMember; import 'package:analyzer/src/error/codes.dart'; +import 'package:analyzer/src/utilities/extensions/object.dart'; import 'package:collection/collection.dart'; /// An [AstVisitor] that fills [UsedLocalElements]. @@ -246,12 +247,13 @@ class GatherUsedLocalElementsVisitor extends RecursiveAstVisitor { if (element is ExecutableMember) { element = element.declaration; } + var variable = element.ifTypeOrNull()?.variable2; bool isIdentifierRead = _isReadIdentifier(node); if (element is PropertyAccessorElement && isIdentifierRead && - element.variable is TopLevelVariableElement) { + variable is TopLevelVariableElement) { if (element.isSynthetic) { - usedElements.addElement(element.variable); + usedElements.addElement(variable); } else { usedElements.members.add(element); _addMemberAndCorrespondingGetter(element); diff --git a/pkg/analyzer/lib/src/error/use_result_verifier.dart b/pkg/analyzer/lib/src/error/use_result_verifier.dart index a2c7a2e0d5d8..a579acddff88 100644 --- a/pkg/analyzer/lib/src/error/use_result_verifier.dart +++ b/pkg/analyzer/lib/src/error/use_result_verifier.dart @@ -168,7 +168,11 @@ class UseResultVerifier { static ElementAnnotation? _getUseResultMetadata(Element element) { // Implicit getters/setters. if (element.isSynthetic && element is PropertyAccessorElement) { - element = element.variable; + var variable = element.variable2; + if (variable == null) { + return null; + } + element = variable; } return element.metadata.firstWhereOrNull((e) => e.isUseResult); } diff --git a/pkg/analyzer/lib/src/generated/element_resolver.dart b/pkg/analyzer/lib/src/generated/element_resolver.dart index 47db46b1560c..12203d6a3cc7 100644 --- a/pkg/analyzer/lib/src/generated/element_resolver.dart +++ b/pkg/analyzer/lib/src/generated/element_resolver.dart @@ -487,7 +487,7 @@ class ElementResolver { // Ensure that the name always resolves to a top-level variable // rather than a getter or setter if (element is PropertyAccessorElement) { - name.staticElement = element.variable; + name.staticElement = element.variable2; } else { name.staticElement = element; } diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 1b49272f0ecf..6cea649f7ddb 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -1798,7 +1798,10 @@ class ErrorVerifier extends RecursiveAstVisitor ); } } else if (element is PropertyAccessorElement && element.isGetter) { - var variable = element.variable; + var variable = element.variable2; + if (variable == null) { + return; + } if (variable.isConst) { errorReporter.atNode( expression, diff --git a/pkg/analyzer/lib/src/generated/ffi_verifier.dart b/pkg/analyzer/lib/src/generated/ffi_verifier.dart index 70d7454f3513..447a3fa9a483 100644 --- a/pkg/analyzer/lib/src/generated/ffi_verifier.dart +++ b/pkg/analyzer/lib/src/generated/ffi_verifier.dart @@ -489,7 +489,11 @@ class FfiVerifier extends RecursiveAstVisitor { } else if (declarationElement is TopLevelVariableElement) { type = declarationElement.type; } else if (declarationElement is PropertyAccessorElement) { - type = declarationElement.variable.type; + var variable = declarationElement.variable2; + if (variable == null) { + return; + } + type = variable.type; } else { _errorReporter.atNode( errorNode, @@ -665,7 +669,7 @@ class FfiVerifier extends RecursiveAstVisitor { return true; } if (staticElm is PropertyAccessorElementImpl) { - if (staticElm.variable is ConstVariableElement) { + if (staticElm.variable2 is ConstVariableElement) { return true; } } @@ -841,9 +845,12 @@ class FfiVerifier extends RecursiveAstVisitor { return staticElm.computeConstantValue()?.toBoolValue(); } if (staticElm is PropertyAccessorElementImpl) { - final v = staticElm.variable; - if (v is ConstVariableElement) { - return v.computeConstantValue()?.toBoolValue(); + final variable = staticElm.variable2; + if (variable == null) { + return null; + } + if (variable is ConstVariableElement) { + return variable.computeConstantValue()?.toBoolValue(); } } } diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index 5518ec9f0aa7..e4918791b7d7 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -1657,7 +1657,10 @@ class ResolverVisitor extends ThrowingAstVisitor node is SimpleIdentifier) { if (element is PropertyAccessorElement && element.isSetter) { if (element.isSynthetic) { - writeType = element.variable.type; + var variable = element.variable2; + if (variable != null) { + writeType = variable.type; + } } else { var parameters = element.parameters; if (parameters.length == 1) { @@ -5115,7 +5118,7 @@ class SwitchExhaustiveness { if (caseConstant != null) { var element = _referencedElement(caseConstant); if (element is PropertyAccessorElement) { - _enumConstants!.remove(element.variable); + _enumConstants!.remove(element.variable2); } if (caseConstant is NullLiteral) { _isNullEnumValueCovered = true; diff --git a/pkg/analyzer/lib/src/generated/testing/element_factory.dart b/pkg/analyzer/lib/src/generated/testing/element_factory.dart index 03efd69d875d..49b930dad301 100644 --- a/pkg/analyzer/lib/src/generated/testing/element_factory.dart +++ b/pkg/analyzer/lib/src/generated/testing/element_factory.dart @@ -193,7 +193,7 @@ class ElementFactory { PropertyAccessorElementImpl getter = PropertyAccessorElementImpl(name, 0); getter.isSynthetic = false; getter.isGetter = true; - getter.variable = field; + getter.variable2 = field; getter.returnType = type; getter.isStatic = isStatic; field.getter = getter; @@ -331,14 +331,14 @@ class ElementFactory { field.type = type; PropertyAccessorElementImpl getter = PropertyAccessorElementImpl(name, -1); getter.isGetter = true; - getter.variable = field; + getter.variable2 = field; getter.returnType = type; field.getter = getter; ParameterElementImpl parameter = requiredParameter2("a", type); PropertyAccessorElementImpl setter = PropertyAccessorElementImpl(name, -1); setter.isSetter = true; setter.isSynthetic = true; - setter.variable = field; + setter.variable2 = field; setter.parameters = [parameter]; setter.returnType = VoidTypeImpl.instance; setter.isStatic = isStatic; diff --git a/pkg/analyzer/lib/src/services/top_level_declarations.dart b/pkg/analyzer/lib/src/services/top_level_declarations.dart index 44ce77f5dddb..5f1aee017d21 100644 --- a/pkg/analyzer/lib/src/services/top_level_declarations.dart +++ b/pkg/analyzer/lib/src/services/top_level_declarations.dart @@ -106,6 +106,12 @@ class TopLevelDeclarations { static Element? _findElement(LibraryElement libraryElement, String name) { var element = libraryElement.exportNamespace.get(name) ?? libraryElement.exportNamespace.get('$name='); - return element is PropertyAccessorElement ? element.variable : element; + if (element is PropertyAccessorElement) { + var variable = element.variable2; + if (variable != null) { + return variable; + } + } + return element; } } diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart index a31a95f64156..89ae8c90de4b 100644 --- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart +++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart @@ -1593,7 +1593,7 @@ class LibraryReader { } } - accessor.variable = property; + accessor.variable2 = property; if (isGetter) { property.getter = accessor; } else { @@ -1910,7 +1910,7 @@ class PropertyAccessorElementLinkedData if (augmentationTarget is PropertyAccessorElementImpl) { augmentationTarget.augmentation = element; element.augmentationTarget = augmentationTarget; - element.variable = augmentationTarget.variable; + element.variable2 = augmentationTarget.variable2; } applyConstantOffsets?.perform(); diff --git a/pkg/analyzer/lib/src/summary2/element_builder.dart b/pkg/analyzer/lib/src/summary2/element_builder.dart index d75f0778f48a..5aa41a72a18c 100644 --- a/pkg/analyzer/lib/src/summary2/element_builder.dart +++ b/pkg/analyzer/lib/src/summary2/element_builder.dart @@ -1353,7 +1353,7 @@ class ElementBuilder extends ThrowingAstVisitor { } } - accessorElement.variable = property; + accessorElement.variable2 = property; if (accessorElement.isGetter) { property.getter = accessorElement; } else { diff --git a/pkg/analyzer/lib/src/summary2/library_builder.dart b/pkg/analyzer/lib/src/summary2/library_builder.dart index 6485a57a602e..7b0d5c57b8a5 100644 --- a/pkg/analyzer/lib/src/summary2/library_builder.dart +++ b/pkg/analyzer/lib/src/summary2/library_builder.dart @@ -72,7 +72,7 @@ abstract class AugmentedInstanceDeclarationBuilder { if (existing != null) { existing.augmentation = element; element.augmentationTarget = existing; - element.variable = existing.variable; + element.variable2 = existing.variable2; } } accessors[name] = element; @@ -152,7 +152,7 @@ class AugmentedTopVariablesBuilder { if (existing != null) { existing.augmentation = element; element.augmentationTarget = existing; - element.variable = existing.variable; + element.variable2 = existing.variable2; } } accessors[name] = element; @@ -1451,7 +1451,8 @@ class _FieldPromotability extends FieldPromotability accessor.variable) + .map((accessor) => accessor.variable2) .cast() .toList(); } diff --git a/pkg/analyzer/test/generated/element_resolver_test.dart b/pkg/analyzer/test/generated/element_resolver_test.dart index 2293b9b87244..a45ebc3f4e42 100644 --- a/pkg/analyzer/test/generated/element_resolver_test.dart +++ b/pkg/analyzer/test/generated/element_resolver_test.dart @@ -366,7 +366,7 @@ export 'dart:math' hide pi; .exportedLibrary! .exportNamespace .get('pi') as PropertyAccessorElement; - expect(findNode.simple('pi').staticElement, pi.variable); + expect(findNode.simple('pi').staticElement, pi.variable2); } test_visitExportDirective_noCombinators() async { @@ -399,7 +399,7 @@ import 'dart:math' show pi; .importedLibrary! .exportNamespace .get('pi') as PropertyAccessorElement; - expect(findNode.simple('pi').staticElement, pi.variable); + expect(findNode.simple('pi').staticElement, pi.variable2); } test_visitImportDirective_combinators_prefix() async { @@ -411,9 +411,9 @@ import 'dart:math' as p show pi hide ln10; var mathNamespace = findElement.import('dart:math').importedLibrary!.exportNamespace; var pi = mathNamespace.get('pi') as PropertyAccessorElement; - expect(findNode.simple('pi').staticElement, pi.variable); + expect(findNode.simple('pi').staticElement, pi.variable2); var ln10 = mathNamespace.get('ln10') as PropertyAccessorElement; - expect(findNode.simple('ln10').staticElement, ln10.variable); + expect(findNode.simple('ln10').staticElement, ln10.variable2); } test_visitImportDirective_noCombinators_noPrefix() async { diff --git a/pkg/analyzer/test/id_tests/constant_test.dart b/pkg/analyzer/test/id_tests/constant_test.dart index c39539e3bae1..7a3763005838 100644 --- a/pkg/analyzer/test/id_tests/constant_test.dart +++ b/pkg/analyzer/test/id_tests/constant_test.dart @@ -62,7 +62,7 @@ class ConstantsDataExtractor extends AstDataExtractor { if (node is Identifier) { var element = node.staticElement; if (element is PropertyAccessorElement && element.isSynthetic) { - var variable = element.variable; + var variable = element.variable2!; if (!variable.isSynthetic && variable.isConst) { var value = variable.computeConstantValue(); if (value != null) return _stringify(value); diff --git a/pkg/analyzer/test/src/dart/element/element_test.dart b/pkg/analyzer/test/src/dart/element/element_test.dart index 99d83b2eca54..214cee6f310f 100644 --- a/pkg/analyzer/test/src/dart/element/element_test.dart +++ b/pkg/analyzer/test/src/dart/element/element_test.dart @@ -2007,7 +2007,7 @@ main() { '''); SimpleIdentifier argument = findNode.simple('C);'); var getter = argument.staticElement as PropertyAccessorElementImpl; - var constant = getter.variable as TopLevelVariableElement; + var constant = getter.variable2 as TopLevelVariableElement; DartObject value = constant.computeConstantValue()!; expect(value, isNotNull); diff --git a/pkg/analyzer/test/src/dart/element/inheritance_manager3_test.dart b/pkg/analyzer/test/src/dart/element/inheritance_manager3_test.dart index a083b210061c..af2c828a7c43 100644 --- a/pkg/analyzer/test/src/dart/element/inheritance_manager3_test.dart +++ b/pkg/analyzer/test/src/dart/element/inheritance_manager3_test.dart @@ -2700,7 +2700,7 @@ class _InheritanceManager3Base extends PubPackageResolutionTest { expect(actual, expected); if (element is PropertyAccessorElement) { - var variable = element.variable; + var variable = element.variable2!; expect(variable.enclosingElement, same(element.enclosingElement)); expect(variable.name, element.displayName); if (element.isGetter) { diff --git a/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart b/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart index abb3692630e1..7f0e68fcc9bd 100644 --- a/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart @@ -275,7 +275,7 @@ var v = a; '''); var vg = findNode.simple('a;').staticElement as PropertyAccessorElement; - var v = vg.variable as ConstVariableElement; + var v = vg.variable2 as ConstVariableElement; var creation = v.constantInitializer as InstanceCreationExpression; return creation; diff --git a/pkg/analyzer/test/src/summary/element_text.dart b/pkg/analyzer/test/src/summary/element_text.dart index a63d145dce12..a50183716b71 100644 --- a/pkg/analyzer/test/src/summary/element_text.dart +++ b/pkg/analyzer/test/src/summary/element_text.dart @@ -1146,14 +1146,17 @@ class _ElementWriter { void _writePropertyAccessorElement(PropertyAccessorElement e) { e as PropertyAccessorElementImpl; - PropertyInducingElement variable = e.variable; - expect(variable, isNotNull); - - var variableEnclosing = variable.enclosingElement; - if (variableEnclosing is CompilationUnitElement) { - expect(variableEnclosing.topLevelVariables, contains(variable)); - } else if (variableEnclosing is InterfaceElement) { - expect(variableEnclosing.fields, contains(variable)); + var variable = e.variable2; + if (variable != null) { + var variableEnclosing = variable.enclosingElement; + if (variableEnclosing is CompilationUnitElement) { + expect(variableEnclosing.topLevelVariables, contains(variable)); + } else if (variableEnclosing is InterfaceElement) { + expect(variableEnclosing.fields, contains(variable)); + } + } else { + expect(e.isAugmentation, isTrue); + expect(e.augmentationTarget, isNull); } if (e.isSynthetic) { @@ -1183,7 +1186,9 @@ class _ElementWriter { void writeLinking() { if (configuration.withPropertyLinking) { _sink.writelnWithIndent('id: ${_idMap[e]}'); - _sink.writelnWithIndent('variable: ${_idMap[e.variable]}'); + if (e.variable2 case final variable?) { + _sink.writelnWithIndent('variable: ${_idMap[variable]}'); + } } } diff --git a/pkg/analyzer/test/src/summary/elements_test.dart b/pkg/analyzer/test/src/summary/elements_test.dart index 6943c18462dc..ab80bb0a216c 100644 --- a/pkg/analyzer/test/src/summary/elements_test.dart +++ b/pkg/analyzer/test/src/summary/elements_test.dart @@ -47669,7 +47669,7 @@ library // requesting individual elements, not all accessors/variables at once. var getter = _elementOfDefiningUnit(library, ['@getter', 'x']) as PropertyAccessorElementImpl; - var variable = getter.variable as TopLevelVariableElementImpl; + var variable = getter.variable2 as TopLevelVariableElementImpl; expect(variable, isNotNull); expect(variable.isFinal, isFalse); expect(variable.getter, same(getter)); diff --git a/pkg/analyzer_plugin/lib/utilities/completion/type_member_contributor.dart b/pkg/analyzer_plugin/lib/utilities/completion/type_member_contributor.dart index f5f024db82b8..156236d6d06f 100644 --- a/pkg/analyzer_plugin/lib/utilities/completion/type_member_contributor.dart +++ b/pkg/analyzer_plugin/lib/utilities/completion/type_member_contributor.dart @@ -323,7 +323,9 @@ class _SuggestionBuilder { if (propertyAccessor.isSynthetic) { // Avoid visiting a field twice if (propertyAccessor.isGetter) { - _addSuggestion(propertyAccessor.variable); + if (propertyAccessor.variable2 case var variable?) { + _addSuggestion(variable); + } } } else { _addSuggestion(propertyAccessor); diff --git a/pkg/linter/lib/src/ast.dart b/pkg/linter/lib/src/ast.dart index c6b0816fe200..35fe821c2ead 100644 --- a/pkg/linter/lib/src/ast.dart +++ b/pkg/linter/lib/src/ast.dart @@ -335,7 +335,10 @@ bool _checkForSimpleGetter(MethodDeclaration getter, Expression? expression) { // Skipping library level getters, test that the enclosing element is // the same if (staticElement.enclosingElement == enclosingElement) { - return staticElement.isSynthetic && staticElement.variable.isPrivate; + var variable = staticElement.variable2; + if (variable != null) { + return staticElement.isSynthetic && variable.isPrivate; + } } } } diff --git a/pkg/linter/lib/src/extensions.dart b/pkg/linter/lib/src/extensions.dart index a0e4233e0e2d..4321f828f38b 100644 --- a/pkg/linter/lib/src/extensions.dart +++ b/pkg/linter/lib/src/extensions.dart @@ -286,7 +286,7 @@ extension ElementExtension on Element { Element get canonicalElement { var self = this; if (self is PropertyAccessorElement) { - var variable = self.variable; + var variable = self.variable2; if (variable is FieldMember) { // A field element defined in a parameterized type where the values of // the type parameters are known. @@ -296,12 +296,11 @@ extension ElementExtension on Element { // equivalent to equivalent FieldMembers. See // https://github.com/dart-lang/sdk/issues/35343. return variable.declaration; - } else { + } else if (variable != null) { return variable; } - } else { - return self; } + return self; } } diff --git a/pkg/linter/lib/src/rules/exhaustive_cases.dart b/pkg/linter/lib/src/rules/exhaustive_cases.dart index 317dbd58febb..7528252f2f8c 100644 --- a/pkg/linter/lib/src/rules/exhaustive_cases.dart +++ b/pkg/linter/lib/src/rules/exhaustive_cases.dart @@ -164,7 +164,12 @@ class _Visitor extends SimpleAstVisitor { extension on Element? { Element? get variableElement { var self = this; - if (self is PropertyAccessorElement) return self.variable; + if (self is PropertyAccessorElement) { + var variable = self.variable2; + if (variable != null) { + return variable; + } + } return self; } } diff --git a/pkg/linter/lib/src/rules/unnecessary_lambdas.dart b/pkg/linter/lib/src/rules/unnecessary_lambdas.dart index 9a71fe2eaedf..9bac67012365 100644 --- a/pkg/linter/lib/src/rules/unnecessary_lambdas.dart +++ b/pkg/linter/lib/src/rules/unnecessary_lambdas.dart @@ -66,9 +66,11 @@ class _FinalExpressionChecker { /// `late`. bool isFinalElement(Element? element) { if (element is PropertyAccessorElement) { + var variable = element.variable2; return element.isSynthetic && - element.variable.isFinal && - !element.variable.isLate; + variable != null && + variable.isFinal && + !variable.isLate; } else if (element is VariableElement) { return element.isFinal && !element.isLate; } diff --git a/pkg/linter/lib/src/util/leak_detector_visitor.dart b/pkg/linter/lib/src/util/leak_detector_visitor.dart index 7e7cfad9278e..ae28c77b549c 100644 --- a/pkg/linter/lib/src/util/leak_detector_visitor.dart +++ b/pkg/linter/lib/src/util/leak_detector_visitor.dart @@ -59,7 +59,7 @@ bool _isElementEqualToVariable( Element? propertyElement, VariableElement? variableElement) => propertyElement == variableElement || propertyElement is PropertyAccessorElement && - propertyElement.variable == variableElement; + propertyElement.variable2 == variableElement; bool _isInvocationThroughCascadeExpression( MethodInvocation invocation, VariableElement variableElement) { @@ -71,7 +71,7 @@ bool _isInvocationThroughCascadeExpression( if (identifier is SimpleIdentifier) { var element = identifier.staticElement; if (element is PropertyAccessorElement) { - return element.variable == variableElement; + return element.variable2 == variableElement; } } return false;