fix some variable inference

This commit is contained in:
pca006132 2020-12-23 15:39:39 +08:00 committed by pca006132
parent 461d403cce
commit 3f4f23c0ab
3 changed files with 11 additions and 9 deletions

View File

@ -264,6 +264,9 @@ def parse_list_comprehension(ctx: Context,
if node.generators[0].is_async: if node.generators[0].is_async:
raise CustomError('async list comprehension is not supported', node) raise CustomError('async list comprehension is not supported', node)
ty = parse_expr(ctx, sym_table, node.generators[0].iter) ty = parse_expr(ctx, sym_table, node.generators[0].iter)
if isinstance(ty, TypeVariable) and \
len(ty.constraints) == 1:
ty = ty.constraints[0]
if not isinstance(ty, ListType): if not isinstance(ty, ListType):
raise CustomError(f'unable to iterate over {ty}', node) raise CustomError(f'unable to iterate over {ty}', node)
try: try:

View File

@ -159,7 +159,11 @@ def parse_for_stmt(ctx: Context,
return_ty: Type, return_ty: Type,
node): node):
ty = parse_expr(ctx, sym_table, node.iter) ty = parse_expr(ctx, sym_table, node.iter)
if isinstance(ty, TypeVariable) and \
len(ty.constraints) == 1:
ty = ty.constraints[0]
if not isinstance(ty, ListType): if not isinstance(ty, ListType):
raise CustomError('only iteration over list is supported', node.iter) raise CustomError('only iteration over list is supported', node.iter)
binding = parse_simple_binding(node.target, ty.params[0]) binding = parse_simple_binding(node.target, ty.params[0])
for key, value in binding.items(): for key, value in binding.items():

View File

@ -126,17 +126,12 @@ def parse_type_var(ctx: Context, node):
raise CustomError('redefining type variable is not allowed', node) raise CustomError('redefining type variable is not allowed', node)
constraints = [] constraints = []
for v in node.value.args[1:]: for v in node.value.args[1:]:
if isinstance(v, ast.Constant): value, var = parse_type(ctx, v)
value = v.value if len(var) > 0:
elif isinstance(v, ast.Name):
value = v.id
else:
raise CustomError( raise CustomError(
'TypeVar constraints must be either string or type name', 'constraints cannot contain type variables',
node) node)
if value not in ctx.types: constraints.append(value)
raise CustomError(f'unbounded type {value}', node)
constraints.append(ctx.types[value])
ctx.variables[name] = TypeVariable(node.value.args[0].value, constraints) ctx.variables[name] = TypeVariable(node.value.args[0].value, constraints)