Skip to content

Commit

Permalink
Elements. Remove 'FunctionExpressionImpl.declaredElement2'
Browse files Browse the repository at this point in the history
Change-Id: Ie398dc23d6d5911dd02104ca4b31114d5d657153
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395050
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 14, 2024
1 parent c97b84e commit a1516ef
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class AddNullCheck extends ResolvedCorrectionProducer {
if (enclosingExecutable is MethodDeclaration) {
toType = enclosingExecutable.returnType?.type;
} else if (enclosingExecutable is FunctionExpressionImpl) {
toType = enclosingExecutable.returnType;
toType = enclosingExecutable.declaredFragment!.element.returnType;
}
} else if (parent is BinaryExpression) {
if (typeSystem.isNonNullable(fromType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class ConvertIntoBlockBody extends ResolvedCorrectionProducer {
} else if (node is ConstructorDeclaration) {
return node.declaredFragment?.element;
} else if (node is FunctionExpression) {
return node.declaredFragment?.element ?? node.declaredElement2;
return node.declaredFragment?.element;
}
return null;
}
Expand Down
28 changes: 1 addition & 27 deletions pkg/analyzer/lib/src/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8406,15 +8406,6 @@ abstract final class FunctionExpression implements Expression {
/// hasn't been resolved.
ExecutableElement? get declaredElement;

/// The element defined by this function expression.
///
/// Returns `null` if the AST structure hasn't been resolved.
///
/// Returns `null` if this expression is not a closure, and the parent is
/// not a local function.
@experimental
LocalFunctionElement? get declaredElement2;

/// The fragment declared by this function expression.
///
/// Returns `null` if the AST structure hasn't been resolved.
Expand Down Expand Up @@ -8451,7 +8442,7 @@ final class FunctionExpressionImpl extends ExpressionImpl
ExecutableElementImpl? declaredElement;

@override
LocalFunctionElementImpl? declaredElement2;
ExecutableElementImpl? declaredFragment;

/// Initializes a newly created function declaration.
FunctionExpressionImpl({
Expand Down Expand Up @@ -8483,14 +8474,6 @@ final class FunctionExpressionImpl extends ExpressionImpl
_body = _becomeParentOf(functionBody);
}

@override
ExecutableFragment? get declaredFragment {
if (declaredElement?.enclosingElement3 is CompilationUnitElement) {
return declaredElement;
}
return null;
}

@override
Token get endToken {
return _body.endToken;
Expand All @@ -8506,15 +8489,6 @@ final class FunctionExpressionImpl extends ExpressionImpl
@override
Precedence get precedence => Precedence.primary;

DartType get returnType {
// If a closure, or a local function.
if (declaredElement2 case var declaredElement?) {
return declaredElement.returnType;
}
// SAFETY: must be a top-level function.
return declaredFragment!.element.returnType;
}

@override
TypeParameterListImpl? get typeParameters => _typeParameters;

Expand Down
16 changes: 9 additions & 7 deletions pkg/analyzer/lib/src/dart/resolver/resolution_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,14 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
: _elementWalker!.getFunction();
node.declaredElement = element;
expression.declaredElement = element;
expression.declaredFragment = element;
} else {
var functionElement = node.declaredElement as FunctionElementImpl;
functionElement.element = LocalFunctionElementImpl(functionElement);

element = functionElement;
expression.declaredElement = functionElement;
expression.declaredElement2 =
functionElement.element as LocalFunctionElementImpl?;
expression.declaredFragment = functionElement;

_setCodeRange(element, node);
setElementDocumentationComment(element, node);
Expand Down Expand Up @@ -767,11 +769,11 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
@override
void visitFunctionExpression(covariant FunctionExpressionImpl node) {
var fragment = FunctionElementImpl.forOffset(node.offset);
fragment.element = LocalFunctionElementImpl(fragment);

_elementHolder.enclose(fragment);
node.declaredElement = fragment;

var element = LocalFunctionElementImpl(fragment);
node.declaredElement2 = element;
node.declaredFragment = fragment;

fragment.hasImplicitReturnType = true;
fragment.returnType = DynamicTypeImpl.instance;
Expand Down Expand Up @@ -1480,8 +1482,8 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
fragment.nameOffset2 = nameToken.offsetIfNotEmpty;
node.declaredElement = fragment;

var element = LocalFunctionElementImpl(fragment);
node.functionExpression.declaredElement2 = element;
fragment.element = LocalFunctionElementImpl(fragment);
node.functionExpression.declaredFragment = fragment;

// The fragment's old enclosing element needs to be set before we can get
// the new element for it.
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/error/error_handler_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class ErrorHandlerVerifier {
_checkErrorHandlerFunctionType(
callback, callbackType, expectedReturnType);
var catchErrorOnErrorExecutable = EnclosingExecutableContext.tmp(
callback.declaredElement2,
callback.declaredFragment!.element,
isAsynchronous: true,
catchErrorOnErrorReturnType: expectedReturnType);
var returnStatementVerifier =
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/test_utilities/find_element2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class FindElement2 extends _FindElementBase {
unit.accept(
FunctionAstVisitor(functionExpression: (node, local) {
if (local) {
var functionElement = node.declaredElement2!;
var functionElement = node.declaredFragment!.element;
findIn(functionElement.formalParameters);
}
}),
Expand Down
8 changes: 1 addition & 7 deletions pkg/linter/lib/src/rules/avoid_returning_null_for_void.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

import '../analyzer.dart';
Expand Down Expand Up @@ -59,7 +58,7 @@ class _Visitor extends SimpleAstVisitor<void> {

var (type, isAsync, code) = switch (parent) {
FunctionExpression() => (
parent.element?.returnType,
parent.declaredFragment?.element.returnType,
parent.body.isAsynchronous,
LinterLintCode.avoid_returning_null_for_void_from_function,
),
Expand All @@ -81,8 +80,3 @@ class _Visitor extends SimpleAstVisitor<void> {
}
}
}

extension on FunctionExpression {
ExecutableElement2? get element =>
declaredFragment?.element ?? declaredElement2;
}
5 changes: 2 additions & 3 deletions pkg/linter/lib/src/rules/unnecessary_lambdas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class _Visitor extends SimpleAstVisitor<void> {

@override
void visitFunctionExpression(FunctionExpression node) {
var element = node.declaredElement2 ?? node.declaredFragment?.element;
var element = node.declaredFragment?.element;
if (element?.name3 != '' || node.body.keyword != null) {
return;
}
Expand Down Expand Up @@ -140,8 +140,7 @@ class _Visitor extends SimpleAstVisitor<void> {
return;
}

var functionElement =
node.declaredElement2 ?? node.declaredFragment?.element;
var functionElement = node.declaredFragment?.element;

var nodeType = functionElement?.type;
var invocationType = expression.constructorName.element?.type;
Expand Down

0 comments on commit a1516ef

Please sign in to comment.