-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
[compiler][be] Clean up compilation skipping logic in Program #30642
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { | |
ErrorSeverity, | ||
} from '../CompilerError'; | ||
import { | ||
EnvironmentConfig, | ||
ExternalFunction, | ||
ReactFunctionType, | ||
parseEnvironmentConfig, | ||
|
@@ -276,41 +277,27 @@ export function compileProgram( | |
program: NodePath<t.Program>, | ||
pass: CompilerPass, | ||
): void { | ||
if (pass.opts.sources) { | ||
if (pass.filename === null) { | ||
const error = new CompilerError(); | ||
error.pushErrorDetail( | ||
new CompilerErrorDetail({ | ||
reason: `Expected a filename but found none.`, | ||
description: | ||
"When the 'sources' config options is specified, the React compiler will only compile files with a name", | ||
severity: ErrorSeverity.InvalidConfig, | ||
loc: null, | ||
}), | ||
); | ||
handleError(error, pass, null); | ||
return; | ||
} | ||
|
||
if (!isFilePartOfSources(pass.opts.sources, pass.filename)) { | ||
return; | ||
} | ||
} | ||
|
||
// Top level "use no forget", skip this file entirely | ||
if ( | ||
findDirectiveDisablingMemoization(program.node.directives, pass.opts) != | ||
null | ||
) { | ||
if (shouldSkipCompilation(program, pass)) { | ||
return; | ||
} | ||
|
||
const environment = parseEnvironmentConfig(pass.opts.environment ?? {}); | ||
/* | ||
* TODO(lauren): Remove pass.opts.environment nullcheck once PluginOptions | ||
* is validated | ||
*/ | ||
const environmentResult = parseEnvironmentConfig(pass.opts.environment ?? {}); | ||
if (environmentResult.isErr()) { | ||
CompilerError.throwInvalidConfig({ | ||
reason: | ||
'Error in validating environment config. This is an advanced setting and not meant to be used directly', | ||
description: environmentResult.unwrapErr().toString(), | ||
suggestions: null, | ||
loc: null, | ||
}); | ||
} | ||
const environment = environmentResult.unwrap(); | ||
const useMemoCacheIdentifier = program.scope.generateUidIdentifier('c'); | ||
const moduleName = pass.opts.runtimeModule ?? 'react/compiler-runtime'; | ||
if (hasMemoCacheFunctionImport(program, moduleName)) { | ||
return; | ||
} | ||
|
||
/* | ||
* Record lint errors and critical errors as depending on Forget's config, | ||
|
@@ -332,7 +319,7 @@ export function compileProgram( | |
const compiledFns: Array<CompileResult> = []; | ||
|
||
const traverseFunction = (fn: BabelFn, pass: CompilerPass): void => { | ||
const fnType = getReactFunctionType(fn, pass); | ||
const fnType = getReactFunctionType(fn, pass, environment); | ||
if (fnType === null || ALREADY_COMPILED.has(fn.node)) { | ||
return; | ||
} | ||
|
@@ -403,24 +390,9 @@ export function compileProgram( | |
|
||
let compiledFn: CodegenFunction; | ||
try { | ||
/* | ||
* TODO(lauren): Remove pass.opts.environment nullcheck once PluginOptions | ||
* is validated | ||
*/ | ||
if (environment.isErr()) { | ||
CompilerError.throwInvalidConfig({ | ||
reason: | ||
'Error in validating environment config. This is an advanced setting and not meant to be used directly', | ||
description: environment.unwrapErr().toString(), | ||
suggestions: null, | ||
loc: null, | ||
}); | ||
} | ||
const config = environment.unwrap(); | ||
|
||
compiledFn = compileFn( | ||
fn, | ||
config, | ||
environment, | ||
fnType, | ||
useMemoCacheIdentifier.name, | ||
pass.opts.logger, | ||
|
@@ -514,43 +486,29 @@ export function compileProgram( | |
externalFunctions.push(gating); | ||
} | ||
|
||
const lowerContextAccess = pass.opts.environment?.lowerContextAccess; | ||
const lowerContextAccess = environment.lowerContextAccess; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we're able to rely on the parsed / validated EnvironmentConfig, which comes with default values (instead of using a |
||
if (lowerContextAccess && hasLoweredContextAccess) { | ||
externalFunctions.push(tryParseExternalFunction(lowerContextAccess)); | ||
externalFunctions.push(lowerContextAccess); | ||
} | ||
|
||
const enableEmitInstrumentForget = | ||
pass.opts.environment?.enableEmitInstrumentForget; | ||
const enableEmitInstrumentForget = environment.enableEmitInstrumentForget; | ||
if (enableEmitInstrumentForget != null) { | ||
externalFunctions.push( | ||
tryParseExternalFunction(enableEmitInstrumentForget.fn), | ||
); | ||
externalFunctions.push(enableEmitInstrumentForget.fn); | ||
if (enableEmitInstrumentForget.gating != null) { | ||
externalFunctions.push( | ||
tryParseExternalFunction(enableEmitInstrumentForget.gating), | ||
); | ||
externalFunctions.push(enableEmitInstrumentForget.gating); | ||
} | ||
} | ||
|
||
if (pass.opts.environment?.enableEmitFreeze != null) { | ||
const enableEmitFreeze = tryParseExternalFunction( | ||
pass.opts.environment.enableEmitFreeze, | ||
); | ||
externalFunctions.push(enableEmitFreeze); | ||
if (environment.enableEmitFreeze != null) { | ||
externalFunctions.push(environment.enableEmitFreeze); | ||
} | ||
|
||
if (pass.opts.environment?.enableEmitHookGuards != null) { | ||
const enableEmitHookGuards = tryParseExternalFunction( | ||
pass.opts.environment.enableEmitHookGuards, | ||
); | ||
externalFunctions.push(enableEmitHookGuards); | ||
if (environment.enableEmitHookGuards != null) { | ||
externalFunctions.push(environment.enableEmitHookGuards); | ||
} | ||
|
||
if (pass.opts.environment?.enableChangeDetectionForDebugging != null) { | ||
const enableChangeDetectionForDebugging = tryParseExternalFunction( | ||
pass.opts.environment.enableChangeDetectionForDebugging, | ||
); | ||
externalFunctions.push(enableChangeDetectionForDebugging); | ||
if (environment.enableChangeDetectionForDebugging != null) { | ||
externalFunctions.push(environment.enableChangeDetectionForDebugging); | ||
} | ||
} catch (err) { | ||
handleError(err, pass, null); | ||
|
@@ -593,11 +551,65 @@ export function compileProgram( | |
} | ||
} | ||
|
||
function shouldSkipCompilation( | ||
program: NodePath<t.Program>, | ||
pass: CompilerPass, | ||
): boolean { | ||
if (pass.opts.sources) { | ||
if (pass.filename === null) { | ||
const error = new CompilerError(); | ||
error.pushErrorDetail( | ||
new CompilerErrorDetail({ | ||
reason: `Expected a filename but found none.`, | ||
description: | ||
"When the 'sources' config options is specified, the React compiler will only compile files with a name", | ||
severity: ErrorSeverity.InvalidConfig, | ||
loc: null, | ||
}), | ||
); | ||
handleError(error, pass, null); | ||
return true; | ||
} | ||
|
||
if (!isFilePartOfSources(pass.opts.sources, pass.filename)) { | ||
return true; | ||
} | ||
} | ||
|
||
// Top level "use no forget", skip this file entirely | ||
const useNoForget = findDirectiveDisablingMemoization( | ||
program.node.directives, | ||
pass.opts, | ||
); | ||
if (useNoForget != null) { | ||
pass.opts.logger?.logEvent(pass.filename, { | ||
kind: 'CompileError', | ||
fnLoc: null, | ||
detail: { | ||
severity: ErrorSeverity.Todo, | ||
reason: 'Skipped due to "use no forget" directive.', | ||
loc: useNoForget.loc ?? null, | ||
suggestions: null, | ||
}, | ||
}); | ||
return true; | ||
} | ||
const moduleName = pass.opts.runtimeModule ?? 'react/compiler-runtime'; | ||
if (hasMemoCacheFunctionImport(program, moduleName)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function getReactFunctionType( | ||
fn: BabelFn, | ||
pass: CompilerPass, | ||
/** | ||
* TODO(mofeiZ): remove once we validate PluginOptions with Zod | ||
*/ | ||
environment: EnvironmentConfig, | ||
): ReactFunctionType | null { | ||
const hookPattern = pass.opts.environment?.hookPattern ?? null; | ||
const hookPattern = environment.hookPattern; | ||
if (fn.node.body.type === 'BlockStatement') { | ||
// Opt-outs disable compilation regardless of mode | ||
const useNoForget = findDirectiveDisablingMemoization( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, we waited until compiling a function to validate environment configs. Let's just do this unconditionally -- this should be a fatal error and hopefully never happens in prod