| 1 | // Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | // |
| 3 | // This source code is licensed under the MIT license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | //! Ensures that method call instructions have scopes such that either: |
| 7 | //! - Both the MethodCall and its property have the same scope |
| 8 | //! - OR neither has a scope |
| 9 | //! |
| 10 | //! Ported from TypeScript `src/ReactiveScopes/AlignMethodCallScopes.ts`. |
| 11 | |
| 12 | use rustc_hash::FxHashMap; |
| 13 | |
| 14 | use react_compiler_hir::environment::Environment; |
| 15 | use react_compiler_hir::{EvaluationOrder, HirFunction, IdentifierId, InstructionValue, ScopeId}; |
| 16 | use react_compiler_utils::DisjointSet; |
| 17 | |
| 18 | // ============================================================================= |
| 19 | // Public API |
| 20 | // ============================================================================= |
| 21 | |
| 22 | /// Aligns method call scopes so that either both the MethodCall result and its |
| 23 | /// property operand share the same scope, or neither has a scope. |
| 24 | /// |
| 25 | /// Corresponds to TS `alignMethodCallScopes(fn: HIRFunction): void`. |
| 26 | pub fn align_method_call_scopes(func: &mut HirFunction, env: &mut Environment) { |
| 27 | // Maps an identifier to the scope it should be assigned to (or None to remove scope) |
| 28 | let mut scope_mapping: FxHashMap<IdentifierId, Option<ScopeId>> = FxHashMap::default(); |
| 29 | let mut merged_scopes = DisjointSet::<ScopeId>::new(); |
| 30 | |
| 31 | // Phase 1: Walk instructions and collect scope relationships |
| 32 | for (_block_id, block) in &func.body.blocks { |
| 33 | for &instr_id in &block.instructions { |
| 34 | let instr = &func.instructions[instr_id.0 as usize]; |
| 35 | match &instr.value { |
| 36 | InstructionValue::MethodCall { property, .. } => { |
| 37 | let lvalue_scope = env.identifiers[instr.lvalue.identifier.0 as usize].scope; |
| 38 | let property_scope = env.identifiers[property.identifier.0 as usize].scope; |
| 39 | |
| 40 | match (lvalue_scope, property_scope) { |
| 41 | (Some(lvalue_sid), Some(property_sid)) => { |
| 42 | // Both have a scope: merge the scopes |
| 43 | merged_scopes.union(&[lvalue_sid, property_sid]); |
| 44 | } |
| 45 | (Some(lvalue_sid), None) => { |
| 46 | // Call has a scope but not the property: |
| 47 | // record that this property should be in this scope |
| 48 | scope_mapping.insert(property.identifier, Some(lvalue_sid)); |
| 49 | } |
| 50 | (None, Some(_)) => { |
| 51 | // Property has a scope but call doesn't: |
| 52 | // this property does not need a scope |
| 53 | scope_mapping.insert(property.identifier, None); |
| 54 | } |
| 55 | (None, None) => { |
| 56 | // Neither has a scope, nothing to do |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | InstructionValue::FunctionExpression { lowered_func, .. } |
| 61 | | InstructionValue::ObjectMethod { lowered_func, .. } => { |
| 62 | // Recurse into inner functions |
| 63 | let func_id = lowered_func.func; |
| 64 | let mut inner_func = std::mem::replace( |
| 65 | &mut env.functions[func_id.0 as usize], |
| 66 | react_compiler_ssa::enter_ssa::placeholder_function(), |
| 67 | ); |
| 68 | align_method_call_scopes(&mut inner_func, env); |
| 69 | env.functions[func_id.0 as usize] = inner_func; |
| 70 | } |
| 71 | _ => {} |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Phase 2: Merge scope ranges for unioned scopes. |
| 77 | // Use a FxHashMap to accumulate min/max across all scopes mapping to the same root, |
| 78 | // matching TS behavior where root.range is updated in-place during iteration. |
| 79 | let mut range_updates: FxHashMap<ScopeId, (EvaluationOrder, EvaluationOrder)> = |
| 80 | FxHashMap::default(); |
| 81 | |
| 82 | merged_scopes.for_each(|scope_id, root_id| { |
| 83 | if scope_id == root_id { |
| 84 | return; |
| 85 | } |
| 86 | let scope_range = env.scopes[scope_id.0 as usize].range.clone(); |
| 87 | let root_range = env.scopes[root_id.0 as usize].range.clone(); |
| 88 | |
| 89 | let entry = range_updates |
| 90 | .entry(root_id) |
| 91 | .or_insert_with(|| (root_range.start, root_range.end)); |
| 92 | entry.0 = EvaluationOrder(std::cmp::min(entry.0.0, scope_range.start.0)); |
| 93 | entry.1 = EvaluationOrder(std::cmp::max(entry.1.0, scope_range.end.0)); |
| 94 | }); |
| 95 | |
| 96 | // Save original scope range IDs before updating |
| 97 | let original_range_ids: FxHashMap<ScopeId, react_compiler_hir::MutableRangeId> = range_updates |
| 98 | .keys() |
| 99 | .map(|&root_id| { |
| 100 | let range_id = env.scopes[root_id.0 as usize].range.id; |
| 101 | (root_id, range_id) |
| 102 | }) |
| 103 | .collect(); |
| 104 | |
| 105 | for (root_id, (new_start, new_end)) in &range_updates { |
| 106 | env.scopes[root_id.0 as usize].range.start = *new_start; |
| 107 | env.scopes[root_id.0 as usize].range.end = *new_end; |
| 108 | } |
| 109 | |
| 110 | // Sync identifier mutable_ranges that shared the old scope range. |
| 111 | // Uses MutableRangeId for exact identity matching instead of value comparison. |
| 112 | for ident in &mut env.identifiers { |
| 113 | if let Some(scope_id) = ident.scope { |
| 114 | if let Some(&orig_range_id) = original_range_ids.get(&scope_id) { |
| 115 | if ident.mutable_range.id == orig_range_id { |
| 116 | let new_range = &env.scopes[scope_id.0 as usize].range; |
| 117 | ident.mutable_range.start = new_range.start; |
| 118 | ident.mutable_range.end = new_range.end; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Phase 3: Apply scope mappings and merged scope reassignments |
| 125 | for (_block_id, block) in &func.body.blocks { |
| 126 | for &instr_id in &block.instructions { |
| 127 | let lvalue_id = func.instructions[instr_id.0 as usize].lvalue.identifier; |
| 128 | |
| 129 | if let Some(mapped_scope) = scope_mapping.get(&lvalue_id) { |
| 130 | env.identifiers[lvalue_id.0 as usize].scope = *mapped_scope; |
| 131 | } else if let Some(current_scope) = env.identifiers[lvalue_id.0 as usize].scope { |
| 132 | // TS: mergedScopes.find() returns null if not in the set |
| 133 | if let Some(merged) = merged_scopes.find_opt(current_scope) { |
| 134 | env.identifiers[lvalue_id.0 as usize].scope = Some(merged); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |