Skip to content

[Feature]: C#: type foreach loop variables for receiver resolution #4420

Closed

Description

@nixvideojuego-ui

Problem

A C# foreach loop variable gets no binding in the method-scoped receiver table (_csharp_method_receiver_types), so a call on it, or a chain starting at it, never resolves across files:

foreach (var a in agents.Values) a.look.Animate();   // agents: Dictionary<string, Agent>
foreach (var a in list) a.Step();                     // list: List<Agent>
foreach (Agent a in xs) a.Step();                     // even an explicitly typed loop variable

All three produce no calls edge when Agent and Look live in another file. In Unity code this is the usual shape for per-frame work (foreach (var a in agents.Values) a.look.LateUpdate();), so the calls that matter most for performance questions are missing from the graph.

Reproduction

import os, tempfile
from pathlib import Path
from graphify.extract import extract

os.chdir(tempfile.mkdtemp())
Path("Model.cs").write_text(
    "namespace Demo {\n"
    "    public class Look { public void Animate() { } }\n"
    "    public class Agent { public Look look; public void Step() { } }\n"
    "}\n")
Path("Crowd.cs").write_text(
    "using System.Collections.Generic;\n"
    "namespace Demo {\n"
    "    public class Crowd {\n"
    "        readonly Dictionary<string, Agent> agents = new Dictionary<string, Agent>();\n"
    "        readonly List<Agent> list = new List<Agent>();\n"
    "        public void Tick() { foreach (var a in agents.Values) a.look.Animate(); }\n"
    "        public void Walk() { foreach (var a in list) a.Step(); }\n"
    "        public void Typed(IEnumerable<object> xs) { foreach (Agent a in xs) a.Step(); }\n"
    "    }\n"
    "}\n")
r = extract([Path("Model.cs"), Path("Crowd.cs")], cache_root=Path(".cache"))
label = {n["id"]: n["label"] for n in r["nodes"]}
calls = [(label[e["source"]], label[e["target"]]) for e in r["edges"] if e["relation"] == "calls"]
print("calls from Crowd:", [c for c in calls if c[0] in (".Tick()", ".Walk()", ".Typed()")])
calls from Crowd: []

Proposed behavior

Bind the loop variable inside its loop:

  • to its explicit type (foreach (Agent a in ...));
  • for var, to what the iterated class field or property yields: the element of a T[] or a sequence type (List<T>, IEnumerable<T>, HashSet<T>...), and a dictionary's value or key type for f.Values / f.Keys.

Stay untyped (no guess) for:

  • iterating a dictionary itself (KeyValuePair);
  • nested collections and type parameters;
  • a loop over a name that a local or parameter shadows.

Alternatives

Typing the iterated expression in general (locals, method return types, chains such as s.modules) would cover more loops. This proposal keeps to fields and properties, where the declared type is already recorded per class, and leaves the rest untyped.

Compatibility

Additive: the change only creates calls edges that are missing today, and in the patch it adds no edges anywhere else. On a 1,512-file Unity C# codebase, it added 58 calls edges and removed none.

Graphify 0.9.89 (v8 e8eb76e), Windows 11, Python 3.14.

Activity

  1. github-actions commented on Oct 13, 2026

    @github-actions

    Thanks for opening this issue, @nixvideojuego-ui. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. added a commit that references this issue on Oct 13, 2026
    b4f4a5b
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions