Re: Proposal: disallow "fall through" to a labeled statement #32565

105 views
Skip to first unread message

Jan Mercl

unread,
Jun 12, 2019, 4:18:03 AM6/12/19
to golang-nuts
Disallowing unintended fall throughs into labeled sections of a block will prevent a class of stupid errors. 

I'm stupid and I know it. Yet, it seems I haven't made this kind of error in the last ten years of Go. Maybe I'm just lucky. Or maybe this kind of error is rare. But if it's rare then a language change preventing it is questionable as the improvements would be also rare.

Are there any statistics available about how many times people run into this class of bugs?

The linked motivation example at https://golang.org/src/cmd/compile/internal/gc/typecheck.go?h=goto+ret#L3800 can be easily refactored to have no gotos and no labels at all. I'd say it's not a typical piece of Go code. I guess it's the original C compiler code translated mechanically to Go, isn't it? BTW: Should I send a CL?

PS: Full disclosure - I think we should mostly keep the language alone. Any tiny change makes the surface of the language a tiny bit larger. But there are _tons_ of proposed language changes at the issue tracker and those tiny changes just add quickly. Also, even though not protected by the compatibility promise, almost every language change breaks an unknown number of tools people wrote in a decade. Only language changes enabling what cannot be done by other means should be considered, IMHO.

Kurtis Rader

unread,
Jun 12, 2019, 1:42:47 PM6/12/19
to golang-nuts
On Wed, Jun 12, 2019 at 1:18 AM Jan Mercl <0xj...@gmail.com> wrote:
Disallowing unintended fall throughs into labeled sections of a block will prevent a class of stupid errors. 

I'm stupid and I know it. Yet, it seems I haven't made this kind of error in the last ten years of Go. Maybe I'm just lucky. Or maybe this kind of error is rare. But if it's rare then a language change preventing it is questionable as the improvements would be also rare.

Are there any statistics available about how many times people run into this class of bugs?

I've been trying to drag the AST/ksh code (written in C) into the 21st century for the past two years. It contains more goto's and associated labels per 1KLOC than any code base I've worked on in the past four decades. I can recall fixing just one bug of this type (i.e., unintended fallthru to a labeled block) in this project. I can't recall dealing with any other instances in the  four decades I've been programming. So my anecdotal experience is that this class of error is exceedingly rare.

Note that this class of bugs is distinct from that caused by C's default of case blocks falling thru to the next case block if you don't explicitly break from the block. A feature that does cause lots of bugs. Fortunately the designers of Go broke from C tradition in this regard.  

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
Reply all
Reply to author
Forward
0 new messages