You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionfail(): never{thrownewError('oops');}functiontest1(){letstr: string;fail();str='helloworld';// No error. Should error with unreachable code.}functiontest2(){letstr: string;if(Math.random()>0.5){str='helloworld';}else{fail();}letstr2=str+'';// Error: str is used before being assigned. But should not error.}
Troy Gerwien (@yortus) your example which Anders Hejlsberg (@ahejlsberg) commented on is slightly different then this case. That example is for inferred 'never' typing for functions. In my example I use a declared 'never' function.
functiontest2(){letstr: string;if(Math.random()>0.5){str='helloworld';}else{fail();}letstr2=str+'';// Error: str is used before being assigned. But should not error.}
Because this one already are:
functiontest2(){letstr: string;if(Math.random()>0.5){str='helloworld';}else{thrownewError('oops');}letstr2=str+'';// No error}
Tingan Ho (@tinganho) First of all, I would prefer the behaviour you are suggesting here (i.e. detect the unreachable code and error).
That aside, I don't think the compiler currently does any cross-function analysis, so it doesn't assume the code after calling fail() is unreachable. So I suspect Anders Hejlsberg (@ahejlsberg)'s comment does apply to your example as well. It would be great if he could clarify it himself.
To clarify what I said here, reachability analysis considers only the grammatical structure of the code, i.e. it doesn't factor in types. So, we know for certain that control won't reach a statement following a throw or return, but we don't know for certain that a function that promises to never return won't actually return. The distinction is important when you for example consider that JavaScript code might violate type annotations by passing parameters or returning values that don't conform to the annotated types.
That said, I agree it would be nice if we could report reachability errors following calls to never-returning functions, be they implicitly or explicitly typed. However, it's not a trivial change given how the analysis is currently structured. Meanwhile, you can make it work by writing return fail() instead of just fail():
I think I'm starting to see a pattern here: never functions must be called in places where they can never be reached. Called elsewhere is not their intentions.
Activity
yortus commentedon Sep 18, 2016
I raised this point in #9260 and Anders Hejlsberg (@ahejlsberg) responded in #9260 (comment) (see second half of comment).
tinganho commentedon Sep 18, 2016
Troy Gerwien (@yortus) your example which Anders Hejlsberg (@ahejlsberg) commented on is slightly different then this case. That example is for inferred 'never' typing for functions. In my example I use a declared 'never' function.
Just quoting Anders Hejlsberg (@ahejlsberg) :
Maybe inferred cases cannot reach 100%. I don't know. But for me, this example is 100%:
Because it is nearly exactly the same as this one:
This is also 100%:
Because this one already are:
yortus commentedon Sep 18, 2016
Tingan Ho (@tinganho) First of all, I would prefer the behaviour you are suggesting here (i.e. detect the unreachable code and error).
That aside, I don't think the compiler currently does any cross-function analysis, so it doesn't assume the code after calling
fail()is unreachable. So I suspect Anders Hejlsberg (@ahejlsberg)'s comment does apply to your example as well. It would be great if he could clarify it himself.ahejlsberg commentedon Sep 18, 2016
To clarify what I said here, reachability analysis considers only the grammatical structure of the code, i.e. it doesn't factor in types. So, we know for certain that control won't reach a statement following a
throworreturn, but we don't know for certain that a function that promises to never return won't actually return. The distinction is important when you for example consider that JavaScript code might violate type annotations by passing parameters or returning values that don't conform to the annotated types.That said, I agree it would be nice if we could report reachability errors following calls to never-returning functions, be they implicitly or explicitly typed. However, it's not a trivial change given how the analysis is currently structured. Meanwhile, you can make it work by writing
return fail()instead of justfail():tinganho commentedon Sep 19, 2016
I think I'm starting to see a pattern here:
neverfunctions must be called in places where they can never be reached. Called elsewhere is not their intentions.