Skip to content

Commit 72dc59f

Browse files
committed
fix(haskell): name infix-form definitions after the operator (DeusData#2440)
A Haskell definition written in infix form (`a <+> b = ...`, ``x `plus` y = ...``) has no `name` field. resolve_haskell_func_name took the first child of its `infix` head, which is the left operand, so the def was indexed as `a` / `x` and the operator itself got no node. Resolve the head's `operator` field instead: a backticked `infix_id` yields its inner variable (`plus`), and a symbolic `operator` is parenthesised in cbm_func_name_node_text (`(<+>)`), matching the name the prefix form `(<+>) a b = ...` already gets. The def extractor and the call-scope QN both go through these two functions, so they stay in agreement. Fixes DeusData#2440 Signed-off-by: Edson <zhuoxi2000@gmail.com>
1 parent 0f52d30 commit 72dc59f

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,33 @@ static TSNode resolve_lean_func_name(TSNode node, TSNode name) {
405405
return cbm_find_child_by_kind(node, "ident");
406406
}
407407

408+
// Haskell infix-form definition head (`a <+> b`, ``x `plus` y``): the defined
409+
// name is the operator, not the left operand that is the head's first child. A
410+
// backticked `infix_id` yields its inner variable (`plus`); a symbolic `operator`
411+
// is returned as-is and parenthesised by cbm_func_name_node_text, matching the
412+
// prefix form `(<+>) a b = ...` (#2440).
413+
static TSNode resolve_haskell_infix_head_name(TSNode head) {
414+
enum { INFIX_MIN_CHILDREN = 3, INFIX_OP_IDX = 1 };
415+
TSNode op = ts_node_child_by_field_name(head, TS_FIELD("operator"));
416+
if (ts_node_is_null(op) && ts_node_child_count(head) >= INFIX_MIN_CHILDREN) {
417+
op = ts_node_child(head, INFIX_OP_IDX);
418+
}
419+
if (!ts_node_is_null(op)) {
420+
const char *ok = ts_node_type(op);
421+
if (strcmp(ok, "operator") == 0) {
422+
return op;
423+
}
424+
if (strcmp(ok, "infix_id") == 0) {
425+
TSNode v = cbm_find_child_by_kind(op, "variable");
426+
if (!ts_node_is_null(v)) {
427+
return v;
428+
}
429+
}
430+
}
431+
TSNode null_node = {0};
432+
return null_node;
433+
}
434+
408435
// Haskell: resolve function name from first named child (variable/name).
409436
static TSNode resolve_haskell_func_name(TSNode node) {
410437
if (ts_node_named_child_count(node) > 0) {
@@ -413,6 +440,9 @@ static TSNode resolve_haskell_func_name(TSNode node) {
413440
if (strcmp(hk, "variable") == 0 || strcmp(hk, "name") == 0) {
414441
return head;
415442
}
443+
if (strcmp(hk, "infix") == 0) {
444+
return resolve_haskell_infix_head_name(head);
445+
}
416446
if (ts_node_named_child_count(head) > 0) {
417447
TSNode v = ts_node_named_child(head, 0);
418448
const char *vk = ts_node_type(v);

‎internal/cbm/helpers.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,14 @@ char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source,
10421042
if (text && lang == CBM_LANG_NIX) {
10431043
cbm_nix_strip_attr_quotes(text);
10441044
}
1045+
/* Haskell infix-form operator definition: `a <+> b = …` resolves to the bare
1046+
* `operator` node `<+>`, while the prefix form `(<+>) a b = …` is named by its
1047+
* `prefix_id` text `(<+>)`. Parenthesise here, not in the resolver, so both
1048+
* forms mint the same name and the def name and the call-scope QN, which both
1049+
* route through this function, cannot disagree (#2440). */
1050+
if (text && lang == CBM_LANG_HASKELL && strcmp(ts_node_type(name_node), "operator") == 0) {
1051+
return cbm_arena_sprintf(a, "(%s)", text);
1052+
}
10451053
return text;
10461054
}
10471055

‎tests/test_extraction.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,33 @@ TEST(haskell_function) {
13771377
PASS();
13781378
}
13791379

1380+
/* #2440: an infix-form definition (`a <+> b = ...`, ``x `plus` y = ...``) has
1381+
* no `name` field; its first named child is an `infix` node whose first child
1382+
* is the LEFT OPERAND, so the def was named `a` / `x`. It must be named after
1383+
* the operator, consistent with the prefix form `(<+>) a b = ...`. */
1384+
TEST(haskell_infix_definition_name) {
1385+
CBMFileResult *r = extract("module M where\n"
1386+
"\n"
1387+
"(<+>) :: Int -> Int -> Int\n"
1388+
"a <+> b = a + b\n"
1389+
"\n"
1390+
"plus :: Int -> Int -> Int\n"
1391+
"x `plus` y = x + y\n"
1392+
"\n"
1393+
"f :: Int\n"
1394+
"f = 1 <+> 2\n",
1395+
CBM_LANG_HASKELL, "t", "M.hs");
1396+
ASSERT_NOT_NULL(r);
1397+
ASSERT_FALSE(r->has_error);
1398+
ASSERT(has_def(r, "Function", "f"));
1399+
ASSERT(has_def(r, "Function", "plus"));
1400+
ASSERT(has_def(r, "Function", "(<+>)"));
1401+
ASSERT_FALSE(has_def(r, "Function", "a"));
1402+
ASSERT_FALSE(has_def(r, "Function", "x"));
1403+
cbm_free_result(r);
1404+
PASS();
1405+
}
1406+
13801407
/* --- OCaml --- */
13811408
TEST(ocaml_function) {
13821409
CBMFileResult *r =
@@ -8954,6 +8981,7 @@ SUITE(extraction) {
89548981
RUN_TEST(elixir_function);
89558982
RUN_TEST(elixir_call_string_argument);
89568983
RUN_TEST(haskell_function);
8984+
RUN_TEST(haskell_infix_definition_name);
89578985
RUN_TEST(ocaml_function);
89588986
RUN_TEST(erlang_function);
89598987

0 commit comments

Comments
 (0)