Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor naming of parser source span #7105

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions edb/edgeql/compiler/casts.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def compile_cast(
) -> irast.Set:

if new_stype.is_polymorphic(ctx.env.schema) and span is not None:
# If we have no srcctx we don't know whether this is a direct cast
# If we have no span we don't know whether this is a direct cast
# or some implicit cast being processed.
raise errors.QueryError(
f'cannot cast into generic type '
Expand Down Expand Up @@ -157,11 +157,11 @@ def compile_cast(
)
):
return _cast_array_literal(
ir_set, orig_stype, new_stype, srcctx=span, ctx=ctx)
ir_set, orig_stype, new_stype, span=span, ctx=ctx)

if orig_stype.is_tuple(ctx.env.schema):
return _cast_tuple(
ir_set, orig_stype, new_stype, srcctx=span, ctx=ctx)
ir_set, orig_stype, new_stype, span=span, ctx=ctx)

if isinstance(orig_stype, s_types.Array):
if not s_types.is_type_compatible(
Expand All @@ -181,7 +181,7 @@ def compile_cast(
ir_set, el_type, orig_stype, ctx=ctx)

return _cast_array(
ir_set, orig_stype, new_stype, srcctx=span, ctx=ctx)
ir_set, orig_stype, new_stype, span=span, ctx=ctx)

if isinstance(orig_stype, s_types.Range):
if s_types.is_type_compatible(
Expand All @@ -206,7 +206,7 @@ def compile_cast(
ir_set, orig_stype, mr_stype,
cardinality_mod=cardinality_mod, ctx=ctx)
return _cast_multirange(
ir_set, mr_stype, new_stype, srcctx=span, ctx=ctx)
ir_set, mr_stype, new_stype, span=span, ctx=ctx)

else:
# The subtypes match, so this is a direct upcast from
Expand All @@ -216,7 +216,7 @@ def compile_cast(
cardinality_mod=cardinality_mod, ctx=ctx)

return _cast_range(
ir_set, orig_stype, new_stype, srcctx=span, ctx=ctx)
ir_set, orig_stype, new_stype, span=span, ctx=ctx)

if orig_stype.is_multirange():
if s_types.is_type_compatible(
Expand All @@ -228,7 +228,7 @@ def compile_cast(
return ir_set
else:
return _cast_multirange(
ir_set, orig_stype, new_stype, srcctx=span, ctx=ctx)
ir_set, orig_stype, new_stype, span=span, ctx=ctx)

if orig_stype.issubclass(ctx.env.schema, new_stype):
# The new type is a supertype of the old type,
Expand Down Expand Up @@ -298,7 +298,7 @@ def compile_cast(
orig_stype,
new_stype,
cardinality_mod,
srcctx=span,
span=span,
ctx=ctx,
)

Expand All @@ -308,7 +308,7 @@ def compile_cast(
orig_stype,
new_stype,
cardinality_mod,
srcctx=span,
span=span,
ctx=ctx,
)

Expand All @@ -318,7 +318,7 @@ def compile_cast(
orig_stype,
new_stype,
cardinality_mod,
srcctx=span,
span=span,
ctx=ctx,
)

Expand All @@ -345,7 +345,7 @@ def compile_cast(
orig_stype,
new_stype,
cardinality_mod=cardinality_mod,
srcctx=span,
span=span,
ctx=ctx,
)

Expand Down Expand Up @@ -385,19 +385,19 @@ def _compile_cast(
ir_expr: Union[irast.Set, irast.Expr],
orig_stype: s_types.Type,
new_stype: s_types.Type, *,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel,
cardinality_mod: Optional[qlast.CardinalityModifier]) -> irast.Set:

ir_set = setgen.ensure_set(ir_expr, ctx=ctx)
cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx)
cast = _find_cast(orig_stype, new_stype, span=span, ctx=ctx)

if cast is None:
raise errors.QueryError(
f'cannot cast '
f'{orig_stype.get_displayname(ctx.env.schema)!r} to '
f'{new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx or ir_set.span)
span=span or ir_set.span)

return _cast_to_ir(ir_set, cast, orig_stype, new_stype,
cardinality_mod, ctx=ctx)
Expand Down Expand Up @@ -563,7 +563,7 @@ def get_abstract(self, schema: s_schema.Schema) -> bool:
def _find_cast(
orig_stype: s_types.Type,
new_stype: s_types.Type, *,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> Optional[s_casts.Cast]:

# Don't try to pick up casts when there is a direct subtyping
Expand Down Expand Up @@ -599,7 +599,7 @@ def _find_cast(
f'cannot unambiguously cast '
f'{orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx)
span=span)
else:
return None

Expand All @@ -610,7 +610,7 @@ def _cast_json_to_tuple(
new_stype: s_types.Tuple,
cardinality_mod: Optional[qlast.CardinalityModifier],
*,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

with ctx.new() as subctx:
Expand Down Expand Up @@ -655,7 +655,7 @@ def _cast_json_to_tuple(
val = compile_cast(
val, new_st,
cardinality_mod=qlast.CardinalityModifier.Required,
ctx=subctx, span=srcctx)
ctx=subctx, span=span)

elements.append(irast.TupleElement(name=new_el_name, val=val))

Expand All @@ -670,7 +670,7 @@ def _cast_tuple(
ir_set: irast.Set,
orig_stype: s_types.Type,
new_stype: s_types.Type, *,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

assert isinstance(orig_stype, s_types.Tuple)
Expand All @@ -680,7 +680,7 @@ def _cast_tuple(
# the tuple indirections.
pathctx.register_set_in_scope(ir_set, ctx=ctx)

direct_cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx)
direct_cast = _find_cast(orig_stype, new_stype, span=span, ctx=ctx)
orig_subtypes = dict(orig_stype.iter_subtypes(ctx.env.schema))

if direct_cast is not None:
Expand All @@ -698,7 +698,7 @@ def _cast_tuple(
)
val_type = setgen.get_set_type(val, ctx=ctx)
# Element cast
val = compile_cast(val, new_stype, ctx=ctx, span=srcctx)
val = compile_cast(val, new_stype, ctx=ctx, span=span)

elements.append(irast.TupleElement(name=n, val=val))

Expand All @@ -715,7 +715,7 @@ def _cast_tuple(
raise errors.QueryError(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx)
span=span)

assert isinstance(new_stype, s_types.Tuple)
new_subtypes = list(new_stype.iter_subtypes(ctx.env.schema))
Expand All @@ -724,7 +724,7 @@ def _cast_tuple(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}: '
f'the number of elements is not the same',
span=srcctx)
span=span)

# For tuple-to-tuple casts we generate a new tuple
# to simplify things on sqlgen side.
Expand All @@ -740,7 +740,7 @@ def _cast_tuple(
new_el_name, new_st = new_subtypes[i]
if val_type != new_st:
# Element cast
val = compile_cast(val, new_st, ctx=ctx, span=srcctx)
val = compile_cast(val, new_st, ctx=ctx, span=span)

elements.append(irast.TupleElement(name=new_el_name, val=val))

Expand All @@ -755,12 +755,12 @@ def _cast_range(
ir_set: irast.Set,
orig_stype: s_types.Type,
new_stype: s_types.Type, *,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

assert isinstance(orig_stype, s_types.Range)

direct_cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx)
direct_cast = _find_cast(orig_stype, new_stype, span=span, ctx=ctx)
if direct_cast is not None:
return _cast_to_ir(
ir_set, direct_cast, orig_stype, new_stype, ctx=ctx
Expand All @@ -770,18 +770,18 @@ def _cast_range(
raise errors.QueryError(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx)
span=span)
assert isinstance(new_stype, s_types.Range)
el_type = new_stype.get_subtypes(ctx.env.schema)[0]
orig_el_type = orig_stype.get_subtypes(ctx.env.schema)[0]
ql_el_type = typegen.type_to_ql_typeref(el_type, ctx=ctx)

el_cast = _find_cast(orig_el_type, el_type, srcctx=srcctx, ctx=ctx)
el_cast = _find_cast(orig_el_type, el_type, span=span, ctx=ctx)
if el_cast is None:
raise errors.QueryError(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx)
span=span)

with ctx.new() as subctx:
subctx.anchors = subctx.anchors.copy()
Expand Down Expand Up @@ -831,12 +831,12 @@ def _cast_multirange(
ir_set: irast.Set,
orig_stype: s_types.Type,
new_stype: s_types.Type, *,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

assert isinstance(orig_stype, s_types.MultiRange)

direct_cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx)
direct_cast = _find_cast(orig_stype, new_stype, span=span, ctx=ctx)
if direct_cast is not None:
return _cast_to_ir(
ir_set, direct_cast, orig_stype, new_stype, ctx=ctx
Expand All @@ -846,17 +846,17 @@ def _cast_multirange(
raise errors.QueryError(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx)
span=span)
assert isinstance(new_stype, s_types.MultiRange)
el_type = new_stype.get_subtypes(ctx.env.schema)[0]
orig_el_type = orig_stype.get_subtypes(ctx.env.schema)[0]

el_cast = _find_cast(orig_el_type, el_type, srcctx=srcctx, ctx=ctx)
el_cast = _find_cast(orig_el_type, el_type, span=span, ctx=ctx)
if el_cast is None:
raise errors.QueryError(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx)
span=span)

ctx.env.schema, new_range_type = s_types.Range.from_subtypes(
ctx.env.schema, [el_type])
Expand Down Expand Up @@ -900,7 +900,7 @@ def _cast_json_to_range(
new_stype: s_types.Range,
cardinality_mod: Optional[qlast.CardinalityModifier],
*,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

with ctx.new() as subctx:
Expand Down Expand Up @@ -1008,7 +1008,7 @@ def _cast_json_to_multirange(
new_stype: s_types.MultiRange,
cardinality_mod: Optional[qlast.CardinalityModifier],
*,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

ctx.env.schema, new_range_type = s_types.Range.from_subtypes(
Expand Down Expand Up @@ -1057,19 +1057,19 @@ def _cast_array(
ir_set: irast.Set,
orig_stype: s_types.Type,
new_stype: s_types.Type, *,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

assert isinstance(orig_stype, s_types.Array)

direct_cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx)
direct_cast = _find_cast(orig_stype, new_stype, span=span, ctx=ctx)

if direct_cast is None:
if not new_stype.is_array():
raise errors.QueryError(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx)
span=span)
assert isinstance(new_stype, s_types.Array)
el_type = new_stype.get_subtypes(ctx.env.schema)[0]
elif new_stype.is_json(ctx.env.schema):
Expand All @@ -1082,7 +1082,7 @@ def _cast_array(

orig_el_type = orig_stype.get_subtypes(ctx.env.schema)[0]

el_cast = _find_cast(orig_el_type, el_type, srcctx=srcctx, ctx=ctx)
el_cast = _find_cast(orig_el_type, el_type, span=span, ctx=ctx)

if el_cast is not None and el_cast.get_from_cast(ctx.env.schema):
# Simple cast
Expand Down Expand Up @@ -1158,21 +1158,21 @@ def _cast_array_literal(
ir_set: irast.Set,
orig_stype: s_types.Type,
new_stype: s_types.Type, *,
srcctx: Optional[parsing.Span],
span: Optional[parsing.Span],
ctx: context.ContextLevel) -> irast.Set:

assert isinstance(ir_set.expr, irast.Array)

orig_typeref = typegen.type_to_typeref(orig_stype, env=ctx.env)
new_typeref = typegen.type_to_typeref(new_stype, env=ctx.env)
direct_cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx)
direct_cast = _find_cast(orig_stype, new_stype, span=span, ctx=ctx)

if direct_cast is None:
if not new_stype.is_array():
raise errors.QueryError(
f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} '
f'to {new_stype.get_displayname(ctx.env.schema)!r}',
span=srcctx) from None
span=span) from None
assert isinstance(new_stype, s_types.Array)
el_type = new_stype.get_subtypes(ctx.env.schema)[0]
intermediate_stype = orig_stype
Expand All @@ -1188,7 +1188,7 @@ def _cast_array_literal(
for el in ir_set.expr.elements:
el = compile_cast(el, el_type,
cardinality_mod=qlast.CardinalityModifier.Required,
ctx=ctx, span=srcctx)
ctx=ctx, span=span)
casted_els.append(el)

new_array = setgen.ensure_set(
Expand Down
4 changes: 2 additions & 2 deletions edb/edgeql/compiler/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ def compile_TypeCast(
def _infer_type_introspection(
typeref: irast.TypeRef,
env: context.Environment,
srcctx: Optional[parsing.Span]=None,
span: Optional[parsing.Span]=None,
) -> s_types.Type:
if irtyputils.is_scalar(typeref):
return cast(s_objtypes.ObjectType,
Expand All @@ -792,7 +792,7 @@ def _infer_type_introspection(
env.schema.get('schema::MultiRange'))
else:
raise errors.QueryError(
'unexpected type in INTROSPECT', span=srcctx)
'unexpected type in INTROSPECT', span=span)


@dispatch.compile.register(qlast.Introspect)
Expand Down
2 changes: 1 addition & 1 deletion edb/edgeql/compiler/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ def get_globals(
and not ctx.env.options.json_parameters
):
glob_set = setgen.get_globals_as_json(
tuple(globs), ctx=ctx, srcctx=expr.span)
tuple(globs), ctx=ctx, span=expr.span)
else:
if ctx.env.options.func_params is not None:
# Make sure that we properly track the globals we use in functions
Expand Down
Loading