refuse an if used as a value with no else

With nothing for the false case the writer made (if c 1) c || 1, a
boolean; and a value if's branches were walked as statements, so one
nested there got through as well.
This commit is contained in:
2026-10-16 15:25:47 +03:00
parent 2c9fde897f
commit 5b0ca21c35
2 changed files with 19 additions and 4 deletions

View File

@@ -109,7 +109,18 @@
"a ? a : b ? b : 0"))
(test-assert "but an assignment there is not"
(emits? (in-fn "(var x int (if a a (= b 1)))")
"a ? a : (b = 1)")))
"a ? a : (b = 1)"))
;; with nothing for the false case it came out `a || 1', a boolean
(test-assert "a value needs an else"
(reports? (in-fn "(var x int (if a 1))") "an if used as a value needs an else"))
(test-assert "so does a chain"
(reports? (in-fn "(var x int (if a 1 b 2))") "an if used as a value needs an else"))
(test-assert "and an if in a value's branch"
(reports? (in-fn "(var x int (if a 1 (if b 2)))") "an if used as a value needs an else"))
(test-assert "a chain with one is a chain"
(emits? (in-fn "(var x int (if a 1 b 2 3))") "a ? 1 : b ? 2 : 3"))
(test-assert "and a statement needs none"
(emits? (in-fn "(if a (g a))") "if (a)")))
;; A subscript binds tighter than anything, so what it subscripts is
;; parenthesised whenever it is an operator: `*q[i]' is *(q[i])