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 52c53c851b
commit f304d92b8d
2 changed files with 14 additions and 4 deletions

View File

@@ -1359,11 +1359,14 @@
;;; (+ (& p) 1) -> (* (struct point)) ;;; (+ (& p) 1) -> (* (struct point))
;;; ;;;
;;; `unsigned int' against `int' answers `int', where C says otherwise. ;;; `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) (define (arithmetic-type expr env)
(fold (lambda (operand joined) (if (null? (cddr expr))
(arith-join joined (expression-type operand env))) (promoted-type (expression-type (second expr) env))
#f (fold (lambda (operand joined)
(cdr expr))) (arith-join joined (expression-type operand env)))
#f
(cdr expr))))
;;; A pointer, or an array that is one as an operand ;;; A pointer, or an array that is one as an operand
(define (address? written) (define (address? written)

View File

@@ -689,6 +689,13 @@ whitespace aside, THEN?"
"ptrdiff_t d = p - xs")) "ptrdiff_t d = p - xs"))
(test-assert "a number from a pointer is still a pointer" (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")) (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 ;; a compound literal carries its own type, where a brace
;; initializer has none and takes one from its context ;; initializer has none and takes one from its context
(test-assert "a compound literal answers a bare wildcard" (test-assert "a compound literal answers a bare wildcard"