Duplicate label not detected when defined by a hotkey Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rohwedder
Posts: 7549
Joined: 04 Jun 2014, 08:33
Location: Germany

Duplicate label not detected when defined by a hotkey

Post by Rohwedder » 23 May 2021, 06:11

[Moderator's note: Topic moved from Bug Reports.]

Hallo,
this version produces, as expected, the Error: Duplicate label

Code: Select all

q::Goto, +1
+1::SoundBeep, 4000, 20
+1:
SoundBeep, 1000, 20
Return
but not that one?:

Code: Select all

q::Goto, +1
+1:
SoundBeep, 1000, 20
Return
+1::SoundBeep, 4000, 20
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: where remains the error?

Post by Delta Pythagorean » 23 May 2021, 11:01

Labels and Hotkeys are somewhat the same in AHK. You can use the GoTo or GoSub command to both a Label and a Hotkey and it would work just fine. Therefor, having a Label have the same text as a Hotkey will produce and error.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

Rohwedder
Posts: 7549
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: where remains the error?

Post by Rohwedder » 23 May 2021, 11:06

Exactly this is how it should be!
So why is no error produced with my 2nd script?
lexikos
Posts: 9551
Joined: 30 Sep 2013, 04:07
Contact:

Re: where remains the error?

Post by lexikos » 23 May 2021, 17:05

Hotkeys are permitted to create duplicate labels. If they were not, you would not be able to have more than one variant of +1::.

The documentation does not make any guarantee that duplicate labels will be detected.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: where remains the error?

Post by swagfag » 23 May 2021, 18:02

looks like a very ancient bug(if u can even consider it that, see below) in the parser from way before ahk was even on version control

there are 2 places where labels are parsed: if we make a distinction between hotkey/hotstring labels and normal, regular labels, hotkey labels ignore other duplicate hotkey(and by proxy regular) labels - they have to, in order to support #If hotkey variants
the problem then is that regular labels perform a blanket check as to whether duplicate labels exist(regardless of type) and if so, the parsing fails. consider these 2 scenarios:
  • Code: Select all

    ; (0) parsing starts
    
    +1: ; (1) this is a regular label. does a duplicate hotkey/hotstring/regular name exist? no, add this label
    	MsgBox label
    return
    
    #If true
    +1::MsgBox true ; (2) this is a hotkey label. dont check for duplicate labels, add this label
    
    #If false
    +1::MsgBox false ; (3) this is a hotkey label. dont check for duplicate labels, add this label
  • Code: Select all

    ; (0) parsing starts
    
    #If true
    +1::MsgBox true ; (1) this is a hotkey label. dont check for duplicate labels, add this label
    
    +1: ; (2) this is a regular label. does a duplicate hotkey/hotstring/regular name exist? yes - due to (1), so fail parsing
    	MsgBox label
    return
    
    #If false
    +1::MsgBox false
to fix this ud have to keep track of hotkey labels and regular labels separately and have the regular labels only check in the regular-label-collection for duplicates

so the only thing that "remains" i guess is the question whether its even worth the time investment and effort to track down the cause and implement a fix, making sure not to break other things in the process, considering this behavior has probably been baked in ever since its code was first written and v2 is unaffected by this(since it doesnt permit labels with silly names)
lexikos
Posts: 9551
Joined: 30 Sep 2013, 04:07
Contact:

Re: Duplicate label not detected when defined by a hotkey  Topic is solved

Post by lexikos » 24 May 2021, 05:09

It is not a bug. You can see that hotkeys are specifically designed to allow duplicates - the second parameter of AddLabel is aAllowDupe.

If you add a normal label first, you can goto or gosub the normal label.

If you add a normal label second, it is completely useless. You can't ever refer to it, because the first label (whether a normal label or hotkey) would take precedence.

If you add a hotkey label second, the hotkey itself still works; you just can't use it like a label. That is documented:
Hotkey and hotstring labels are also valid targets for Goto, Gosub and other commands. However, if a hotkey or hotstring has multiple variants, the variant closest to the top of the script is used.
Source: Labels - Syntax & Usage | AutoHotkey
Although the current explanation does not take into account normal labels. The actual and expected behaviour is that when there are duplicate labels, the label first encountered during parsing ("closest to the top of the script") is used. It is possible to create a set of hotkey variants where each variant can be addressed by gosub, such as with ^+a and +^a.


@Rohwedder
When starting a topic, please use a descriptive topic title; one that sets it apart from other topics. "where remains the error?" is as generic and non-descriptive as it gets. I have renamed the topic.
Post Reply

Return to “Ask for Help (v1)”