Anonymous functions: Fatal: Internal error 200109092 issued by compiler when setting result of an anonymous function to a procedure cast to a function reference
## Summary When using an anonymous function that returns a function reference, the compile fails with `Fatal: Internal error 200109092` when the result variable is set to a function reference cast of an existing procedure with a matching function reference prototype. ## System Information - **Operating system:** Linux, AlmaLinux 9.1, x86_64 - **Processor architecture:** x86-64 - **Compiler version:** 3.3.1-12460-g0e05e908d5 - **Device:** Computer ## Steps to reproduce When trying to compile the following program: ```pascal program something_cool; {$Mode objfpc}{$H+} {$ModeSwitch anonymousfunctions} {$ModeSwitch functionreferences} {$ModeSwitch nestedprocvars} type TVoidFunc = reference to procedure; TFuncMaker = reference to function(const thing: string): TVoidFunc; procedure something; begin writeln('something...'); end; procedure main; var cool_bingo: TVoidFunc; coolifier: TFuncMaker; begin coolifier := function (const thing: string): TVoidFunc begin // result := procedure begin writeln('cool ', thing) end; result := TVoidFunc(@something); end; cool_bingo := coolifier('bingo'); cool_bingo(); end; begin main; end. ``` The compile fails as follows: ```shell $ fpc something_cool.pas Free Pascal Compiler version 3.3.1 [2023/02/09] for x86_64 Copyright (c) 1993-2023 by Florian Klaempfl and others Target OS: Linux for x86-64 Compiling something_cool.pas something_cool.pas(25,14) Fatal: Internal error 200109092 Fatal: Compilation aborted Error: /home/dev/fpc_usr/lib/fpc/3.3.1/ppcx64 returned an error exitcode ``` ## Example Project See Steps to Reproduce. ## What is the current bug behavior? See Steps to Reproduce. ## What is the expected (correct) behavior? That it compiles and runs, because this compiles and runs: ```pascal program something_cool2; {$Mode objfpc}{$H+} {$ModeSwitch anonymousfunctions} {$ModeSwitch functionreferences} {$ModeSwitch nestedprocvars} type TVoidFunc = reference to procedure; TFuncMaker = reference to procedure(const thing: string; var func: TVoidFunc); function main: TVoidFunc; var cool_bingo: TVoidFunc; coolifier: TFuncMaker; n: integer = 0; begin coolifier := procedure (const thing: string; var func: TVoidFunc) begin func := procedure begin inc(n); writeln(n, ' cool ', thing); end; end; coolifier('bingo', cool_bingo); cool_bingo(); result := cool_bingo; end; var func: TVoidFunc; begin func := main; func(); end. ``` ```shell $ fpc something_cool2.pas Free Pascal Compiler version 3.3.1 [2023/02/09] for x86_64 Copyright (c) 1993-2023 by Florian Klaempfl and others Target OS: Linux for x86-64 Compiling something_cool2.pas Linking something_cool2 35 lines compiled, 0.1 sec, 144624 bytes code, 58008 bytes data $ ./something_cool2 1 cool bingo 2 cool bingo ``` ## Relevant logs and/or screenshots See Steps to Reproduce. ## Possible fixes None that I know of. ## Related bugs This is related to https://gitlab.com/freepascal.org/fpc/source/-/issues/40142, but with a different compiler error message. https://gitlab.com/freepascal.org/fpc/source/-/issues/20251 has the same internal error, but does not appear to be related.
issue