Files
TermiNexus 66ebd75dec test(lifetime): 补齐 A2 @life(ret(...)) 的正/负向回归工程
新增 3 个工程(均登记进 regression_test.py / SequentialRegressionTest.py /
run_lifetime_negative_tests.py 的 escape/负向清单):
- NewLifetimeRetOkTest(正向):ret(heap)/ret(static)/ret(borrows=none) 的结果
  可存进堆对象字段;ret(frame)/ret(borrows=x) 的结果只在本帧内使用;
  同模块契约表与跨模块 .vpi(escr=) 两条取用路径都覆盖 → 编译通过 + 退出码 0。
- NewLifetimeRetEscapeTest(负向):同模块 @life(ret(frame)) 的返回值写进堆对象
  字段 → LifetimeError(隔离性:FrameRet 摘要为 0,唯一报错源是 ret 契约)。
- NewLifetimeRetXmodTest(负向):跨模块 @life(noescape(x), ret(borrows=x)) 的结果
  写进堆对象字段 → LifetimeError(契约经对方 .vpi 的 escr= 传来;noescape 清掉了
  实参逃逸路径,判据收敛到 ret 契约)。
README.md 的 escape 测试表与负向清单同步。
2026-10-10 09:03:05 +08:00

44 lines
1.6 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from stdint import *
import retdep2
# =============================================================================
# 负向工程(**应当编译失败**):A2 **跨模块** `@life(ret(...))` —— 契约经 .vpi 的 `escr=`
#
# 被调者定义在 retdep2.vp,契约(含 ret_kind)只能经对方 .vpi 行尾的 `escr=` 传来;
# 本文件里它的本地契约表必然为空 → 命中跨模块回退路径。
#
# 本工程验:跨模块声明为 `borrows=x` 的返回值(生命期 = 实参 = 本帧)被写进长命存储
# 时必须被拒。期望报错:LifetimeError(`a longer-lived field/element`)。
#
# 隔离性:IdentBorrow 带 `@life(noescape(x))`,"实参逃逸"那条路径被清掉,
# 故唯一的报错来源是 .vpi 的 `escr=` → ret(borrows=x) → 结果 LIFE_FRAME。
# =============================================================================
novirtual class Holder:
p: int*
# 唯一用例:跨模块 ret(borrows=x) 的结果存进**堆对象**字段 → 左值长命、右值本帧
def CaseBorrow() -> int:
h: Holder* = new heap hlife Holder()
l: int* = new stack int
h.p = retdep2.IdentBorrow(l) # ❌ 期望 LifetimeError
return 0
export def main() -> int:
CaseBorrow()
return 0
# -----------------------------------------------------------------------------
# 对照用例(把 main 里的 CaseBorrow() 换成本函数即可,一次只开一条):
#
# def CaseFrame() -> int:
# h: Holder* = new heap hlife Holder()
# h.p = retdep2.FrameRet() # ❌ 期望 LifetimeError
# return 0
# -----------------------------------------------------------------------------