[a119] #Warn Unreachable detection failure Topic is solved

Report problems with documented functionality
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

[a119] #Warn Unreachable detection failure

Post by swagfag » 06 Aug 2020, 12:11

minimal example

Code: Select all

#Requires AutoHotkey v2.0-a119-179d27fd
#Warn Unreachable

{
	return
}

MsgBox ; unreachable
real-world example: see https://www.autohotkey.com/boards/viewtopic.php?f=82&t=79521&p=346012#warn_fail
Unreachable: wrote:Before the script starts to run, show a warning for each line that immediately follows a Return, Break, Continue, Throw or Goto at the same nesting level, unless that line is the target of a label.
despite the braces, its at the same nesting level
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a119] #Warn Unreachable detection failure

Post by Helgef » 06 Aug 2020, 12:58

I didn't see this before I posted in the other thread. It's not a bug. But I wouldn't use blocks like this to circumvent the warning.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [a119] #Warn Unreachable detection failure  Topic is solved

Post by lexikos » 08 Aug 2020, 20:06

return is nested inside the block. It is not at the same nesting level as MsgBox.

For one, if it was at the same nesting level, you could do this:

Code: Select all

goto lbl
{
lbl:
MsgBox
}
A control flow statement such as If or Loop takes a single statement as its body. You can use an entire block because the content of the block (and its block-end) is nested under the block-begin, allowing the overall block to act as a compound statement. Lines are grouped within blocks this way regardless of what happens to be above the block.

Also, the warning is shown "for each line that immediately follows a Return [etc.]". In your example, the line immediately following return is }, but common sense says it must be permitted. (In this case common sense might say to not use a block at all, but if there's a warning, it should be about that and not } being unreachable.) The exception is that function definitions are skipped; otherwise, only the line immediately after the return is considered.

Also, "this does not detect all unreachable code".

The detection is intentionally simplistic, to minimize code size and complexity and the risk of showing warnings for legitimate code. All block-ends are treated the same, without regard to where the corresponding block-begin is or what is above it.

Compilers of other languages would tend to eliminate the braces during compilation, but that doesn't happen here, as you can see by checking ListLines. Those compilers do a lot more work (for other reasons than detecting unreachable code), breaking the code into smaller units and analyzing each code path.
Post Reply

Return to “Bug Reports”