promote a lone arithmetic operand

One operand was joined with nothing and came back as it was, so (- c)
over a char was a char where C makes it an int.
This commit is contained in:
2026-10-16 17:04:20 +03:00
parent 3b43800ebb
commit c0738ff965
2 changed files with 14 additions and 4 deletions

View File

@@ -1362,11 +1362,14 @@
;;; (+ (& p) 1) -> (* (struct point))
;;;
;;; `unsigned int' against `int' answers `int', where C says otherwise.
;;; One operand is promoted on its own: (- c), c : char -> int
(define (arithmetic-type expr env)
(fold (lambda (operand joined)
(arith-join joined (expression-type operand env)))
#f
(cdr expr)))
(if (null? (cddr expr))
(promoted-type (expression-type (second expr) env))
(fold (lambda (operand joined)
(arith-join joined (expression-type operand env)))
#f
(cdr expr))))
;;; A pointer, or an array that is one as an operand
(define (address? written)

View File

@@ -690,6 +690,13 @@ whitespace aside, THEN?"
"ptrdiff_t d = p - xs"))
(test-assert "a number from a pointer is still a pointer"
(emits? (in-fn "(var p (* int) 0) (var r _ (- p 1))") "int * r = p - 1"))
;; one operand was a join with nothing, which left it unpromoted
(test-assert "a negation promotes its operand"
(emits? (in-fn "(var c char 1) (var n _ (- c))") "int n = -c"))
(test-assert "so does a unary plus"
(emits? (in-fn "(var h short 1) (var n _ (+ h))") "int n = +h"))
(test-assert "and leaves a wide one as it is"
(emits? (in-fn "(var l long 1) (var n _ (- l))") "long n = -l"))
;; a compound literal carries its own type, where a brace
;; initializer has none and takes one from its context
(test-assert "a compound literal answers a bare wildcard"