[swift-evolution] Disallowing unreachable code

John McCall rjmccall at apple.com
Mon Mar 27 13:27:43 CDT 2017


> On Mar 27, 2017, at 1:48 PM, Peter Dillinger <Peter.Dillinger at synopsys.com> wrote:
> 
>> To use a germane example, if unreachable code were always an error, a programmer trying to
>> debug a problem wouldn't be able to short-circuit a function by just adding a return;
> 
> True, but
> 
>> they'd also have to comment out the rest of the function.
> 
> No, as you can do
> 
>  if (true) {
>    return -1
>  }
> 
> See clarifying reply on definition of "unreachable".

I'm not claiming that it's impossible to suppress the error.  I'm saying that we made a conscious decision to not use hard errors for issues that merely *might* indicate a semantic mistake because doing so can interfere with the realities of development.

Note that such interference can be actively counter-productive.  If a diagnostic is a warning, the programmer can safely put it out of mind because the warning will stick around until they fix it.  If the diagnostic is instead an error, they'll have to immediately suppress it in order to get on with the build; that suppression mechanism will remain in place indefinitely until they come back and fix it.

In fact, we should probably double-down on this design and add an "unimplemented" expression that always triggers warnings and just traps if you try to evaluate it.  That expression would be a logical thing to use in e.g. code snippets automatically inserted by an IDE.

John.

> Returning to Charlie's reply:
>> Similarly returning earlier. The warning is convenient as it warns you to review that part
>> before releasing the code to the public.
> 
> Since swift implements a warning for the if(true) or if(false) cases, we still get that, and without the cost of accepting code with a serious chance of being accidentally wrong.  I don't think anyone ever wrote if(true) or if(false) expecting some behavior other than what they wrote.
> 
> -- 
> Peter Dillinger, Ph.D.
> Software Engineering Manager, Coverity Analysis, Software Integrity Group | Synopsys
> www.synopsys.com/software
> 



More information about the swift-evolution mailing list