Skip to content

Conversation

lsrcz
Copy link
Collaborator

@lsrcz lsrcz commented Aug 30, 2025

AstCloner previously attempted to prevent double-cloning by checking the old_to_new_ mapping inside VisitChildren. However, this missed the case where ReplaceOrVisit was invoked directly on a node. In such cases, the same node could be cloned multiple times, breaking semantics.

For example, with the following code, the old logic would incorrectly produce distinct type entities for the argument and return type in the clone:

type MyU32 = u32;
fn foo(x: MyU32) -> MyU32 {
  x
}

This PR fixes the issue by moving the old_to_new_ check into ReplaceOrVisit and ensures nodes are only cloned once regardless of how they are visited.

This change revealed another bug: HandleTestFunction and HandleTestProc were not using CastIfNotVerbatim and would cause failure in the formatter test suite. This pull request make it consistent with existing implementations like HandleFunction and fixed the test failure.

@lsrcz lsrcz changed the title Fix duplicated cloned type entities bug [bugfix] Ensure nodes aren't cloned multiple times Sep 1, 2025
@mikex-oss mikex-oss requested a review from dplassgit September 4, 2025 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants