Recent

Author Topic: Not allow “do;“  (Read 5037 times)

Kraig

  • New Member
  • *
  • Posts: 26
Not allow “do;“
« on: September 01, 2023, 05:25:13 pm »
I just spent a lot of time trying to figure out why the statement with flowpanel do was not working right, then I noticed a stray “;” after the do and removed it and now it works.
Having a semicolon after do seems to have the same effect as commenting out the entire statement containing the do.

I’d like to suggest that putting a semicolon after do should generate a compile error or at least a warning about the fact that the code is useless and will never have any affect on the statement that follows it.  :o

It would also be nice if the semicolon was not automatically placed after the do in the auto completion of Lazarus ide.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9990
  • Debugger - SynEdit - and more
    • wiki
Re: Not allow “do;“
« Reply #1 on: September 01, 2023, 05:30:39 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 14615
  • Sensorship about opinions does not belong here.
Re: Not allow “do;“
« Reply #2 on: September 01, 2023, 06:06:56 pm »
I just spent a lot of time trying to figure out why the statement with flowpanel do was not working right, then I noticed a stray “;” after the do and removed it and now it works.
Stray? Of course not. That is simply programmer error. This does not warrant a warning or hint because the "stray" ; can be intentional.
And that happens very often...Intentional. (e.g. case/else)
Learn the language first. The ; simply means any code after that is not part of the evaluation.
« Last Edit: September 01, 2023, 06:12:59 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

440bx

  • Hero Member
  • *****
  • Posts: 4198
Re: Not allow “do;“
« Reply #3 on: September 01, 2023, 11:08:00 pm »
Having a semicolon after do seems to have the same effect as commenting out the entire statement containing the do.
It doesn't comment out the entire statement.  The problem is that without the statement being in a "begin"/"end" pair then the "do" executes an empty statement (which is what the ";" indicates -> an empty statement.)

I’d like to suggest that putting a semicolon after do should generate a compile error or at least a warning about the fact that the code is useless and will never have any affect on the statement that follows it.  :o
It would be incorrect for the compiler to generate an error because the construction "while <someboolean> do ;" is perfectly valid and sometimes _useful_.  The case in which it may be useful is when the "while" expression may be changed by code that executes independently of the "while", for instance code in another thread that may be part of the program or the O/S.  In that case the <expression> acts as a barrier to the execution of the code that follows it.

Considering that it is a somewhat uncommon construction, it might be reasonable for the compiler to emit a hint in that case but, it's not a really easy thing for the compiler to do because the hint would be valid only when the expression is entirely made of local variables (since those cannot be changed by external code, therefore the "while <expression> do ;" may effectively be an infinite loop (if <expression> happens to be true.)

IMO, while it would be nice to get a hint from the compiler, I would be inclined to leave the responsibility entirely on the programmer's shoulders (unless it happens to be easy for the compiler to correctly detect the case and emit the hint only when it is appropriate.)

I think @Martin_fr's suggestion to have the IDE highlight the construction is an excellent substitute for the hint you are requesting.

It would also be nice if the semicolon was not automatically placed after the do in the auto completion of Lazarus ide.
If you have the IDE do auto completion for you, you have to accept that there will be times when the IDE will "complete" something in a way that is not quite the way you want.  It's a downside that comes with the territory.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Kraig

  • New Member
  • *
  • Posts: 26
Re: Not allow “do;“
« Reply #4 on: September 02, 2023, 01:30:31 am »
Thanks for the answers

If I had put the “;” in there I would feel more responsible and yes it could be said that it’s my fault. However it was put there for me by the code completion which is otherwise a good feature.

Is there any way to improve  the code completion to not put a semicolon everytime I have a do statement?
I don’t understand how the code completion works but surely there is a way to check if it involves a “do” and forgo the semicolon if it does. Rare instances that require a semicolon could add it. It’s much easier to add on rare occasions than remove on all occasions.

Having a choice of either no code completion or having to manually remove a semicolon that will break my code everytime I use a “do” is no fun.

ASerge

  • Hero Member
  • *****
  • Posts: 2262
Re: Not allow “do;“
« Reply #5 on: September 02, 2023, 08:01:56 am »
It would also be nice if the semicolon was not automatically placed after the do in the auto completion of Lazarus ide.
My IDE doesn't put a semicolon automatically after do, only after end.
From lazarus.dci (empty lines added by me for better visibility):
Code: XML  [Select][+][-]
  1. ...
  2. [whileb | while statement]
  3. $(AttributesStart)
  4. EnableMakros=true
  5. RemoveChar=true
  6. $(AttributesEnd)
  7. while $Param(LoopCondition) do
  8. begin
  9.   |
  10. end$AddSemicolon()
  11.  
  12. [whiles | while (no begin)]
  13. while $Param(LoopCondition) do
  14.   |
  15.  
  16. [withb | with statement]
  17. ...

MarkMLl

  • Hero Member
  • *****
  • Posts: 6754
Re: Not allow “do;“
« Reply #6 on: September 02, 2023, 09:39:19 am »
If I had put the “;” in there I would feel more responsible and yes it could be said that it’s my fault. However it was put there for me by the code completion which is otherwise a good feature.

While few would be as gratuitously rude as Thaddy, it boils down to the fact that the checking of your code is your responsibility.

A Semicolon after do is valid:

Code: Pascal  [Select][+][-]
  1. while somethingWithSideEffects() do ;
  2.  

although many would argue that it's bad style. Conversely, there are places where a semicolon should not be inserted (e.g. immediately before else) and places where Wirth's original definition of Pascal did not intend it to be inserted (e.g. immediately before end).

The rule is that ; is a statement /separator/ not a /terminator/, so all  do ;  does is iterate an empty statement.

Having said which, I sympathise if this really is something which was inserted by the IDE's code completion, although I've not seen that behaviour (and often disable that sort of thing anyway as intrusive and distracting). I'd suggest that you'd make life much easier for everybody if, having found something questionable like that, you explicitly said what version of Lazarus and what platform you're using just in case it's an "erratum" which has got in accidentally.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9990
  • Debugger - SynEdit - and more
    • wiki
Re: Not allow “do;“
« Reply #7 on: September 02, 2023, 09:58:33 am »
You can switch of the ";" entirely

Menu: Tools > Options:
Page: Codetools > Identifier Completion
Checkbox "Add semicolon"

But then it won't add a semicolon on any completion at all.



Making is smarter, so it won't do it after "do" or "then" ... => only by changing the codetools implementation.

alpine

  • Hero Member
  • *****
  • Posts: 1111
Re: Not allow “do;“
« Reply #8 on: September 02, 2023, 10:55:08 am »
I think @Martin_fr's suggestion to have the IDE highlight the construction is an excellent substitute for the hint you are requesting.
I completely agree.

With autocomplete putting ';' after 'do' there is a risk to forget to insert the actual statement, with no putting ';' there is an equal risk to forget and the loop will execute the statement that follows. Where is the risk mitigation then?

When I want to put an empty statement, I usually put a comment before the ';' to indicate that the empty statement was intentional:
Code: Pascal  [Select][+][-]
  1.   while not Reply() and not Timeout() do
  2.     {wait a reply for a specified timeout};
Or at least:
Code: Pascal  [Select][+][-]
  1.   while some_condition do
  2.     {empty};

Then using the Martin_fr highlight trick is perfectly sufficient.
   

"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9990
  • Debugger - SynEdit - and more
    • wiki
Re: Not allow “do;“
« Reply #9 on: September 02, 2023, 11:06:35 am »
Actually, the problem IMHO is
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.   FooBar: boolean;
  4. begin
  5.   while Fo
  6. end.
  7.  

Completing "FooBar" in the above inserts a semicolon "while FooBar;". That is even before there is a "do".

That seems to be just wrong to me. And for me, it is that semicolon, that then gets pushed forward, until it is after the "do".

I don't know if there is already an issue about it... But currently there is no fix for that part.



Kraig

  • New Member
  • *
  • Posts: 26
Re: Not allow “do;“
« Reply #10 on: September 02, 2023, 03:00:31 pm »
You can switch of the ";" entirely

Menu: Tools > Options:
Page: Codetools > Identifier Completion
Checkbox "Add semicolon"

But then it won't add a semicolon on any completion at all.



Making is smarter, so it won't do it after "do" or "then" ... => only by changing the codetools implementation.

I know nothing about how code tools work so I’m assuming that it would some how be very difficult to fix this problem?
I’m using fpc 3.2.0 lazarus 2.0.12 on windows in case the version I’m using has an effect.

The problem I have with this bug is that it’s very subtle and I compiles Just fine but introduces runtime errors that can be difficult to track down.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9990
  • Debugger - SynEdit - and more
    • wiki
Re: Not allow “do;“
« Reply #11 on: September 02, 2023, 03:31:57 pm »
Making is smarter, so it won't do it after "do" or "then" ... => only by changing the codetools implementation.

I know nothing about how code tools work so I’m assuming that it would some how be very difficult to fix this problem?
I’m using fpc 3.2.0 lazarus 2.0.12 on windows in case the version I’m using has an effect.

The problem I have with this bug is that it’s very subtle and I compiles Just fine but introduces runtime errors that can be difficult to track down.

Yes, it requires deeper knowledge of the IDE's code. Not trivial.

Your 2 options at the moment are:
- disable the semicolon for auto-completion entirely (see steps in my prior post)
- setup coloring for it.


I recommend the coloring in any case.  You can setup colors for
- common typos
- unlikely code (like "do ;"
- flow control like "break", "continue" if you want them to stand out.


I also recommend
https://wiki.freepascal.org/New_IDE_features_since#Outline_for_Pascal_Sources

Which helps finding unaligned begin/end. (you can fine tune the colors and style).

MarkMLl

  • Hero Member
  • *****
  • Posts: 6754
Re: Not allow “do;“
« Reply #12 on: September 03, 2023, 09:47:46 am »
...and in any event finding spurious occurrences of  do ;  is easy enough with the "search all files" facility.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kraig

  • New Member
  • *
  • Posts: 26
Re: Not allow “do;“
« Reply #13 on: September 03, 2023, 11:05:56 am »
I wouldn’t mind having a peek at the snippet of code that adds the “;”.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9990
  • Debugger - SynEdit - and more
    • wiki
Re: Not allow “do;“
« Reply #14 on: September 03, 2023, 12:07:07 pm »
I wouldn’t mind having a peek at the snippet of code that adds the “;”.

components\codetools\identcompletiontool.pas

As well as ide\SourceEditor.pp  line 2630
procedure TSourceEditCompletion.ccComplete(var Value: string;
...
            // get value
            NewValue:=GetIdentCompletionValue(Self, KeyChar, ValueType, CursorToLeft);
            if ValueType=icvIdentifier then ;
            // insert value plus special chars like brackets, semicolons, ...
            if ValueType <> icvNone then
              Editor.TextBetweenPointsEx[SourceStart, SourceEnd, scamEnd] := NewValue;


Look at GetIdentCompletionValue ?

 

TinyPortal © 2005-2018