Do not answer when compiling this function?

I tried with Delphi XE and during compilation I did not respond. Does it work on your computer or is there something wrong with the function?

function Test(const FileName: string; const Force: boolean = false): boolean; var IsAllowed: boolean; begin result := false; if FileExists(FileName) then begin try if (Force) then begin result := false; exit; end; finally if IsAllowed then DeleteFile(FileName); end; try result := true; except result := false; end; end; end; 
+6
source share
1 answer

It compiles on my computer. Although I get warning W1036, the variable "IsAllowed" may not have been initialized.

Update: I can reproduce the hang when I include Windows in the uses clause. Submitted to Quality Central: QC93806 .

 program hang_test; {$APPTYPE CONSOLE} uses // Windows, // uncomment to include Windows -> hang on compile SysUtils; function Test(const FileName: string; const Force: boolean = false): boolean; // your function here begin try except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. 

Sounds like a mistake; You must report this to Quality Central .

Update 2: the minimum register that the compiler reproduces:

 function HangCompiler: Boolean; begin try Exit; // 1. exit from a try..finally finally DeleteFile(''); // 2. inlined function call in finally (include Windows to inline) end; // 3. try..except try Result := True; except Result := False; end; end; 
+11
source

Source: https://habr.com/ru/post/887909/


All Articles