Skip to content

Commit a556e9b

Browse files
committed
Langkit_Support.Symbols: fix Get_Symbol for null symbols
When symbol canonicalization fails, $.Lexer_Implementation.Force_Symbol calls Get_Symbol passing to it No_Thin_Symbol, so Get_Symbol needs to support this case. TN: T923-007
1 parent 2580460 commit a556e9b

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

langkit/support/langkit_support-symbols.adb

+5-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ package body Langkit_Support.Symbols is
135135
function Get_Symbol
136136
(Self : Symbol_Table; TS : Thin_Symbol) return Symbol_Type is
137137
begin
138-
return Self.Symbols.Get (Positive (TS));
138+
if TS = No_Thin_Symbol then
139+
return null;
140+
else
141+
return Self.Symbols.Get (Positive (TS));
142+
end if;
139143
end Get_Symbol;
140144

141145
end Langkit_Support.Symbols;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package body Pkg is
2+
3+
------------------
4+
-- Canonicalize --
5+
------------------
6+
7+
function Canonicalize (Name : Text_Type) return Symbolization_Result is
8+
pragma Unreferenced (Name);
9+
begin
10+
return Create_Error ("no symbol allowed");
11+
end Canonicalize;
12+
13+
end Pkg;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
with Langkit_Support.Text; use Langkit_Support.Text;
2+
with Libfoolang.Common; use Libfoolang.Common;
3+
4+
package Pkg is
5+
6+
function Canonicalize (Name : Text_Type) return Symbolization_Result;
7+
8+
end Pkg;

testsuite/tests/properties/symbol/main.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
print(d)
1313
sys.exit(1)
1414

15-
try:
16-
result = u.root.p_prop(None)
17-
except libfoolang.PropertyError as exc:
18-
result = '<{}: {}>'.format(type(exc).__name__, exc)
19-
20-
print('p_prop(None) = {}'.format(result))
15+
for n in (None, u.root):
16+
try:
17+
result = libfoolang._py2to3.text_repr(u.root.p_prop(n))
18+
except libfoolang.PropertyError as exc:
19+
result = '<{}: {}>'.format(type(exc).__name__, exc)
20+
print('p_prop({}) = {}'.format(n, result))
2121

2222
print('main.py: Done.')
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
main.py: Running...
22
p_prop(None) = <PropertyError: cannot get the symbol of a null node>
3+
p_prop(<Example main.txt:1:1-1:8>) = ''
34
main.py: Done.
45
Done

testsuite/tests/properties/symbol/test.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""
2-
Test that ".symbol" raises a property error on null nodes.
2+
Test that ".symbol" raises a property error on null nodes or when symbol
3+
canonicalization fails.
34
"""
45

6+
from langkit.compile_context import LibraryEntity
57
from langkit.dsl import ASTNode, T
68
from langkit.expressions import langkit_property
79

@@ -19,5 +21,6 @@ class Example(FooNode):
1921
token_node = True
2022

2123

22-
build_and_run(lkt_file='expected_concrete_syntax.lkt', py_script='main.py')
24+
build_and_run(lkt_file='expected_concrete_syntax.lkt', py_script='main.py',
25+
symbol_canonicalizer=LibraryEntity('Pkg', 'Canonicalize'))
2326
print('Done')

0 commit comments

Comments
 (0)