Files
Viper_RegressionTesting/NewLifetimeUseAfterFreeTest/App/main.vp
T
TermiNexus aed57de44c test(lifetime): 新增 willFree 正/负向工程并注册到回归脚本
NewLifetimeWillFreeTest(二次 free)、NewLifetimeUseAfterFreeTest(释放后使用)为负向;NewLifetimeWillFreeOkTest 验证合法用法与重赋值清除失效态。同步更新 SequentialRegressionTest / regression_test / run_lifetime_negative_tests 与 README。
2026-10-08 21:28:55 +08:00

25 lines
835 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
# =============================================================================
# 负向工程(**应当编译失败**,对照工程:NewLifetimeWillFreeOkTest)
#
# P2 willFree 标注驱动:`@life(willFree(p))` 标注的调用返回后,实参变量即失效;
# 其后对它做任何**读取**(取值、参与表达式、作为实参传递)都必须在编译期被拒。
#
# 本工程:Use(p) 在 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) # ← 必须被拒:p 已失效(使用已释放变量)
return 0