Avoid conflicting names for reactive scope codegen

This is a key part of avoiding generating conflicting names in our output. To 
start, RenameVariables now returns a Set of the unique identifier names that 
exist in the function. Codegen uses this to avoid generating duplicate names for 
change variables and for the `$` useMemoCache variable. Rather than always emit 
`$` or `c_N`, codegen checks that this name would not conflict and appends an 
incrementing suffix until it finds a unique name. 

Note that it's still possible for us to generate conflicts with global 
variables, both during RenameVariable and Codegen. The next step will be to 
avoid conflicts with globals.
This commit is contained in:
Joe Savona
2024-03-06 11:07:11 -08:00
parent 2ae0f36543
commit 8faed2af4c
8 changed files with 156 additions and 27 deletions
@@ -348,7 +348,7 @@ function* runWithEnvironment(
value: reactiveFunction,
});
renameVariables(reactiveFunction);
const uniqueIdentifiers = renameVariables(reactiveFunction);
yield log({
kind: "reactive",
name: "RenameVariables",
@@ -373,7 +373,11 @@ function* runWithEnvironment(
validatePreservedManualMemoization(reactiveFunction);
}
const ast = codegenFunction(reactiveFunction, filename).unwrap();
const ast = codegenFunction(
reactiveFunction,
uniqueIdentifiers,
filename
).unwrap();
yield log({ kind: "ast", name: "Codegen", value: ast });
/**