Stream: t-compiler/wg-rls-2.0

Topic: Using `SyntaxRewriter` to replace a single node


Jonas Schievink (Jun 13 2020 at 16:28, on Zulip):

I'm having a somewhat hard time to understand the code in SyntaxRewriter, but it seems to me that if the rewriter has exactly one replacement, rewrite_root will return the to-be-replaced node, but then calling rewrite with that node won't do anything because that only looks at children. Am I misunderstanding this or is this a bug in SyntaxRewriter?

(AssistBuilder::rewrite uses it in this way and doesn't seem to work correctly because of it)

matklad (Jun 13 2020 at 16:32, on Zulip):

Indeed looks like a bug!

matklad (Jun 13 2020 at 16:33, on Zulip):

Not suuuper trivial to fix though -- the obvious solution "allow rewrite to rewrite the node itself" doesn't work API wise, as rewriting might turn a single node into a bunch of node

matklad (Jun 13 2020 at 16:34, on Zulip):

So, I think rewrite_root needs to look at lca of "parents" of replacements

Jonas Schievink (Jun 13 2020 at 16:35, on Zulip):

Hmm, yeah

Jonas Schievink (Jun 13 2020 at 16:36, on Zulip):

Calling .parent().unwrap() on the result of least_common_ancestor seems to return the same node again

Jonas Schievink (Jun 13 2020 at 16:36, on Zulip):

Oh, because the closure never gets invoked when there's only one replacement

matklad (Jun 13 2020 at 16:37, on Zulip):

seems weird -- parent never returns the node itself

matklad (Jun 13 2020 at 16:37, on Zulip):

right!

matklad (Jun 13 2020 at 16:37, on Zulip):

I think you need .map(|it| it.parent().unwrap_or(it)) before the fold

Jonas Schievink (Jun 13 2020 at 16:40, on Zulip):

Seems to work, thanks!

Last update: Sep 27 2020 at 13:45UTC