timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

nim needs a way to find deadcode #245

Open timotheecour opened 4 years ago

timotheecour commented 4 years ago

eg: in io.nim:

proc writeLn[Ty](f: File, x: varargs[Ty, `$`]) =
  for i in items(x):
    write(f, i)
  write(f, "\n")

the tool should be robust to A calling B and A being deadcode, or to cycles of deadcode

it should be smart wrt conditional compilation

ringabout commented 3 years ago

Related: https://github.com/jyapayne/mort

timotheecour commented 3 years ago

thanks for the pointer; I think this needs compiler support though. mort requires users to modify their source code in a pretty invasive way.

I think https://github.com/nim-lang/Nim/pull/15827 (which doesn't require modifying sources) can be used as basis to solve problem A, but not problem B:

proc fn1(n: int)
proc fn2(n: int) =
  if n>0: fn1(n-1)
proc fn1(n: int) =
  if n>0: fn2(n-1)

proc main() = discard
main()

nim's DCE correctly cgen's main and doesn't cgen fn1,fn2 (not reachable by main even though both are being called by each other in their own cycle), which is good. But it doesn't provide a way to identify that fn1, fn2 are (statically) un-reachable in this program.