Bug 20179 – 'goto' skips over declaration triggered for unused variable

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2019-08-29T03:11:02Z
Last change time
2024-12-13T19:05:17Z
Assigned to
No Owner
Creator
Mathias LANG
Moved to GitHub: dmd#19616 →

Comments

Comment #0 by pro.mathias.lang — 2019-08-29T03:11:02Z
The following code: ``` string foo () { return null; } void main () { if (auto str = foo()) { assert(str.length); L1: } else goto L1; } ``` Produces "oa.d(10): Error: goto skips declaration of variable oa.main.str at oa.d(5)" But the variable is not used after L1.
Comment #1 by razvan.nitu1305 — 2023-04-18T11:06:29Z
The compiler does not do any dataflow analysis so it cannot know whether `str` is used or not below L1. However, `str` could be used, therefore the compiler is conservative in this case and errors. In most cases, I think that the error could actually prevent wrong code. Anyway, if last use analysis is implemented for move constructors/perfect fordwarding maybe this will have a chance of being fixed, however, chances are slim.
Comment #2 by robert.schadek — 2024-12-13T19:05:17Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19616 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB