diff --git a/semen.scm b/semen.scm index 74cfc83..5efc7bd 100644 --- a/semen.scm +++ b/semen.scm @@ -1351,11 +1351,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) diff --git a/tests/codegen.scm b/tests/codegen.scm index e7fa82c..0f40d62 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -632,6 +632,13 @@ "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"