fix(tpc): 局部 AnnAssign 反向重建不再窄化「基类指针」声明为派生类
【类别 a 编译器缺陷】局部带注解赋值的「反向重建」缺少方向判定,
把源码显式声明的**基类指针**落地成**派生类指针**,
再用新收紧的指针隐式转换判据翻译后续赋值时就必然失败。
现象:
翻译 includes/ast/parser.vp 报
「指针隐式转换已禁止(非基本类型(结构体/类/联合)):不能把指针类型
%"….AST"* 隐式转为 %"….Name"*」,
出错语句 `_parse_atom` 内 `node = _new_const_str(...)`(parser.vp:1921 附近)。
根因链(全部可取证):
1. `node` 在 _parse_atom 内首次声明于
`node: rawptr[AST] = Name(ps.pool, name_str, ASTCtx.Load)`(parser.vp:1899),
注解写明的被指类是 **AST(基类)**,RHS 是 **Name(派生类)指针**;
2. HandlesAnnAssign._HandleAnnAssignLlvmInner 中「以 RHS 被指类型反向重建
alloca」的代码块(初版快照即存在,注释自述本意是「注解退化成裸基类 / i8*,
RHS 构造出精确特化 ⇒ 用 RHS 反向升级」)**无条件**执行:
if isinstance(VarType, ir.PointerType) and VarType.pointee is not ST ...:
var = Gen._allocaEntry(ir.PointerType(pointee), name=VarName)
⇒ node 的栈槽被落成 `Name*` 而非注解的 `AST*`;
(既有仅有的方向判据 `_rb_is_bare_generic`,commit 0ffaadc9,只拦「RHS 是
裸泛型基类」这一个方向,拦不住「RHS 是注解被指类的派生类」)
3. 旧构建产物实测确认槽类型确为派生类指针:
Test/AstTest/.tpv_build/temp/05/056a6a1271f871d2.ll 的
`@"…._parse_atom"` 内 `%24 = alloca %"8f3ee13abe0c5527.Name"*`,
且 `_parse_fstring` / `_new_const_str`(均返回 AST*)的返回值后面紧跟
`bitcast …AST* → …Name*` 再 store —— 即 AST*→Name* 的**下转**此前是静默
bitcast;2026-09-28 指针隐式转换收紧(结构体只允许上转)后,同一处变成
fail-fast,暴露出上层这个窄化缺陷。收紧本身正确,缺陷在窄化。
修法(与既有 `_rb_is_bare_generic` 对称的「方向判定(其二)」,非兜底):
· 新增注解侧被指类快照 `_ann_pointee`:优先取已解析出的 `VarType.pointee`;
注解被 TypeMerge 折叠成 i8*(rawptr[X] / t.CPtr[X])时取同一函数上方
635-684 已恢复进 `var_struct_class` 的 X(那正是注解写明的被指类);
· 仅当 `_IsInheritanceUpcastPointee(RHS 被指类, 注解被指类)` 为真
(即注解就是合法的「基类指针」声明、RHS 只是继承上转)时**跳过重建**,
保留注解类型,杜绝「按 RHS 窄化声明类型」。
判据只拦这一个确定的方向错误:注解退化成 i8*/裸占位(被指类非 RHS 的基类)
时 `_rb_is_upcast` 为假,泛型特化升级路径(commit 0ffaadc9 修的现场)不受影响。
影响面:本处是 TPC 内唯一的「以 RHS 重建 alloca」点(全仓 grep 该调用仅此一处)。
窄化取消后,槽类型回归注解声明;后续赋值/取值按正常隐式转换判定
(派生→基类为上转,放行),不再出现「先被悄悄窄化、再被判下转」的自相矛盾。
1 file changed, 31 insertions(+), 1 deletion(-)
This commit is contained in:
1 parent
ac0d0f2cc8
commit
d3537c7b3f
1 file changed
+31
-1
@@ -848,6 +848,21 @@ class AnnAssignHandle(BaseHandle):
|
||||
_ann_base_v = _ann_base_v.value
|
||||
if isinstance(_ann_base_v, ast.Name):
|
||||
_ann_gen_base = _ann_base_v.id
|
||||
# 「反向重建」方向判定(其二,见下方 _rb_is_upcast)所需的**注解侧
|
||||
# 被指类**:注解被 TypeMerge 折叠成 i8*(rawptr[X] / t.CPtr[X],见
|
||||
# 上方 635-684)时 VarType.pointee 不是结构体,此时用该处恢复进
|
||||
# var_struct_class 的 X —— 那正是注解写明的被指类。
|
||||
_ann_pointee: ir.Type | None = None
|
||||
if isinstance(VarType, ir.PointerType):
|
||||
_ap: ir.Type = VarType.pointee
|
||||
if isinstance(_ap, ir.IdentifiedStructType):
|
||||
_ann_pointee = _ap
|
||||
else:
|
||||
_ask: str | None = Gen.var_struct_class.get(VarName)
|
||||
_ann_st: ir.Type | None = Gen.structs.get(_ask) if _ask else None
|
||||
if isinstance(_ann_st, ir.PointerType):
|
||||
_ann_st = _ann_st.pointee
|
||||
_ann_pointee = _ann_st
|
||||
for CN, ST in Gen.structs.items():
|
||||
# 泛型占位类型(T/U/V,如 GSList[T].Head 的元素类型)opaque 无 body,
|
||||
# 不参与变量类型重建/var_struct_class 设置:跟随 T* 会污染注解声明的
|
||||
@@ -905,7 +920,22 @@ class AnnAssignHandle(BaseHandle):
|
||||
and '[' not in _cn_short
|
||||
and _cn_short == _ann_gen_base
|
||||
)
|
||||
if not _rb_is_bare_generic:
|
||||
# 方向判定(其二):注解写明的被指类是 RHS 被指类的
|
||||
# **基类**时,注解就是合法的「基类指针」声明,RHS 只是
|
||||
# 继承上转 —— 以 RHS 重建 alloca 会把声明类型**窄化**成
|
||||
# 派生类,使同一变量随后赋基类/兄弟类的值被指针隐式
|
||||
# 转换 fail-fast 拦下(includes/ast/parser.vp
|
||||
# `node: rawptr[AST] = Name(...)` 之后
|
||||
# `node = _new_const_str(...)` 现场根因)。
|
||||
# 判据只拦「上转被窄化」这一个确定的方向错误:注解退化
|
||||
# 成 i8*/裸占位(_ann_pointee 非其基类)时不受影响。
|
||||
_rb_is_upcast: bool = (
|
||||
_ann_pointee is not None
|
||||
and _ann_pointee is not ST
|
||||
and _ann_pointee != ST
|
||||
and Gen._IsInheritanceUpcastPointee(pointee, _ann_pointee)
|
||||
)
|
||||
if not _rb_is_bare_generic and not _rb_is_upcast:
|
||||
var = Gen._allocaEntry(ir.PointerType(pointee), name=VarName)
|
||||
VarType = ir.PointerType(pointee)
|
||||
break
|
||||
|
||||
Reference in new issue
Block a user