Skip to content

refactor: replace 27 positional-argument infer/hir.rs signatures with an inference context #1489

Description

@shunichironomura

Warning

This content was written by an AI agent and must be verified by a human developer. After human verification, this alert may be removed.

Summary

27 functions in crates/graphcal-compiler/src/tir/dim_check/infer/hir.rs (5283 lines) carry #[expect(clippy::too_many_arguments, ...)], each threading the same ambient parameter bundle. The context struct that solves this already exists one directory up and is already used by two sibling modules — the infer/ submodule was simply never migrated.

Current shape

#[expect(clippy::too_many_arguments, reason = "mirrors syntax inference context")]
fn infer_hir_type(
    expr: &hir::Expr,
    owner_decl_name: Option<&ResolvedDeclName>,
    declared_types: &HashMap<ScopedName, DeclaredType>,
    local_types: &HirLocalTypes<'_>,
    dag: &crate::tir::typed::DagTIR,
    tir: &crate::tir::typed::TIR,
    registry: &SemanticRegistry,
    builtin_fns: &crate::registry::builtins::BuiltinFunctions,
    src: &NamedSource<Arc<String>>,
) -> Result<InferredType, GraphcalError>

The reason strings are themselves the tell: "mirrors syntax inference context", "mirrors infer_hir_type's signature". There is a context object; it just is not reified here.

Affected functions include infer_hir_type, infer_hir_type_inner, infer_arg, infer_hir_fn_call, infer_hir_binop, infer_hir_if, infer_hir_for_comp, infer_hir_index_access, infer_hir_field_access, infer_hir_constructor_call, infer_hir_scan, infer_hir_unfold, infer_hir_match, infer_hir_dag_call and others.

Mechanically, in this one file: 85 bare registry, threading lines and 69 bare builtin_fns, threading lines.

The remedy already exists

crates/graphcal-compiler/src/tir/dim_check/mod.rs:282:

/// Per-DAG context bundle threaded through the dimension-check passes.
///
/// Bundles the read-only inputs that every per-declaration check needs
/// (declared types, the locals scope, TIR, registry, builtins, source)
/// so individual helpers take a single `&DimCheckContext` instead of
/// six positional arguments.
#[derive(Clone, Copy)]
struct DimCheckContext<'a> {
    cancellation: &'a crate::cancellation::CancellationToken,
    materialized_shapes: &'a infer::hir::MaterializedShapeCollector,
    declared_types: &'a HashMap<ScopedName, DeclaredType>,
    dag: Option<&'a crate::tir::typed::DagTIR>,
    tir: &'a crate::tir::typed::TIR,
    registry: &'a SemanticRegistry,
    builtin_fns: &'a crate::registry::builtins::BuiltinFunctions,
    src: &'a NamedSource<Arc<String>>,
}

It is already used by dim_check/mod.rs and dim_check/plot.rs.

Suggested fix

Introduce an InferCtx<'a> (or reuse/extend DimCheckContext) carrying the invariant fields, leaving only the genuinely varying arguments (expr, local_types, owner_decl_name) positional.

Expected effect: ~27 #[expect] attributes and several hundred argument-threading lines removed from the largest non-test file in the compiler.

Sequencing note

This is the largest diff of the review's findings and touches the file most likely to conflict with other in-flight work. Worth landing alone, on its own branch, with no behavioural change — the 2926-test suite is the oracle.

Provenance

Found during a full-workspace code review at 6e462341a (v0.0.1-alpha.27). Baseline at that commit: cargo clippy --workspace --all-targets clean, cargo test --workspace 2926 passed / 0 failed, cargo deny check advisories ok.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions