Home / File/ AssertScopeInstructionsWithinScope.ts — react Source File

AssertScopeInstructionsWithinScope.ts — react Source File

Architecture documentation for AssertScopeInstructionsWithinScope.ts, a typescript file in the react codebase. 7 imports, 0 dependents.

File typescript BabelCompiler Validation 7 imports 1 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  01cf585d_2384_4412_6dde_8416d27772a6["AssertScopeInstructionsWithinScope.ts"]
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  01cf585d_2384_4412_6dde_8416d27772a6 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  18a78965_f593_105b_e5e8_07001321c2ec["HIR.ts"]
  01cf585d_2384_4412_6dde_8416d27772a6 --> 18a78965_f593_105b_e5e8_07001321c2ec
  7107ba22_4065_0956_1ec3_286f637d0d22["getPlaceScope"]
  01cf585d_2384_4412_6dde_8416d27772a6 --> 7107ba22_4065_0956_1ec3_286f637d0d22
  21609915_b03a_fd75_b58a_4cb86ef9315b["visitors.ts"]
  01cf585d_2384_4412_6dde_8416d27772a6 --> 21609915_b03a_fd75_b58a_4cb86ef9315b
  171a5d22_bb6b_1c99_05a4_6ad897438a35["ReactiveFunctionVisitor"]
  01cf585d_2384_4412_6dde_8416d27772a6 --> 171a5d22_bb6b_1c99_05a4_6ad897438a35
  9dc3e5f8_0649_d981_e520_38c0ab672d18["."]
  01cf585d_2384_4412_6dde_8416d27772a6 --> 9dc3e5f8_0649_d981_e520_38c0ab672d18
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  01cf585d_2384_4412_6dde_8416d27772a6 --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  style 01cf585d_2384_4412_6dde_8416d27772a6 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 {visitReactiveFunction} from '.';
import {CompilerError} from '..';
import {
  InstructionId,
  Place,
  ReactiveFunction,
  ReactiveScopeBlock,
  ScopeId,
} from '../HIR';
import {getPlaceScope} from '../HIR/HIR';
import {ReactiveFunctionVisitor} from './visitors';

/*
 * Internal validation pass that checks all the instructions involved in creating
 * values for a given scope are within the corresponding ReactiveScopeBlock. Errors
 * in HIR/ReactiveFunction structure and alias analysis could theoretically create
 * a structure such as:
 *
 * Function
 *    LabelTerminal
 *     Instruction in scope 0
 *    Instruction in scope 0
 *
 * Because ReactiveScopeBlocks are closed when their surrounding block ends, this
 * structure would create reactive scopes as follows:
 *
 * Function
 *    LabelTerminal
 *      ReactiveScopeBlock scope=0
 *        Instruction in scope 0
 *    Instruction in scope 0
 *
 * This pass asserts we didn't accidentally end up with such a structure, as a guard
 * against compiler coding mistakes in earlier passes.
 */
export function assertScopeInstructionsWithinScopes(
  fn: ReactiveFunction,
): void {
  const existingScopes = new Set<ScopeId>();
  visitReactiveFunction(fn, new FindAllScopesVisitor(), existingScopes);
  visitReactiveFunction(
    fn,
    new CheckInstructionsAgainstScopesVisitor(),
    existingScopes,
  );
}

class FindAllScopesVisitor extends ReactiveFunctionVisitor<Set<ScopeId>> {
  override visitScope(block: ReactiveScopeBlock, state: Set<ScopeId>): void {
    this.traverseScope(block, state);
    state.add(block.scope.id);
  }
}

class CheckInstructionsAgainstScopesVisitor extends ReactiveFunctionVisitor<
  Set<ScopeId>
> {
  activeScopes: Set<ScopeId> = new Set();

  override visitPlace(
    id: InstructionId,
    place: Place,
    state: Set<ScopeId>,
  ): void {
    const scope = getPlaceScope(id, place);
    if (
      scope !== null &&
      // is there a scope for this at all, or did we end up pruning this scope?
      state.has(scope.id) &&
      /*
       * if the scope exists somewhere, it must be active or else this is a straggler
       * instruction
       */
      !this.activeScopes.has(scope.id)
    ) {
      CompilerError.invariant(false, {
        reason:
          'Encountered an instruction that should be part of a scope, but where that scope has already completed',
        description: `Instruction [${id}] is part of scope @${scope.id}, but that scope has already completed`,
        loc: place.loc,
      });
    }
  }

  override visitScope(block: ReactiveScopeBlock, state: Set<ScopeId>): void {
    this.activeScopes.add(block.scope.id);
    this.traverseScope(block, state);
    this.activeScopes.delete(block.scope.id);
  }
}

Domain

Subdomains

Frequently Asked Questions

What does AssertScopeInstructionsWithinScope.ts do?
AssertScopeInstructionsWithinScope.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 AssertScopeInstructionsWithinScope.ts?
AssertScopeInstructionsWithinScope.ts defines 1 function(s): assertScopeInstructionsWithinScopes.
What does AssertScopeInstructionsWithinScope.ts depend on?
AssertScopeInstructionsWithinScope.ts imports 7 module(s): ., .., HIR.ts, ReactiveFunctionVisitor, getPlaceScope, index.ts, visitors.ts.
Where is AssertScopeInstructionsWithinScope.ts in the architecture?
AssertScopeInstructionsWithinScope.ts is located at compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/AssertScopeInstructionsWithinScope.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes).

Analyze Your Own Codebase

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

Try Supermodel Free