Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssignmentExpressionResolver doesn't resolve the leftHandSide? #59861

Open
nehzata opened this issue Jan 8, 2025 · 2 comments
Open

AssignmentExpressionResolver doesn't resolve the leftHandSide? #59861

nehzata opened this issue Jan 8, 2025 · 2 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@nehzata
Copy link

nehzata commented Jan 8, 2025

I'm trying to determine if an identifier is a class instance variables or not and have hit what seems like a bug but I'm not sure.

It seems that AssignmentExpressionResolver does not resolve the left hand side of an assignment expression correctly.

Smallest reproducible example is using resolveFile2 with the following file:

class Test {
  String _str = 't1';
  void fn1() {
    _str = 't2';
  }
  String fn2() {
    return _str;
  }
}

In the debugger I can see AssignmentExpressionResolver.resolve() being called when resolving fn1(), but neither the staticElement field on the AssignmentExpression node, nor the staticElement field on the leftHandSide (which is a SimpleIdentifier for _str) of the assignment expression node get updated and remain null. leftHandSide has a valid scopeLookupResult value.

In the case of fn2() all works fine and I can see staticElement of the SimpleIdentifier node associated with _str getting updated via SimpleIdentifierResolver.resolve().

I'm using SDK v3.6.0 with analyzer v7.1.0.

Any assistance would be greatly appreciated!

@nehzata nehzata added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jan 8, 2025
@nehzata
Copy link
Author

nehzata commented Jan 9, 2025

Further to the above, the patch below against 3.6.1 tag of the sdk seems to fix the problem for me but not sure if it's correct or not.

diff --git a/pkg/analyzer/lib/src/dart/resolver/assignment_expression_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/assignment_expression_resolver.dart
index 0c93d46f156..94fa77913b1 100644
--- a/pkg/analyzer/lib/src/dart/resolver/assignment_expression_resolver.dart
+++ b/pkg/analyzer/lib/src/dart/resolver/assignment_expression_resolver.dart
@@ -86,6 +86,9 @@ class AssignmentExpressionResolver {
             .getType(left as SimpleIdentifier, isRead: false);
       }
       rhsContext = _computeRhsContext(node, leftType!, operator, right);
+
+      _resolver.analyzeExpression(left, SharedTypeSchemaView(leftType));
+      left = _resolver.popRewrite()!;
     }

     var flow = _resolver.flowAnalysis.flow;

@scheglov & @stereotype441 FYI

@scheglov
Copy link
Contributor

scheglov commented Jan 9, 2025

Here is what I see.
Note writeElement in AssignmentExpression.

  solo_test_X() async {
    await assertNoErrorsInCode(r'''
class Test {
  String _str = 't1';
  void fn1() {
    _str = 't2';
  }
  String fn2() {
    return _str;
  }
}
''');

    var node = findNode.singleAssignmentExpression;
    assertResolvedNodeText(node, r'''
AssignmentExpression
  leftHandSide: SimpleIdentifier
    token: _str
    staticElement: <null>
    element: <null>
    staticType: null
  operator: =
  rightHandSide: SimpleStringLiteral
    literal: 't2'
  readElement: <null>
  readElement2: <null>
  readType: null
  writeElement: <testLibraryFragment>::@class::Test::@setter::_str
  writeElement2: <testLibraryFragment>::@class::Test::@setter::_str#element
  writeType: String
  staticElement: <null>
  element: <null>
  staticType: String
''');
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

2 participants