Skip to content

Consolidate fold helpers (arith fold ×5, ToSnakeCase ×2, TypeParameter wrappers ×2) #225

Description

@danfma

Problem

Multiple near-identical helpers shipped across recent compiler work:

Compiler — IrExpressionTreeExtractor.cs

  • TryFoldIntArith / TryFoldLongArith / TryFoldDoubleArith / TryFoldFloatArith / TryFoldDecimalArith — five 20-LOC switches differing only in numeric type and divide-by-zero/overflow guard.
  • TryFoldUnary mixes + identity (numeric-only), Negate, BitwiseNot, logical !.

Dart bridges

  • ToSnakeCase duplicated in Bridge/IrToDartTypeMapper.cs:132 and IrToDartNamingPolicy.cs:45.
  • ConvertTypeParameters wrapper duplicated in IrToDartInterfaceBridge.cs:85 and IrToDartClassBridge.cs:481 (both thin one-liners around IrToDartTypeParameterMapper.Map).

Fix

Compiler

  • One generic TryFoldArith<T> with delegate-keyed ops + INumber<T> constraint, or a single dispatch via dynamic (acceptable at build time).
  • Split TryFoldUnary into TryFoldNegate / TryFoldBitwiseNot / TryFoldLogicalNot / TryFoldUnaryPlus with a 4-arm dispatcher.

Dart

  • Hoist ToSnakeCase to IrToDartNamingPolicy (make internal), delete the private copy in the type mapper.
  • Delete ConvertTypeParameters wrappers; call IrToDartTypeParameterMapper.Map(...) at the 4 call sites directly. The delegate bridge already does this (line 51).

Motivation

Drive-by inconsistencies pile up with every numeric edge case (overflow, IEEE-754 semantics, decimal exceptions). One shared helper means one place to audit + fix.

Pure refactor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions