j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
178 stars 15 forks source link

"-2147483648" should be a valid expression (again) #260

Closed klausler closed 2 years ago

klausler commented 2 years ago

Assume 32-bit two's-complement as the default kind of integer. It turns out that the expression "-2147483648" is conforming in FORTRAN '77 but at best questionable and at worst nonconforming in Fortran '90 or later.

FORTRAN '77' defined an integer constant (4.3.1) as a signed digit string. Fortran '90's grammar, on the other hand, parses a signed integer literal as the application of a unary minus to an unsigned digit string. In the case of "-2147483648", the integer literal to be negated is not a valid number in the range of the default kind of integer.

References to Fortran 2018: A primary (R1001) can be a literal-constant (R605) which can be an int-literal-constant (R708) which does not allow for a sign. So the minus sign is an add-op (R1009) being used in a level-2-expr (R1006).

Oddly, a complex-literal-constant (R718) can have a signed-int-literal-constant as its real or imaginary parts, so "(-2147483648,-2147483648)" is not a problem in that context. A signed-int-literal-constant may also appear in some other contexts, e.g. DATA statements.