_ mistypes or refuses a few expressions #68

Open
opened 2026-10-19 14:09:42 +02:00 by alex-eg · 0 comments
Owner

_ takes the type C gives an expression. A few expressions still come out with a type C would not give them, or with none:

  • (+ a b) with a : (unsigned long) and b : (long long) is typed long long. On LP64 hosts C makes it unsigned long long: a signed type of higher rank wins only when it can hold every value of the unsigned one.

    (var x _ (+ a b))   ->   long long x = a + b;
    
  • (+ d 1) with d : (enum color) stays enum color. C promotes an enum operand, and the sum is an int. (An enum constant is typed as its enum on purpose, so that its ops are found through it; arithmetic on one is another matter.)

  • #(1 2.5) for [_ 2] is refused, type mismatch: expected: int got double, where the elements could join to double the way an operator's operands do.

  • (var t [_ 8] "abc") is refused, type mismatch: expected: (¤ _ 8) got (* const char): a string literal is typed as the pointer it decays to, and a char array set from one is C's own idiom.

  • In a block inside a function whose x is a long, (var x _ (+ x 1)) is typed from the outer x. C's initializer reads the new one, uninitialized:

    long x = x + 1;
    

    That is a C pitfall more than a typing one, but _ makes it easy to write. Sex could refuse an initializer that names the variable it declares.

  • (var q (const * _) (& k)) with k : int emits correct C, int * const q = &k;, but warns qualifiers differ between (const * _) and (* int). The top-level const is the object's own, and setting it from a non-const pointer is fine.


Found with sexc 35604ad, probing corner cases the test suite does not cover; the rest of what that turned up is fixed on more-tests-and-corner-cases.

`_` takes the type C gives an expression. A few expressions still come out with a type C would not give them, or with none: - `(+ a b)` with `a : (unsigned long)` and `b : (long long)` is typed `long long`. On LP64 hosts C makes it `unsigned long long`: a signed type of higher rank wins only when it can hold every value of the unsigned one. (var x _ (+ a b)) -> long long x = a + b; - `(+ d 1)` with `d : (enum color)` stays `enum color`. C promotes an enum operand, and the sum is an `int`. (An enum *constant* is typed as its enum on purpose, so that its ops are found through it; arithmetic on one is another matter.) - `#(1 2.5)` for `[_ 2]` is refused, `type mismatch: expected: int got double`, where the elements could join to `double` the way an operator's operands do. - `(var t [_ 8] "abc")` is refused, `type mismatch: expected: (¤ _ 8) got (* const char)`: a string literal is typed as the pointer it decays to, and a `char` array set from one is C's own idiom. - In a block inside a function whose `x` is a `long`, `(var x _ (+ x 1))` is typed from the outer `x`. C's initializer reads the new one, uninitialized: long x = x + 1; That is a C pitfall more than a typing one, but `_` makes it easy to write. Sex could refuse an initializer that names the variable it declares. - `(var q (const * _) (& k))` with `k : int` emits correct C, `int * const q = &k;`, but warns `qualifiers differ between (const * _) and (* int)`. The top-level `const` is the object's own, and setting it from a non-const pointer is fine. -------- Found with sexc 35604ad, probing corner cases the test suite does not cover; the rest of what that turned up is fixed on `more-tests-and-corner-cases`.
alex-eg added the bug label 2026-10-19 14:09:42 +02:00
Sign in to join this conversation.