Skip to content

Commit 33ce1a3

Browse files
committed
refactor: simplify branch code with loop merge, function promotion, and match dedup
- Merge two separate function_symbols loops into a single pass in kompile.py - Promote _normalize_symbol_hashes to module-level in fixtures.py - Hoist duplicated def_id assignment out of match arms in utils.py
1 parent cc40dd6 commit 33ce1a3

3 files changed

Lines changed: 20 additions & 21 deletions

File tree

kmir/src/kmir/kompile.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -521,23 +521,21 @@ def _functions(kmir: KMIR, smir_info: SMIRInfo) -> dict[int, KInner]:
521521
for ty in smir_info.function_symbols_reverse[item_name]:
522522
functions[ty] = parsed_item_kinner.args[1]
523523

524-
# Add intrinsic functions
524+
# Add intrinsic functions and linked normal symbols that have no local body in `items`.
525+
# Normal symbols must still map to `monoItemFn(..., noBody)` instead of falling back to UNKNOWN FUNCTION.
525526
for ty, sym in smir_info.function_symbols.items():
526-
if 'IntrinsicSym' in sym and ty not in functions:
527+
if ty in functions:
528+
continue
529+
if 'IntrinsicSym' in sym:
527530
functions[ty] = KApply(
528531
'IntrinsicFunction',
529532
[KApply('symbol(_)_LIB_Symbol_String', [stringToken(sym['IntrinsicSym'])])],
530533
)
531-
532-
# Add linked normal symbols that have no local body in `items`.
533-
# They must still map to `monoItemFn(..., noBody)` instead of falling back to UNKNOWN FUNCTION.
534-
for ty, sym in smir_info.function_symbols.items():
535-
normal_sym = sym.get('NormalSym')
536-
if isinstance(normal_sym, str) and ty not in functions:
534+
elif isinstance(sym.get('NormalSym'), str):
537535
functions[ty] = KApply(
538536
'MonoItemKind::MonoItemFn',
539537
(
540-
KApply('symbol(_)_LIB_Symbol_String', (stringToken(normal_sym),)),
538+
KApply('symbol(_)_LIB_Symbol_String', (stringToken(sym['NormalSym']),)),
541539
KApply('defId(_)_BODY_DefId_Int', (intToken(ty),)),
542540
KApply('noBody_BODY_MaybeBody', ()),
543541
),

kmir/src/kmir/testing/fixtures.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ def pytest_configure(config) -> None:
2020
sys.setrecursionlimit(1000000)
2121

2222

23+
def _normalize_symbol_hashes(text: str) -> str:
24+
"""Normalize rustc symbol hash suffixes that drift across builds/environments."""
25+
text = re.sub(r'(_ZN[0-9A-Za-z_]+17h)[0-9a-f]+E', r'\1<hash>E', text)
26+
# Some show outputs truncate mangled symbols before trailing `E`.
27+
text = re.sub(r'(_ZN[0-9A-Za-z_]+17h)[0-9a-f]+', r'\1<hash>', text)
28+
# Normalize demangled hash suffixes (`...::h<hex>`).
29+
text = re.sub(r'(::h)[0-9a-f]{8,}', r'\1<hash>', text)
30+
return text
31+
32+
2333
def assert_or_update_show_output(
2434
actual_text: str, expected_file: Path, *, update: bool, path_replacements: dict[str, str] | None = None
2535
) -> None:
26-
def _normalize_symbol_hashes(text: str) -> str:
27-
# Mangled symbol hash suffixes can drift across rustc versions/builds.
28-
text = re.sub(r'(_ZN[0-9A-Za-z_]+17h)[0-9a-f]+E', r'\1<hash>E', text)
29-
# Some show outputs truncate mangled symbols before trailing `E`.
30-
text = re.sub(r'(_ZN[0-9A-Za-z_]+17h)[0-9a-f]+', r'\1<hash>', text)
31-
# Normalize demangled hash suffixes (`...::h<hex>`).
32-
text = re.sub(r'(::h)[0-9a-f]{8,}', r'\1<hash>', text)
33-
return text
34-
3536
if path_replacements:
3637
for old, new in path_replacements.items():
3738
actual_text = actual_text.replace(old, new)

kmir/src/kmir/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ def _annotate_nobody_function(k_cell: KInner, smir_info: SMIRInfo) -> list[str]:
319319
operands,
320320
KApply(label=KLabel(name='span'), args=[KToken(token=span_str)]),
321321
]:
322-
def_id = int(def_id_str)
323322
span = int(span_str)
324323
case [
325324
KApply(
@@ -332,13 +331,14 @@ def _annotate_nobody_function(k_cell: KInner, smir_info: SMIRInfo) -> list[str]:
332331
),
333332
operands,
334333
]:
335-
def_id = int(def_id_str)
336-
span = None
334+
pass
337335
case _:
338336
return []
339337
case _:
340338
return []
341339

340+
def_id = int(def_id_str)
341+
342342
# Prefer concrete symbol from the term; for unresolved placeholders, fall back to DefId lookup.
343343
if symbol_name == '\"** UNKNOWN FUNCTION **\"':
344344
func_sym = smir_info.function_symbols.get(def_id, {})

0 commit comments

Comments
 (0)