refactor(cleanup): Phase 285A1 Post-Implementation Cleanup

Code quality improvements after Phase 285A1 implementation:

**Task 1: Dead Code Cleanup**
- Removed unnecessary #[allow(dead_code)] from emit_weak_load()
- Function is actively used in weak_to_strong() method handler

**Task 2: Unused Import Removal (cargo fix)**
- edgecfg/api/mod.rs: Removed seq, if_, loop_, cleanup, verify_frag_invariants
- pattern3.rs: Removed BinaryOperator
- pattern2/api/mod.rs: Removed PromoteStepResult
- jump.rs: Removed EffectMask, Span
- Result: 6 unused imports eliminated

**Task 3: Deprecated Pattern Removal**
- Fixed 4 PlanKind::LoopWithPost deprecated warnings
- Updated to Phase 142 P0 architecture (statement-level normalization)
- Files: normalized_shadow_suffix_router_box.rs, routing.rs,
  execute_box.rs, plan.rs
- Removed 2 deprecated tests: test_loop_with_post_*

**Task 4: WeakFieldValidatorBox Boxification**
- Extracted weak field validation logic into dedicated Box
- New file: src/mir/builder/weak_field_validator.rs (147 lines)
- fields.rs: 277 → 237 lines (-40 lines, -14.4%)
- Added 5 unit tests for validation logic
- Follows Phase 33 boxification principles (single responsibility,
  testability, reusability)

**Metrics**:
- Code reduction: -40 lines in fields.rs
- Test coverage: +5 unit tests
- Warnings fixed: 4 deprecated warnings
- Imports cleaned: 6 unused imports

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-24 03:44:56 +09:00
parent cc8b27a1aa
commit 2da730de35
13 changed files with 365 additions and 87 deletions

View File

@ -120,13 +120,14 @@ impl super::MirBuilder {
if let Some(weak_set) = self.comp_ctx.weak_fields_by_box.get(&class_name) {
if weak_set.contains(&field) {
// Phase 285A1: Read weak field returns WeakRef (no auto-upgrade)
// field_val is the result of getField, which we treat as WeakRef
// Delegated to WeakFieldValidatorBox
let dst = field_val; // The load result is already our return value
// Phase 285A1: Mark the result as WeakRef type
self.type_ctx
.value_types
.insert(dst, crate::mir::types::MirType::WeakRef);
// Phase 285A1: Annotate result as WeakRef type
super::weak_field_validator::WeakFieldValidatorBox::annotate_read_result(
&mut self.type_ctx,
dst,
);
let _ = self.emit_barrier_read(dst);
return Ok(dst); // Return WeakRef directly (no WeakLoad)
@ -154,6 +155,7 @@ impl super::MirBuilder {
value_result = self.local_arg(value_result);
// Phase 285A1: If field is weak, enforce type contract (3 allowed cases)
// Delegated to WeakFieldValidatorBox
if let Some(class_name) = self
.type_ctx
.value_origin_newbox
@ -162,8 +164,13 @@ impl super::MirBuilder {
{
if let Some(weak_set) = self.comp_ctx.weak_fields_by_box.get(&class_name) {
if weak_set.contains(&field) {
// Phase 285A1: Strict type check (no automatic conversion)
self.check_weak_field_assignment(&class_name, &field, value_result)?;
// Phase 285A1: Strict type check (delegated to validator)
let value_type = self.type_ctx.value_types.get(&value_result);
super::weak_field_validator::WeakFieldValidatorBox::validate_assignment(
value_type,
&class_name,
&field,
)?;
}
}
}
@ -228,50 +235,4 @@ impl super::MirBuilder {
Ok(value_result)
}
/// Phase 285A1: Enforce weak field assignment contract
///
/// Allowed assignments:
/// 1. WeakRef (from weak() or weak field read)
/// 2. Void (clear operation)
///
/// Forbidden (Fail-Fast):
/// - BoxRef without weak()
/// - Primitives
/// - Unknown/untracked values
fn check_weak_field_assignment(
&mut self,
box_name: &str,
field_name: &str,
value_id: ValueId,
) -> Result<(), String> {
// Get value type
let value_type = self.type_ctx.value_types.get(&value_id);
match value_type {
// Case 1 & 2: WeakRef allowed
Some(crate::mir::types::MirType::WeakRef) => Ok(()),
// Case 3: Void allowed (clear)
Some(crate::mir::types::MirType::Void) => Ok(()),
// Forbidden: None/Unknown (型追跡漏れ防止)
None => Err(format!(
"Cannot assign untracked value to weak field '{}.{}'. Use weak(...) or Void explicitly.",
box_name, field_name
)),
// Forbidden: BoxRef
Some(crate::mir::types::MirType::Box(box_type)) => Err(format!(
"Cannot assign Box ({}) to weak field '{}.{}'. Use weak(...) to create weak reference: me.{} = weak(value)",
box_type, box_name, field_name, field_name
)),
// Forbidden: Primitives and others
Some(other_type) => Err(format!(
"Cannot assign {:?} to weak field '{}.{}'. Weak fields require WeakRef type. Use weak(...) or Void.",
other_type, box_name, field_name
)),
}
}
}