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

[analyzer] NodeReplacer throws when replacing the block of a catch clause #47726

Closed
shyndman opened this issue Nov 18, 2021 · 7 comments
Closed
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@shyndman
Copy link

Hi there,

I found a small issue in the implementation of NodeReplacer.visitCatchClause, which prevents catch cause replacement.

bool visitCatchClause(covariant CatchClauseImpl node) {
if (identical(node.exceptionType, _oldNode)) {
node.exceptionType = _newNode as TypeAnnotation;
return true;
} else if (identical(node.exceptionParameter, _oldNode)) {
node.exceptionParameter = _newNode as SimpleIdentifier;
return true;
} else if (identical(node.stackTraceParameter, _oldNode)) {
node.stackTraceParameter = _newNode as SimpleIdentifier;
return true;
}
return visitNode(node);
}

You'll notice that there is no handling for the catch's body block, and as a result the code hits visitNode, and visitNode throws.

It's an easy fix. Happy to send a PR your way. For now I've gotten around it with a subclass.

Thanks!

@lrhn lrhn added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Nov 18, 2021
@bwilkerson
Copy link
Member

Glad you found a workaround. A PR would be much appreciated.

@shyndman
Copy link
Author

shyndman commented Nov 18, 2021

So I put together a test case, ran it, and oddly, it didn't fail.

Turns out some code was added somewhat recently that invalidates the testing approach — the first if statement in this snippet: (line 3012–3014)

static bool replace(AstNode oldNode, AstNode newNode, {AstNode? parent}) {
if (identical(oldNode, newNode)) {
return true;
}
parent ??= oldNode.parent;
if (parent == null) {
throw ArgumentError("The old node is not a child of another node");
}
NodeReplacer replacer = NodeReplacer(oldNode, newNode);
return parent.accept(replacer)!;
}
}

The NodeReplacer tests work by replacing a node with itself, then verifying that the tree isn't broken. With that early exit, the node replacement code isn't running.

BUT, on the upside, I commented out the early exit and tests are all passing (except for the one I added, yay!),.

Fixing this is a bit beyond the scope of what I'm wanting to do. Would you like me to send a PR for my catch body change, along with a test that will work when this other issue is resolved?

@bwilkerson
Copy link
Member

@scheglov @srawlins

@srawlins srawlins added P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Nov 18, 2021
@shyndman
Copy link
Author

So...still send the PR? I can at least fix one of the bugs.

@scheglov scheglov self-assigned this Nov 18, 2021
@scheglov
Copy link
Contributor

I will fix it, there is some work to be done in better testing.

@shyndman
Copy link
Author

sg. Thanks.

@scheglov
Copy link
Contributor

copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
Bug: #47726
Change-Id: I6eb301df8b1452660f06963008f9de861ea745d9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220806
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
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. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants