ASDAlexander77 / TypeScriptCompiler

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

what is the warning mean? #92

Closed cereschen closed 9 months ago

cereschen commented 9 months ago

This code has no errors, but it still reports this warning. I guess it's related to generics

`:0: warning: types have different sizes: '!llvm.struct<packed (ptr, i1)>' size of #9, '!llvm.struct<packed (ptr, ptr)>' size of #16

:0: warning: using casting to undefined value` ```ts class LocalMap { #storage: Array<[T, R]> = []; get(key: T): R | null { for (let i = 0; i < this.#storage.length; i++) { if (this.#storage[i][0] === key) { return this.#storage[i][1]; } } return null } set(key: T, value: R): this { for (let i = 0; i < this.#storage.length; i++) { if (this.#storage[i][0] === key) { this.#storage[i][1] = value; return this; } } this.#storage.push([key, value]); return this; } forEach(callbackFn: (value: R, key: T) => void) { for (let i = 0; i < this.#storage.length; i++) { callbackFn(this.#storage[i][1], this.#storage[i][0]); } } *[Symbol.iterator]() { for (let i = 0; i < this.#storage.length; i++) { yield this.#storage[i] } } get size() { return this.#storage.length } clear() { this.#storage = [] } delete(key: T): this { let index = -1 for (let i = 0; i < this.#storage.length; i++) { if (this.#storage[i][0] === key) { index = i break } } if (index > -1) { let newArr: Array<[T, R]> = [] for (let i = 0; i < this.#storage.length; i++) { if (i !== index) { newArr.push(this.#storage[i]) } } this.#storage = newArr } return this } } void function main() { let obj = new LocalMap() obj.set('1111', true) obj.set('2222', false) obj.set('3333', false) obj.delete('2222') for (let [k, v] of obj) { console.log(`${k} is ${v}`); } } ```
ASDAlexander77 commented 9 months ago

warning 1) is about returning type "R | null" R is not the same size as "null" as R in generic will be boolean. You can ignore it, it is just place where u need to be careful

warning 2) about using "yield", as yield should return "[false, undefined]" as last element thus "undefined" will be casted into type R which boolean and thus will be set as "false"

ASDAlexander77 commented 9 months ago

BTW: as u r returning type in "get" R | null u need to have special access code to it

  const v = obj.get('1111');
  if (typeof v == "boolean")
    console.log("val 1111 (bool): ", v);
  else
    console.log("this is null");