-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
echo -0x80'i8
prints 128 (which is out of range) and other bugs
#18422
Comments
echo -0x80'i8
prints 128 (which is out of range) and other bugs
Slightly related: #4350 What Go/Rust does is just like package main
import (
"fmt"
"strconv"
)
func main() {
j, err := strconv.ParseInt("-80", 16, 8)
fmt.Println(j)
fmt.Println(err)
fmt.Println(int8(-0x80)) // 128
fmt.Println(int8(0x80)) // overflow
fmt.Println(-0x8000000000000000)
fmt.Println(0x8000000000000000) // constant 9223372036854775808 overflows int
fmt.Println(0x8000000000000001) // ./prog.go:18:14: constant 9223372036854775809 overflows int
} Rust pub fn main() {
println!("num: {}", 0x80i8);
} error message:
Maybe the lexer can work like Expected
Pros
Related
>>> import strformat
>>> fmt"{-128:x}"
-80 |
Here's how we can solve this without breaking code: as suggested here: nim-lang/RFCs#364 (comment) |
Example
ditto with
-0o200'i8
Current Output
Expected Output
-128
-128
bugs:
CT error for
-0x80'i8
and similar literalsPossible Solution
1.5.1 3ceaf5c
-128'i8
is useful and it's good we now support it.-0x80'i8
is an outright bug and that's the subject of this issue, the lexer should reject it as -128 doesn't have a negative that fits as int128; and as seen above leads to inconsistencies-0x81'i8
is questionable as it allows 2 writings for the same number,0x7f'i8
and-0x81'i8
proposal
make parser give CT error for literals (hex,oct,binary) with a minus sign that result in a >=0 number: these would become CT errors; they're error prone and serve no purpose:
The text was updated successfully, but these errors were encountered: