stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
140 stars 44 forks source link

Consider a return statement in an otherwise infinite loop as 'complete' #1340

Closed WardBrian closed 1 year ago

WardBrian commented 1 year ago

This fixes an issue that @bob-carpenter pointed out in (at least) October 2019 on the forums where a function with an infinite loop which obviously is the only way out of the function still requires a dummy return statement after the loop to typecheck.

The solution here is pretty narrow: loops are considered infinite if and only if they have a nonzero integer literal as their loop expression. If a loop is infinite, we treat what would normally be considered an "incomplete" return as a full one.

Submission Checklist

Release notes

Hard-coded infinite loops like while (1) which contain a return statement are now considered valid without needing to put a dummy return statement afterwards.

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

codecov[bot] commented 1 year ago

Codecov Report

Merging #1340 (1e95c9e) into master (7ac5e3a) will increase coverage by 0.01%. The diff coverage is 100.00%.

:exclamation: Current head 1e95c9e differs from pull request most recent head 2615bab. Consider uploading reports for the commit 2615bab to get more accurate results

@@            Coverage Diff             @@
##           master    #1340      +/-   ##
==========================================
+ Coverage   89.18%   89.20%   +0.01%     
==========================================
  Files          64       64              
  Lines       10606    10612       +6     
==========================================
+ Hits         9459     9466       +7     
+ Misses       1147     1146       -1     
Files Changed Coverage Δ
src/frontend/Typechecker.ml 89.81% <100.00%> (+0.16%) :arrow_up:
WardBrian commented 1 year ago

Good catch. That's much more involved to check. Not impossible, but I'm not sure if it's worth it relative to the requirement that you put a dummy return (I think these cases are relatively few and far between, even the original example could have been written in such a way that the loop terminated and then returned)

WardBrian commented 1 year ago

I ended up moving the resolution of this into #1341