Skip to content

Commit de0d147

Browse files
committed
Properties: add "Self" sloc to messages of implicit Property_Error exc.
When a property raises an error, for instance because it tries to get some field in a null node, it would be nice to include the sloc of the node in that property, so that error messages give users a clue about how it relates to the Ada source code that Libadalang analyzes, which would be useful to workaround or investigate an issue. Note that adding this sloc information is done on a best effort basis. As a notable example, the Property_Error exception raised in the lexical environment lookup system do not provide it, as the "Self" node for the property that triggers a lexical env lookup is not available there, and taking this information to this point would require a lot of logistics, and possibly run time performance degradation. Closes #635
1 parent c174e5c commit de0d147

File tree

27 files changed

+248
-115
lines changed

27 files changed

+248
-115
lines changed

langkit/expressions/base.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,7 @@ def to_symbol(self, prefix):
26302630
"""
26312631
prefix_expr = construct(prefix, T.String)
26322632
return CallExpr('Sym', 'String_To_Symbol', T.Symbol,
2633-
['Self.Unit.Context', prefix_expr],
2633+
[construct(Self), 'Self.Unit.Context', prefix_expr],
26342634
abstract_expr=self)
26352635

26362636

@@ -5434,8 +5434,13 @@ def as_int(self, expr):
54345434
if the big integer is out of range.
54355435
"""
54365436
big_int_expr = construct(expr, T.BigInt)
5437-
return CallExpr('Small_Int', 'To_Integer', T.Int, [big_int_expr],
5438-
abstract_expr=self)
5437+
return CallExpr(
5438+
'Small_Int',
5439+
'To_Integer',
5440+
T.Int,
5441+
[construct(Self), big_int_expr],
5442+
abstract_expr=self,
5443+
)
54395444

54405445

54415446
class Arithmetic(AbstractExpression):

langkit/expressions/boolean.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from langkit.diagnostics import check_source_language
88
from langkit.expressions.base import (
99
AbstractExpression, AbstractVariable, BasicExpr, BindingScope, CallExpr,
10-
ComputingExpr, LiteralExpr, PropertyDef, attr_call, construct,
10+
ComputingExpr, LiteralExpr, PropertyDef, Self, attr_call, construct,
1111
dsl_document, expr_or_null, render, sloc_info_arg, unsugar
1212
)
1313

@@ -289,8 +289,13 @@ def construct(self):
289289
self.LE: 'Less_Or_Equal',
290290
self.GT: 'Greater_Than',
291291
self.GE: 'Greater_Or_Equal'}[self.operator]
292-
return CallExpr('Node_Comp', 'Compare', T.Bool,
293-
[lhs, rhs, relation], abstract_expr=self)
292+
return CallExpr(
293+
'Node_Comp',
294+
'Compare',
295+
T.Bool,
296+
[construct(Self), lhs, rhs, relation],
297+
abstract_expr=self,
298+
)
294299

295300
# Otherwise, expect strict equality for both operands and use the
296301
# native comparison operator for code generation.

langkit/expressions/collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from langkit.expressions.base import (
1515
AbstractExpression, AbstractNodeData, AbstractVariable, CallExpr,
1616
ComputingExpr, FieldAccessExpr, LocalVars, NullCheckExpr, PropertyDef,
17-
ResolvedExpression, SequenceExpr, T, UncheckedCastExpr, VariableExpr,
17+
ResolvedExpression, Self, SequenceExpr, T, UncheckedCastExpr, VariableExpr,
1818
attr_call, attr_expr, auto_attr, auto_attr_custom, construct, render,
1919
unsugar
2020
)
@@ -865,7 +865,7 @@ def collection_get(self: AbstractExpression,
865865
or_null_expr = construct(or_null)
866866
result: ResolvedExpression = CallExpr(
867867
'Get_Result', 'Get', element_type,
868-
[coll_expr, index_expr, or_null_expr]
868+
[construct(Self), coll_expr, index_expr, or_null_expr]
869869
)
870870

871871
if as_entity:

langkit/expressions/envs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,11 @@ class IsVisibleFromExpr(CallExpr):
308308
def __init__(self, referenced_env, base_env, abstract_expr=None):
309309
super().__init__(
310310
'Is_Visible', 'Is_Visible_From', T.Bool,
311-
[construct(referenced_env, T.LexicalEnv),
312-
construct(base_env, T.LexicalEnv)],
311+
[
312+
construct(Self),
313+
construct(referenced_env, T.LexicalEnv),
314+
construct(base_env, T.LexicalEnv),
315+
],
313316
abstract_expr=abstract_expr
314317
)
315318

langkit/templates/array_types_ada.mako

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@
144144
145145
## Helper getter generated for properties code. Used in CollectionGet's code
146146
function Get
147-
(T : ${cls.name};
147+
(Node : ${T.root_node.name};
148+
T : ${cls.name};
148149
Index : Integer;
149150
Or_Null : Boolean := False) return ${elt_type};
150151
-- When Index is positive, return the Index'th element in T. Otherwise,
@@ -208,7 +209,8 @@
208209
---------
209210
210211
function Get
211-
(T : ${cls.name};
212+
(Node : ${T.root_node.name};
213+
T : ${cls.name};
212214
Index : Integer;
213215
Or_Null : Boolean := False) return ${elt_type}
214216
is
@@ -234,7 +236,10 @@
234236
elsif Or_Null then
235237
return ${cls.element_type.nullexpr};
236238
else
237-
raise Property_Error with "out-of-bounds array access";
239+
Raise_Property_Exception
240+
(Node,
241+
Property_Error'Identity,
242+
"out-of-bounds array access");
238243
end if;
239244
end Get;
240245

langkit/templates/astnode_types_ada.mako

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,11 @@
329329
## it's fine for the empty/root environments, as they don't
330330
## trigger relocations.
331331
if Is_Foreign_Strict (Env, Self) then
332-
raise Property_Error with
332+
Raise_Property_Exception
333+
(Self,
334+
Property_Error'Identity,
333335
"unsound foreign environment in RefEnvs ("
334-
& "${ref_env.str_location})";
336+
& "${ref_env.str_location})");
335337
end if;
336338
% endif
337339

0 commit comments

Comments
 (0)