|
7 | 7 | List,
|
8 | 8 | Optional,
|
9 | 9 | Sequence,
|
| 10 | + Set, |
10 | 11 | Tuple,
|
11 | 12 | TypeAlias,
|
12 | 13 | TypeVar,
|
@@ -50,34 +51,48 @@ def _make_func_template(f: Callable[..., Any], func_name: str, func_globals: Dic
|
50 | 51 |
|
51 | 52 | func_sig = classinfo.parse_func_signature(f, func_globals, [])
|
52 | 53 | func_sig_converted, sig_parser = parse.convert_func_signature(
|
53 |
| - func_sig, func_name, func_globals, {}, [], self_type) |
| 54 | + func_sig, func_name, func_globals, {}, {}, self_type) |
| 55 | + implicit_type_params = sig_parser.implicit_type_params |
| 56 | + implicit_generic_params: Set[hir.GenericParameter] = set() |
| 57 | + for p in implicit_type_params.values(): |
| 58 | + assert isinstance(p, hir.SymbolicType) |
| 59 | + implicit_generic_params.add(p.param) |
54 | 60 |
|
55 | 61 | def parsing_func(args: hir.FunctionTemplateResolvingArgs) -> hir.FunctionLike:
|
56 | 62 | type_var_ns: Dict[TypeVar, hir.Type | hir.ComptimeValue] = {}
|
57 |
| - any_param_types: List[hir.Type] = [] |
| 63 | + mapped_implicit_type_params: Dict[str, |
| 64 | + hir.Type] = dict() |
58 | 65 | if is_generic:
|
59 | 66 | mapping = hir.match_func_template_args(func_sig_converted, args)
|
60 | 67 | if isinstance(mapping, hir.TypeInferenceError):
|
61 | 68 | raise mapping
|
62 | 69 | if len(mapping) != len(func_sig_converted.generic_params):
|
63 |
| - # print(mapping, func_sig_converted.generic_params) |
64 | 70 | raise hir.TypeInferenceError(
|
65 | 71 | None, "not all type parameters are resolved")
|
66 | 72 | for p in func_sig_converted.generic_params:
|
67 | 73 | if p not in mapping:
|
68 | 74 | raise hir.TypeInferenceError(
|
69 | 75 | None, f"type parameter {p} is not resolved")
|
70 |
| - type_var_ns[sig_parser.generic_param_to_type_var[p] |
71 |
| - ] = mapping[p] |
72 |
| - # print(f'binding {p.name} = {mapping[p]}, tv: {sig_parser.generic_param_to_type_var[p]} @{id(sig_parser.generic_param_to_type_var[p])}') |
73 |
| - # print('parsing instantiated signature') |
74 |
| - func_sig_instantiated, _ = parse.convert_func_signature( |
75 |
| - func_sig, func_name, func_globals, type_var_ns, any_param_types, self_type) |
| 76 | + if p not in implicit_generic_params: |
| 77 | + type_var_ns[sig_parser.generic_param_to_type_var[p] |
| 78 | + ] = mapping[p] |
| 79 | + |
| 80 | + for name, itp, in implicit_type_params.items(): |
| 81 | + assert isinstance(itp, hir.SymbolicType) |
| 82 | + gp = itp.param |
| 83 | + mapped_type = mapping[gp] |
| 84 | + assert isinstance(mapped_type, hir.Type) |
| 85 | + mapped_implicit_type_params[name] = mapped_type |
| 86 | + func_sig_instantiated, _p = parse.convert_func_signature( |
| 87 | + func_sig, func_name, func_globals, type_var_ns, mapped_implicit_type_params, self_type, mode='instantiate') |
| 88 | + assert len( |
| 89 | + func_sig_instantiated.generic_params) == 0, f"generic params should be resolved but found {func_sig_instantiated.generic_params}" |
76 | 90 | func_parser = FuncParser(
|
77 | 91 | func_name, f, func_sig_instantiated, func_globals, type_var_ns, self_type)
|
78 | 92 | return func_parser.parse_body()
|
79 | 93 | params = [v[0] for v in func_sig.args]
|
80 |
| - is_generic = len(func_sig.type_vars) > 0 |
| 94 | + is_generic = len(func_sig_converted.generic_params) > 0 |
| 95 | + # print(f"func {func_name} is_generic: {is_generic}") |
81 | 96 | return hir.FunctionTemplate(func_name, params, parsing_func, is_generic)
|
82 | 97 |
|
83 | 98 |
|
@@ -223,5 +238,3 @@ def decorator(f):
|
223 | 238 | return impl(f)
|
224 | 239 |
|
225 | 240 | return decorator
|
226 |
| - |
227 |
| - |
|
0 commit comments