ASDAlexander77 / TypeScriptCompiler

TypeScript Compiler (by LLVM)
MIT License
588 stars 28 forks source link

Type issues #102

Closed Sinfolke closed 1 month ago

Sinfolke commented 1 month ago

I have tested it recently and found out some issues with types. They seem to be simple so maybe you just didn't went up to fix them

  1. Here it outputs 'a' but cannot the array's elements. When it comes to do it the program waits for up around 5 seconds and exits. When use emit=jit it shows certain error
    let arr: Array<any> = [1, 2, 3, 4];
    function main() {
    let a: any = "Hello, World!";
    print(a);
    for (let i: int = 0; i < arr.length; i++) {
        print(arr[i]);
    }
    }
    
    C:\programming\tsc>tsc --emit=jit
    let arr: Array<any> = [1, 2, 3, 4];
    function main() {
    let a: any = "Hello, World!";
    print(a);
    for (let i: int = 0; i < arr.length; i++) {
        print(arr[i]);
    }
    }
    ^Z
    Hello, World!
    PLEASE submit a bug report to https://github.com/ASDAlexander77/TypeScriptCompiler/issues and include the crash backtrace.Stack dump:
  2. Program arguments: tsc --emit=jit Exception Code: 0xC0000005

    0 0x00007ff9cfa4a6e0 (C:\WINDOWS\System32\ucrtbase.dll+0x5a6e0)

    1 0x00007ff9cfa4a762 (C:\WINDOWS\System32\ucrtbase.dll+0x5a762)

    2 0x0000029e8d94015b

  3. Caused the compiler waite also for around 3-5 seconds and just quit with no messages. It occurs just if insert that line even without main function.
    let arr: Array<number> = "";
ASDAlexander77 commented 1 month ago

I will improve it for "any" type.

try this code:

let arr: any[] = [1, 2, 3, 4];
function main() {
    let a: any = "Hello, World!";
    print(a);
    for (let i: int = 0; i < arr.length; i++) {
        print(<string>arr[i]);
    }
}
ASDAlexander77 commented 1 month ago

I would recommend to write something like the following:

const arr : TypeOf<1>[] = [1, 2, 3, 4];
function main() {
    let a = "Hello, World!";
    print(a);
    for (const i of arr) {
        print(i);
    }
}
ASDAlexander77 commented 1 month ago
  1. Caused the compiler waite also for around 3-5 seconds and just quit with no messages. It occurs just if insert that line even without main function.
let arr: Array<number> = "";

this one is working in my latest git version

Sinfolke commented 1 month ago

IDK

C:\programming\tsc>tsc --version
TypeScript Native Compiler (https://github.com/ASDAlexander77/TypeScriptCompiler):
  TSNC version v0.0-pre-alpha46

LLVM (http://llvm.org/):
  LLVM version 17.0.2
  Optimized build.
  Default target: x86_64-pc-windows-msvc
  Host CPU: alderlake

  Registered Targets:
    aarch64    - AArch64 (little endian)
    aarch64_32 - AArch64 (little endian ILP32)
    aarch64_be - AArch64 (big endian)
    arm        - ARM
    arm64      - ARM64 (little endian)
    arm64_32   - ARM64 (little endian ILP32)
    armeb      - ARM (big endian)
    thumb      - Thumb
    thumbeb    - Thumb (big endian)
    wasm32     - WebAssembly 32-bit
    wasm64     - WebAssembly 64-bit
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64
Sinfolke commented 1 month ago

C:\programming\tsc>tsc --emit=jit let arr: Array = ""; ^Z

C:\programming\tsc>

Sinfolke commented 1 month ago

i used the prebuild release. Possibly you're using own build release and therefore missmatches. And possibly you've already accidentally fixed it. Also you could run on linux while i do on windows.

ASDAlexander77 commented 1 month ago

C:\programming\tsc>tsc --emit=jit let arr: Array = ""; ^Z

C:\programming\tsc>

here is missing

function main() {} 

it should show the error:

tsc: error: JIT invocation failed, error: Symbols not found: [ _mlir_main ]
Program aborted due to an unhandled Error: ...
Sinfolke commented 1 month ago

idk when i add to the release i installed it always aborts without depend what's more is there

Sinfolke commented 1 month ago

and even no matter what type is within Array<>

ASDAlexander77 commented 1 month ago

in other words you can cast "any" type into any but you can't cast from any into other type that type was stored. for example

const v1: number = 1;
const any1: any = v1; // it is OK
const v2: number = any1; // it is still OK
const s1: string = any1; // it is ***NOT OK***;
const s2: string = <string>any1; // it is OK

here is example of unboxing from any:

function __as<T>(a: any) : T
{ 
    if (typeof a == 'number') return <T>a;
    if (typeof a == 'string') return <T>a;
    if (typeof a == 'i32') return <T>a;
    if (typeof a == 'class') if (a instanceof T) return <T>a;
    return <T>null;
}

so u can use it as

const s3: string = __as(any1);
ASDAlexander77 commented 1 month ago

improved code in the latest commit, it will be included in the next release

ASDAlexander77 commented 1 month ago

in the next release unboxing any type from any will be OK.

    let a = 1;
    let any1: any = a;
    let s: string = any1;

    print(s);