Skip to content

compiler: fix nested &self method lookup and global array indexing - #209

Merged
dslsdzc merged 1 commit into
developfrom
fix-nested-self-global-index
Oct 26, 2026
Merged

dslsdzc merged 1 commit into
developfrom
fix-nested-self-global-index

Conversation

@dslsdzc

@dslsdzc dslsdzc commented Oct 26, 2026

Copy link
Copy Markdown
Owner

修复

1. 嵌套 &self 方法调用段错误

&self / &mut self 的参数类型是 TYP_REF(struct),但方法的接收者查找只解 TYP_GENERIC_APPLY,没有解 TYP_REF:

impl Acc:
    fn inner(&self) -> int: return self.n
    fn outer(&self) -> int: return self.inner() + 1   # func_ni 未解析 -> 空指针调用

修法:方法表查找前先对接收者类型解一层 TYP_REF,再按原有流程解泛型应用/命名 struct。

2. 全局数组动态下标读取 0 / 写错槽

e2_load_var 的全局路径用 r11 作为 lea scratch:

lea r11, [rip+0]     # clobber r11
mov reg, [r11]

BigInt 模式下 IR_LOAD_INDEX_VAR / IR_STORE_INDEX_VAR 先把 index 解 tag 到 r11,随后加载全局数组指针时 lea r11 覆盖 index ⇒ mov dest, [r10 + r11*8] 用数组指针当索引。

修法:全局加载改用目标寄存器自身做 RIP scratch(lea reg; mov reg,[reg]),编码长度不变,不再触碰 r11。该路径同时修复 BYTE_ADD/RAW_ADD 等“先转 index 到 r11、再加载全局操作数”的同族冲突。

判据

  • test_impl.py 新增 nested_self_method(8/8)
  • test_global_seams.py 新增 B8:全局 [int;4], mut 的动态下标读/写(13/13,ELF + 解释器双绿)
  • 全套 selfhost-tests / bootstrap-tests / check 绿
  • canary 5/5 不变

- Method lookup dereferences TYP_REF receivers. `&self` parameters have
  type TYP_REF(struct), so `self.inner()` inside a method body missed the
  method table and emitted a call through an empty function pointer.
- e2_load_var global loads used r11 as the RIP scratch, clobbering an
  already-untagged index held in r11. Global array `a[i]` then indexed
  with the array pointer and read zero. Use the destination register as
  the lea scratch (`lea reg; mov reg,[reg]`), which keeps r11 intact.
- Tests: nested &self method case in test_impl; global array dynamic
  read/write case (B8) in test_global_seams.

Canary 5/5 unchanged.
@dslsdzc
dslsdzc merged commit 9f647a5 into develop Oct 26, 2026
4 checks passed
@dslsdzc
dslsdzc deleted the fix-nested-self-global-index branch October 26, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant