From 4346e3fb2764afc624a4f37232e42aff994d7fdc Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sun, 18 Oct 2026 17:24:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(len):=20list=20=E7=89=B9=E5=8C=96=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=AF=BB=E5=8F=96=20=5F=5Fcount=5F=5F=20=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=BD=9C=E4=B8=BA=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 泛型特化方法未生成时,len(list[...]) 按结构体偏移直接读取 __count__,不再误报没有 __len__。 --- lib/core/Handles/HandlesExprBuiltin.py | 8 ++++++++ lib/core/Handles/HandlesExprCall.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/core/Handles/HandlesExprBuiltin.py b/lib/core/Handles/HandlesExprBuiltin.py index bab40b9..848ce25 100644 --- a/lib/core/Handles/HandlesExprBuiltin.py +++ b/lib/core/Handles/HandlesExprBuiltin.py @@ -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。 diff --git a/lib/core/Handles/HandlesExprCall.py b/lib/core/Handles/HandlesExprCall.py index 00b7b0a..b5107ad 100644 --- a/lib/core/Handles/HandlesExprCall.py +++ b/lib/core/Handles/HandlesExprCall.py @@ -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。