尝试编译此代码是否导致 IDE 终止或编译器未能 运行 是一个错误?

Is it a bug that attempts to compile this code results in IDE terminating or the compiler failing to run?

注意在内联函数中使用 Exit 命令!我一直在这里使用 Delphi XE3。

症状

在某些情况下,当调用包含 Exit 命令的内联函数时,内联函数的 return 值 是在WriteLn()中直接使用,编译报错,

"dcc" exited with code 1.

甚至更糟糕的是,Delphi IDE 会在没有任何确认的情况下终止。

function ProcessNumber(const iNumber: Integer): Boolean; inline;
begin
  if iNumber = 0 then begin
    Result := False;
    Exit;
  end;
  // some code here ...
  Result := True;
end;

procedure Test;
begin
  writeln( ProcessNumber(0) );
end;

begin
  Test;
  ReadLn;
end.

但是,如果将内联函数的return值存储在一个变量中,然后在WriteLn()中使用该变量,问题不会发生。

procedure Test;
var
  b: Boolean;
begin
  b := ProcessNumber(0);
  writeln(b);
end;

问题

  1. 这是编译器错误吗?
  2. 如果这是一个错误,是否有安全退出内联函数的解决方法?

这肯定是一个错误。它出现在我测试的所有 IDE 版本中,XE3、XE7 和 XE8。老实说,我认为您无能为力。对我来说,IDE 每次都在编译时终止。我认为您只需要以不会导致 IDE 崩溃的方式编写代码。

您可以使用 IDE 选项强制编译使用 msbuild。这会将编译放入一个单独的进程中,从而确保 IDE 不会崩溃。不过这对你帮助不大,因为虽然你的 IDE 不会一直死掉,但你仍然无法编译你的程序!

当您使用 msbuild 构建时,您会收到以下形式的错误:

error F2084: Internal Error: GPFC00000FD-004D3F34-0

GPF代表General Protection Fault,即内存访问冲突。这大概是一个未处理的异常,当编译在进程中执行时,它正在杀死 IDE。

我的建议是您向 Quality Portal 提交错误报告。这是修复缺陷的唯一方法。尽管不要指望 XE3 会得到修复。

您可以在此处使用的一种解决方法是反转 if 条件实现,从而完全避免使用 Exit 命令。

所以不用

function ProcessNumber(const iNumber: Integer): Boolean; inline;
begin
  if iNumber = 0 then begin
    Result := False;
    Exit;
  end;
  // some code here ...
  Result := True;
end;

使用

function ProcessNumber(const iNumber: Integer): Boolean; inline;
begin
  if iNumber <> 0 then begin
    // some code here
    Result := True;
  end;
  else
    Result := False;
    //No exit needed here as this is already at the end of your method
end;