Skip to content

Fix compiler state corruption after an exception in the OnUses handler - #300

Open
TetzkatLipHoka wants to merge 2 commits into
remobjects:masterfrom
TetzkatLipHoka:fix/onuses-exception-state
Open

TetzkatLipHoka wants to merge 2 commits into
remobjects:masterfrom
TetzkatLipHoka:fix/onuses-exception-state

Conversation

@TetzkatLipHoka

Copy link
Copy Markdown

With PS_USESSUPPORT, ProcessUses sets fModule to the name of the unit
being imported before invoking the OnUses callback. The regular failure
path (OnUses returning False) restores fModule and FParser afterwards,
but the exception handler did not. The stale fModule survives Cleanup:
Compile saves it as OldFileName on entry and restores it after the
System import, so every following Compile on the same instance treats
the main module as the previously failed unit and rejects any
'uses ;' with a bogus cross-reference error.

Reproduction (one compiler instance):

  1. Compile a script whose OnUses handler raises (e.g. a failing
    AddTypeS registration) -> correct error is reported.
  2. Compile any script with 'uses ;' again
    -> before: 'Cross-Reference error of '
    -> after: compiles normally (or reports the real error again).

The exception handler now restores FParser and fModule exactly like the
regular failure path. MakeError stays first so the error keeps being
attributed to the failed unit.

🤖 Generated with Claude Code

With PS_USESSUPPORT, ProcessUses sets fModule to the name of the unit
being imported before invoking the OnUses callback. The regular failure
path (OnUses returning False) restores fModule and FParser afterwards,
but the exception handler did not. The stale fModule survives Cleanup:
Compile saves it as OldFileName on entry and restores it after the
System import, so every following Compile on the same instance treats
the main module as the previously failed unit and rejects any
'uses <that unit>;' with a bogus cross-reference error.

Reproduction (one compiler instance):
  1. Compile a script whose OnUses handler raises (e.g. a failing
     AddTypeS registration) -> correct error is reported.
  2. Compile any script with 'uses <SameUnit>;' again
     -> before: 'Cross-Reference error of <unit>'
     -> after:  compiles normally (or reports the real error again).

The exception handler now restores FParser and fModule exactly like the
regular failure path. MakeError stays first so the error keeps being
attributed to the failed unit.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Vizit0r

Vizit0r commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

We took this fix downstream (Delphi 13, PS_USESSUPPORT defined) and it does what it says: before it, a second Compile on the same instance failed with Cross-Reference error of '<unit>'; after it, it compiles.

One thing to adjust: the except branch now reads ParserPos and OldFileName, but both are only assigned inside the try, so Delphi reports

uPSCompiler.pas: warning W1036: Variable 'ParserPos' might not have been initialized

and it is not only formal: an exception raised before those assignments (e.g. from FParser.OriginalToken) would restore FParser from an uninitialized local. Saving both before the try fixes it; they cannot raise and FParser does not change in between:

        if @FOnUses <> nil then
        begin
          {$IFDEF PS_USESSUPPORT}
          // saved before the try: the except branch restores both
          OldFileName:=fModule;
          ParserPos:=FParser;
          {$ENDIF}
          try
            {$IFDEF PS_USESSUPPORT}
            fModule:=FParser.OriginalToken;
            ParseUnit:=FParser.OriginalToken;
            {$ENDIF}
            if not OnUses(Self, FParser.GetToken) then

With that the warning is gone and the behaviour is the same.

… it from

The except branch restores FParser and fModule from locals that were only
assigned inside the try. An exception before those assignments would restore
FParser from an uninitialized local; dcc reports W1036 'ParserPos might not have
been initialized' with PS_USESSUPPORT. Save both before the try: they cannot
raise and FParser does not change in between.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TetzkatLipHoka

Copy link
Copy Markdown
Author

Thanks, applied: both are now saved before the try, and W1036 is gone.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants