Lars Fosdal 1792 Posted September 13, 2019 https://quality.embarcadero.com/browse/RSP-23056 Debugger watch / evaluate shows wrong values for inline declared local variables with duplicate names within same routine. Put a breakpoint on each of the lines with breakpoint in the comment. Run at A, hover over variable s, or evaluate s shows value A at C, hover over variable s, or evaluate s shows value A - should have been C The actual value is C - so it is a debugger bug program ReusedLocalVariables; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TTest = class public procedure Check(const aValue: String); procedure Test; end; procedure TTest.Check(const aValue: String); begin Writeln(aValue); end; procedure TTest.Test; begin begin var s := 'A'; Check(s); // Breakpoint: S shows 'A' - Output in check = 'A' end; begin var s := 'C'; Check(s); // Breakpoint: S shows 'A' - should have been 'C' - Output in check = 'C' end; end; begin try var Test := TTest.Create; try Test.Test; Test.Free; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; finally Write('Press Enter: '); Readln; end; end. 1 1 Share this post Link to post
Sherlock 663 Posted September 13, 2019 1) Don't use it. Secondly: Don't use it like that. 😉 1 Share this post Link to post
David Heffernan 2345 Posted September 13, 2019 You reap what you sow. Don't create multiple variables with the same name. 1 Share this post Link to post
Clément 148 Posted September 13, 2019 This really should be fixed! Debugging this code structure will be more common as soon as 10.4 will fix inline declaration or when LSP will be released. procedure TTest.Test; begin if SomeCondition then begin var Qry := 'select * from sometable'; Check(Qry); // Breakpoint: S shows 'A' - Output in check = 'A' end; {..} if SomeOtherCondition then begin var Qry := 'select * from sometable where condition'; Check(Qry); // Breakpoint: S shows 'A' - should have been 'C' - Output in check = 'C' end; {..} while SomeCondition do begin var Qry := SomeFunction( SomeParameter ); Check(Qry); end; end; I'm not saying this construction is good or not, but if the new feature allows it, should work as advertised! Don't have much hair left to pull out. 4 Share this post Link to post