crust icon indicating copy to clipboard operation
crust copied to clipboard

Wrong output when doing addition on short

Open bungcip opened this issue 7 years ago • 3 comments

#include <iostream>
using namespace std;

int main() {
    
    short a = 32767;
    short b = 32767;
    int c = a + b;
    cout<<c;
}

Current crust will produce something like this

...
 fn main ( ) {
   ...
   let mut a : i16 = 32767 ; let mut b : i16 = 32767 ; 
   let mut c : i32 = a+b ;  // ~~> wrong
   ...
 }

Its seems crust don't handle c integer promotion from short to int.

bungcip avatar May 23 '17 21:05 bungcip

Hi It require you to add cast manually.

NishanthSpShetty avatar May 24 '17 07:05 NishanthSpShetty

Hi @NishanthSpShetty, Ideally crust will be automatically handling the cast, I know this issue will be tough to solve in current crust state. It will require some semantic analysis on the C code. However crust will need semantic & flow control analysis if you plan to support goto

bungcip avatar May 24 '17 08:05 bungcip

@bungcip Yeah..you are correct. Semantic analysis and flow controller analysis required, which may solve lot of issues.

NishanthSpShetty avatar May 24 '17 09:05 NishanthSpShetty