swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.33k stars 10.34k forks source link

[SR-1214] Illegal Instruction exception in REPL and Playground for UInt8 conversion to negative Int #43822

Open swift-ci opened 8 years ago

swift-ci commented 8 years ago
Previous ID SR-1214
Radar None
Original Reporter pbohrer (JIRA User)
Type Improvement
Status Reopened
Resolution
Environment Swift REPL. Testing on Linux for 3.0 (mar 1 & feb 25) and 2.2 (Jan 6 & 11) as well as Playground 7.3
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Standard Library | |Labels | Improvement | |Assignee | None | |Priority | Medium | md5: 12a7140ba20384c7b70ed37310ef2530

Issue Description:

The following code causes an illegal instruction in the swift REPL.

Either load this code into 7.3 playground or go to the Swift Sandbox:
http://swiftlang.ng.bluemix.net/#/repl/2dde02db5d77c12fc2c8ef841e79a1679493b4e83cdde459ffcc635fc99b5e44

let char : UInt8? = 46

// This statement causes a Illegal instruction exception in the Swift REPL
let newValue = Int(char! - 48)

// However, the following works
// let newValue = Int(Int8(char!) - 48)
print (newValue)

belkadan commented 8 years ago

This is correct behavior. Int(char! - 48) evaluates the subtraction in UInt8-space, not in Int-space, and UInts can't represent negative numbers.

belkadan commented 8 years ago

It would definitely be nice to get a proper error message for this, though.

swift-ci commented 8 years ago

Comment by Patrick Bohrer (JIRA)

I agree it was bad code but it took a while to figure out the error based on the dump/stack trace.

swift-ci commented 8 years ago

Comment by Patrick Bohrer (JIRA)

This was the error I got which did not help me figure out what I had done in the larger program:
Error running code:
0 swift 0x0000000002f4d598 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1 swift 0x0000000002f4bd96 llvm::sys::RunSignalHandlers() + 54
2 swift 0x0000000002f4e0c6
3 libpthread.so.0 0x00007f52efaf9d10
4 libpthread.so.0 0x00007f52f0998142
5 swift 0x0000000000d0f084 llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef\<llvm::GenericValue>) + 996
6 swift 0x0000000000d1268f llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::vector\<std::*cxx11::basic_string\<char, std::char_traits\<char>, std::allocator\<char> >, std::allocator\<std::_cxx11::basic_string\<char, std::char_traits\<char>, std::allocator\<char> > > > const&, char const* const*) + 1263
7 swift 0x0000000000bedbf8 swift::RunImmediately(swift::CompilerInstance&, std::vector\<std::cxx11::basic_string\<char, std::char_traits\<char>, std::allocator\<char> >, std::allocator\<std::_cxx11::basic_string\<char, std::char_traits\<char>, std::allocator\<char> > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 2312
8 swift 0x0000000000762a98 frontend_main(llvm::ArrayRef\<char const*>, char const*, void\
) + 7816
9 swift 0x000000000075cd43 main + 2835
10 libc.so.6 0x00007f52eee9ea40 __libc_start_main + 240
11 swift 0x000000000075c129 _start + 41
Stack dump:

  1. Program arguments: /usr/bin/swift -frontend -interpret /swift-execution/code-tmp.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -module-name main -lFoundation -lETSocket
    /usr/bin/doit.sh: line 19: 8 Illegal instruction timeout 5 swift -lFoundation -lETSocket -v /swift-execution/$fileroot
    Source Code
    Clear Code
    Settings