fix(len): list 特化直接读取 __count__ 字段作为长度
泛型特化方法未生成时,len(list[...]) 按结构体偏移直接读取 __count__,不再误报没有 __len__。
This commit is contained in:
1 parent
944f5cafbb
commit
4346e3fb27
2 files changed
+16
No files matched your search
@@ -737,6 +737,14 @@ class ExprBuiltinHandle(BaseHandle):
|
||||
result = self.Trans.ExprUtils._try_operator_overLoad(_matched_cn, '__len__', val, Gen)
|
||||
if result is not None:
|
||||
return result
|
||||
if _matched_cn.startswith('list['):
|
||||
_count_off: int | None = Gen._get_member_offset('__count__', _matched_cn, pointee)
|
||||
if _count_off is not None:
|
||||
_count_ptr = Gen.builder.gep(
|
||||
val,
|
||||
[ir.Constant(ir.IntType(32), 0), ir.Constant(ir.IntType(32), _count_off)],
|
||||
name='list_count_ptr')
|
||||
return Gen._load(_count_ptr, name='list_count')
|
||||
self._FailFastLenUnsupported(arg_node, Gen, val, f"len() 参数类型 '{_matched_cn}' 没有 __len__ 方法")
|
||||
else:
|
||||
# 未在 Gen.structs 登记的 struct(opaque/匿名)→ fail-fast。
|
||||
|
||||
@@ -4120,6 +4120,14 @@ class ExprCallHandle(BaseHandle):
|
||||
result = self.Trans.ExprUtils._try_operator_overLoad(_matched_cn, '__len__', val, Gen)
|
||||
if result is not None:
|
||||
return result
|
||||
if _matched_cn.startswith('list['):
|
||||
_count_off: int | None = Gen._get_member_offset('__count__', _matched_cn, pointee)
|
||||
if _count_off is not None:
|
||||
_count_ptr = Gen.builder.gep(
|
||||
val,
|
||||
[ir.Constant(ir.IntType(32), 0), ir.Constant(ir.IntType(32), _count_off)],
|
||||
name='list_count_ptr')
|
||||
return Gen._load(_count_ptr, name='list_count')
|
||||
self._FailFastLenUnsupported(arg_node, Gen, val, f"len() 参数类型 '{_matched_cn}' 没有 __len__ 方法")
|
||||
else:
|
||||
# 未在 Gen.structs 登记的 struct(opaque/匿名)→ fail-fast。
|
||||
|
||||
Reference in new issue
Block a user