Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Delphi parser loses every call inside a `for var x in …` loop

Open
#2,546 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
tooling

Research direction

Start by reproducing with the Loop.pas sample in the issue, running codegraph callees WithVar and codegraph callers DropIfEmpty. Then inspect the parse tree the Pascal grammar produces for for var lItem in AList do, which the issue shows as an ERROR node, and compare it with the working for lItem in AList do form. The grammar ships as tree-sitter-pascal.wasm, so the fix may belong upstream. Done when WithVar lists DropIfEmpty as a callee and DropIfEmpty has two callers.

Written by the indexing model from the issue text.

Description

Summary

In a Delphi (Object Pascal) unit, a for … in loop that declares its variable inline (for var lItem in AList do, Delphi 10.3 and later) is an ERROR node for the parser: the loop and its body contribute nothing, so a call made in the body is missing from callees of the
routine and from callers of the callee. The same loop with the variable declared in a var section (for lItem in AList do) is indexed with its calls.

Versions

  • codegraph --version: 1.6.2 (Windows x64 bundle).
  • Windows 10 Pro 10.0.19045.

Minimal reproduction

Loop.pas:

unit Loop;

interface

type
  TItem = class
  public
    function DropIfEmpty: Boolean;
  end;

procedure WithVar(const AList: TArray<TItem>);
procedure WithDecl(const AList: TArray<TItem>);

implementation

function TItem.DropIfEmpty: Boolean;
begin
  Result := True;
end;

procedure WithVar(const AList: TArray<TItem>);
begin
  for var lItem in AList do
    lItem.DropIfEmpty;
end;

procedure WithDecl(const AList: TArray<TItem>);
var
  lItem: TItem;
begin
  for lItem in AList do
    lItem.DropIfEmpty;
end;

end.

Steps, in a directory holding only that file:

codegraph init -y .
codegraph callees WithVar
codegraph callees WithDecl
codegraph callers DropIfEmpty

Observed (1.6.2):

●  6 nodes, 6 edges
function WithVar (pascal) — Loop.pas:11
ℹ No callees found for "WithVar"
function WithDecl (pascal) — Loop.pas:12
method      DropIfEmpty
  Loop.pas:8
Callers of "DropIfEmpty" (1):
method TItem::DropIfEmpty (pascal) — Loop.pas:8
function    WithDecl
  Loop.pas:12

Expected: WithVar with the callee DropIfEmpty as well, and DropIfEmpty with two callers.

The grammar's tree for the body of WithVar (tree-sitter-pascal.wasm shipped in 1.6.2) — the whole loop is an ERROR, the body's lItem.DropIfEmpty never becomes a statement/exprDot:

block !
  ERROR !
    kFor
    varAssignDef !
      kVar                 "var"
      ERROR !              lItem in AList do
      ERROR !              lItem
      identifier           "DropIfEmpty"

The other inline-variable forms parse: var x := …; (varAssignDef), var y: T; (varDef) and for var i := 0 to … do (for with a varAssignDef); only for var x in … is missing.

Where it was found

A product of about 600 for var … in loops across its units: callers DropIfEmpty names five of the six routines that call it, and the one left out calls it from for var lItem in lEmpty do lItem.DropIfEmpty;. Fourteen of the loops type the variable (for var lHandle: THandle in lHandles do).

Note on the syntax

for var x in … is Delphi's inline variable declaration inside a for-in loop (documented under Inline Variable Declaration, Delphi 10.3 Rio and later); the variable may carry a type (for var x: T in …), generic types included.

Dominant language
C
Stars
73.2k
Forks
4.7k
Avg merge
2h 13m
Merged PRs (30d)
418

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from colbymchenry/codegraph

All issues in colbymchenry/codegraph

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.