Home / File/ ValidateNoFreezingKnownMutableFunctions.ts — react Source File

ValidateNoFreezingKnownMutableFunctions.ts — react Source File

Architecture documentation for ValidateNoFreezingKnownMutableFunctions.ts, a typescript file in the react codebase. 11 imports, 1 dependents.

File typescript BabelCompiler Validation 11 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb["ValidateNoFreezingKnownMutableFunctions.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> e96f281e_f381_272d_2359_3e6a091c9a1d
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 0423f759_97e0_9101_4634_ed555abc5ca9
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 41232a25_deb6_6e83_05a8_ae9f961656f7
  2f00e901_2271_5b46_4d72_3fa77ff15e31["AliasingEffects.ts"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 2f00e901_2271_5b46_4d72_3fa77ff15e31
  9f54f993_76c3_b507_621f_ba4f24e13eda["AliasingEffect"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 9f54f993_76c3_b507_621f_ba4f24e13eda
  494e3425_0b47_293a_1ea4_d4670b0fc0e7["Result.ts"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 494e3425_0b47_293a_1ea4_d4670b0fc0e7
  7aace723_0ee1_cff5_b263_aec8e06dd79e["Result"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 7aace723_0ee1_cff5_b263_aec8e06dd79e
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> 8ecac9ec_c10d_6d28_acad_1f65ff39ebfb
  style 8ecac9ec_c10d_6d28_acad_1f65ff39ebfb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {CompilerDiagnostic, CompilerError, Effect} from '..';
import {ErrorCategory} from '../CompilerError';
import {
  HIRFunction,
  IdentifierId,
  isRefOrRefLikeMutableType,
  Place,
} from '../HIR';
import {
  eachInstructionValueOperand,
  eachTerminalOperand,
} from '../HIR/visitors';
import {AliasingEffect} from '../Inference/AliasingEffects';
import {Result} from '../Utils/Result';

/**
 * Validates that functions with known mutations (ie due to types) cannot be passed
 * where a frozen value is expected. Example:
 *
 * ```
 * function Component() {
 *   const cache = new Map();
 *   const onClick = () => {
 *     cache.set(...);
 *   }
 *   useHook(onClick); // ERROR: cannot pass a mutable value
 *   return <Foo onClick={onClick} /> // ERROR: cannot pass a mutable value
 * }
 * ```
 *
 * Because `onClick` function mutates `cache` when called, `onClick` is equivalent to a mutable
 * variables. But unlike other mutables values like an array, the receiver of the function has
 * no way to avoid mutation — for example, a function can receive an array and choose not to mutate
 * it, but there's no way to know that a function is mutable and avoid calling it.
 *
 * This pass detects functions with *known* mutations (Store or Mutate, not ConditionallyMutate)
 * that are passed where a frozen value is expected and rejects them.
 */
export function validateNoFreezingKnownMutableFunctions(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const errors = new CompilerError();
  const contextMutationEffects: Map<
    IdentifierId,
    Extract<AliasingEffect, {kind: 'Mutate'} | {kind: 'MutateTransitive'}>
  > = new Map();

  function visitOperand(operand: Place): void {
    if (operand.effect === Effect.Freeze) {
      const effect = contextMutationEffects.get(operand.identifier.id);
      if (effect != null) {
        const place = effect.value;
        const variable =
// ... (107 more lines)

Domain

Subdomains

Frequently Asked Questions

What does ValidateNoFreezingKnownMutableFunctions.ts do?
ValidateNoFreezingKnownMutableFunctions.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in ValidateNoFreezingKnownMutableFunctions.ts?
ValidateNoFreezingKnownMutableFunctions.ts defines 1 function(s): validateNoFreezingKnownMutableFunctions.
What does ValidateNoFreezingKnownMutableFunctions.ts depend on?
ValidateNoFreezingKnownMutableFunctions.ts imports 11 module(s): .., AliasingEffect, AliasingEffects.ts, CompilerError.ts, ErrorCategory, Result, Result.ts, eachInstructionValueOperand, and 3 more.
What files import ValidateNoFreezingKnownMutableFunctions.ts?
ValidateNoFreezingKnownMutableFunctions.ts is imported by 1 file(s): Pipeline.ts.
Where is ValidateNoFreezingKnownMutableFunctions.ts in the architecture?
ValidateNoFreezingKnownMutableFunctions.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/Validation).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free