[fpc-devel] goto illegal in fpc?

Sven Barth pascaldragon at googlemail.com
Fri Oct 9 13:39:05 CEST 2015

Am 09.10.2015 12:34 schrieb "Schneider" <schneidt at mail.nih.gov>:
> Folks:
> https://alum.mit.edu/www/toms/ftp/shell.p
> This is a test program I use to check Pascal compilers.  I could not
> figure out from the FPC site how "standard" it is.  When I try to
> compile shell.p, I get:
> % fpc shell.p
> Free Pascal Compiler version 2.6.4 [2015/10/07] for x86_64
> Copyright (c) 1993-2014 by Florian Klaempfl and others
> Target OS: Darwin for x86_64
> Compiling shell.p
> shell.p(211,1) Error: Goto statements aren't allowed between different
> shell.p(234) Fatal: There were 1 errors compiling module, stopping
> Fatal: Compilation aborted
> Error: /sw/bin/ppcx64 returned an error exitcode (normal if you did not
specify a source file to be compiled)
> The use of the goto is to escape the program when it detects an
> unresolvable error.  I use ONLY ONE goto in a 'halt' proceedure and
> that gives a message and then does a goto to the end of the program.
> As far as I know, jumping out of a procedure like this is standard
> Pascal.  Many of my programs use this mechanism, so if it is not
> allowed, I will be forced to abandon FPC.

By default Free Pascal only allows gotos inside the same function (and even
there with a few restrictions) (the compiler follows Delphi and AFAIK also
Turbo Pascal here). This is because goto is quite destructive regarding
some newer language features that require cleanup. That said there is the
modeswitch "nonlocalgoto" that you can enable (AFAIK that needs FPC 3.0.0
or newer).
Just add "{$modeswitch nonlocalgoto}" add the top of your program after any
other mode directive (AFAIK there is also a possibility to set modeswitches
on the command line).
AFAIK mode MacPas has that switch set by default (at least mode ISO does),
so it might also help to add "{$mode macpas}" or add -Mmacpas as a
parameter. This should also solve other compatibility problems.

> (Note:  I never need goto for any other code so being forced to have
> gotos stay within a single proceedure seems to make goto totally
> useless.)
> I thought that the -Sg flag would allow this but it didn't help.

-Sg only enables intrafunction gotos if they should be disabled (don't
remember which mode disables them...)

> Is there a (non standard!) exit function in fpc that I could use
> during compilation?  (Then I could substitute it for the goto call
> before compiling.)

I'd suggest you to use Halt(ErrorCode) instead. It's defined in unit system.

Alternatively you could use the exception handling mechanisms that FPC
"inherited" from Delphi.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20151009/ed9250cc/attachment.html>

More information about the fpc-devel mailing list