Files
Viper_RegressionTesting/NewLifetimeWillFreeAddrTest/App/main.vp
T
TermiNexus 5fa5a9f37c test(lifetime): 补 P3 注解形态 + P2 方法级/取地址的自动工程
- 新增 NewLifetimeAnnFieldEscapeTest:h.p: int* = &local → LifetimeError(P3 注解形态)

- 新增 NewLifetimeWillFreeMethodTest:r.Release(p) 后再用 p → LifetimeError(方法级标注,形参位含 self 偏移)

- 新增 NewLifetimeWillFreeAddrTest:stdlib.free(p) 后 &p → LifetimeError(取地址路径)

- NewLifetimeTest.CaseAnnFieldStore:p.x: int = 42 必须真的写进字段(正向)

- 注册进 SequentialRegressionTest / regression_test / run_lifetime_negative_tests + README
2026-10-09 14:55:52 +08:00

27 lines
907 B
Plaintext
Raw 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 stdlib
# =============================================================================
# 负向工程(**应当编译失败**)
#
# P2 willFree —— **取地址**路径。变量被 @life(willFree(...)) 标注的调用消费后只是"值失效",
# 但 `&p` 同样是把指向已释放对象的指针交出去(后续解引用即 use-after-free),
# 必须与**读取**同一口径拒绝。
# 检查点:ExprGenOps 的 AddrOf 分支(`_FreedCheck`),与 ExprGenName 的读取路径同源。
#
# 本工程:`free(p)` 之后取 `&p`。
# 期望报错:LifetimeError。
# =============================================================================
def Use(x: rawptr*) -> int:
return 0
export def main() -> int:
p: rawptr = stdlib.malloc(8)
stdlib.free(p)
Use(&p) # ← 必须被拒:对已失效变量取地址
return 0