nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.54k stars 1.47k forks source link

`nim check` doesn't report some compiler errors (eg: static array bound check) #9906

Open timotheecour opened 5 years ago

timotheecour commented 5 years ago
#[
KEY nim check

`nim check` doesn't report some compiler errors

nim check $timn_D/bugs/all/t0053.nim
passes (BUG!)

nim c -o:/tmp/z01 $timn_D/bugs/all/t0053.nim
t0053.nim(10, 6) Error: index out of bounds

## note: with debug info,
$nim_dev_debug_X c -o:/tmp/z01 $timn_D/bugs/all/t0053.nim
gives:
Traceback (most recent call last)
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(109) nim
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(73) handleCmdLine
/Users/timothee/git_clone/nim/Nim/compiler/cmdlinehelper.nim(91) loadConfigsAndRunMainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(168) mainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(78) commandCompileToC
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(134) compileProject
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(79) compileModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(194) processModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(86) processTopLevelStmt
/Users/timothee/git_clone/nim/Nim/compiler/cgen.nim(1487) myProcess
/Users/timothee/git_clone/nim/Nim/compiler/ccgstmts.nim(1186) genStmts
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2408) expr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2201) genStmtList
/Users/timothee/git_clone/nim/Nim/compiler/ccgstmts.nim(1186) genStmts
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2370) expr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2039) genMagicExpr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(1020) genEcho
/Users/timothee/git_clone/nim/Nim/compiler/cgen.nim(504) initLocExpr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2390) expr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2166) genArrayConstr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2376) expr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(1987) genMagicExpr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(1538) genDollar
/Users/timothee/git_clone/nim/Nim/compiler/cgen.nim(504) initLocExpr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(2402) expr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(951) genBracketExpr
/Users/timothee/git_clone/nim/Nim/compiler/ccgexprs.nim(878) genArrayElem
/Users/timothee/git_clone/nim/Nim/compiler/msgs.nim(522) localError
/Users/timothee/git_clone/nim/Nim/compiler/msgs.nim(504) liMessage
/Users/timothee/git_clone/nim/Nim/compiler/msgs.nim(332) handleError
/Users/timothee/git_clone/nim/Nim/compiler/msgs.nim(317) quit
]#

let a = [1,2]
echo a[3]

why this is annoying

Araq commented 5 years ago

That's what you get when the backend is allowed to report errors. You would have written it the same way because of "DRY".

timotheecour commented 5 years ago

@araq follow up from https://github.com/nim-lang/Nim/pull/9916#issuecomment-445839902 :

IMO system.compiles's spec should be "type supports operation" as well as "is importable module", nothing more

so compiles("[0,1][10]") returning true is intended behavior ? docs could be a bit clarified regarding that aspect (from user POV, it's not clear that it's a backend vs semantic error)

in any case, I agree with your point thatcompiles(s) shouldn't call into backend ; and also should be kept as efficient as possible, since otherwise it could slow down compilation for some programming patterns (eg toSeq)

Araq commented 5 years ago

compiles("[0,1][10]") is an edge case that could return either. But in theory array indexing is a "supported operation" so true fits the bill.