kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.61k stars 112 forks source link

Runtime type check can ignore type package name #1536

Closed bozaro closed 1 month ago

bozaro commented 1 month ago

Bug Report

Runtime type check can ignore type package name

1. Minimal reproduce step (Required)

Run script:

#!/bin/sh

mkdir -p base main child

cat > kcl.mod <<EOF
[package]
name = "__main__"
edition = "0.0.1"
version = "0.0.1"
EOF

cat > base/base.k <<EOF
schema Base:
    name: str

schema A(Base):
    bar: str

a = A {
    name: "base.A"
    bar: "xxx"
}
EOF

cat > child/child.k <<EOF
import base

schema A(base.Base):
    foo: str

a = A {
    name: "child.A"
    foo: "test"
}

# Cast to `base.Base` used for skip compile-time type check
_base_a: base.Base = base.a

# Must fail at runtime: typeof(_base_a) == 'base.A'
base_a: A = _base_a as A
base_a_type = typeof(base_a, True)
child_a_type = typeof(a, True)
EOF

cat > main/main.k <<EOF
import base
import child

schema A(base.Base):
    name: str
    main: str

a = A {
    name: "main.A"
    main: "123"
}

# Cast to `base.Base`/`any` used for skip compile-time type check
_child_a: base.Base = child.a
_child_any: any = child.a
_base_a: base.Base = base.a
_child_base_a: base.Base = child.base_a

# Must fail at runtime: typeof(_child_a) == 'child.A'
child_a: A = _child_a as A
# Must fail at runtime: typeof(_child_any) == 'child.A'
child_any: A = _child_any as A
# Must fail at runtime: typeof(_base_a) == 'base.A'
base_a: A = _base_a as A
# Must fail at runtime: typeof(_child_base_a) == 'base.A'
child_base_a1: A = _child_base_a as A

child_base_a2 = child.base_a

a_type = typeof(a, True)
child_a_type = typeof(child_a, True)
child_any_type = typeof(child_any, True)
base_a_type = typeof(base_a, True)
child_base_a1_type = typeof(child_base_a1, True)
child_base_a2_type = typeof(child_base_a2, True)
child_base_a_type = child.base_a_type
child_child_a_type = child.child_a_type
EOF

echo "=== Version ==="
kclvm_cli version
echo

echo "=== LLVM mode ==="
kclvm_cli run main/
echo

echo "=== Fast eval mode ==="
kclvm_cli run -K main/
echo

2. What did you expect to see? (Required)

All configuration evaluated succesfully:

=== Version ===
Version: 0.9.2-c020ab3eb4b9179219d6837a57f5d323
Platform: x86_64-unknown-linux-gnu
GitCommit: VERGEN_IDEMPOTENT_OUTPUT

=== LLVM mode ===
a:
  name: main.A
  main: '123'
child_a:
  name: child.A
  foo: test
child_any:
  name: child.A
  foo: test
base_a:
  name: base.A
  bar: xxx
child_base_a1:
  name: base.A
  bar: xxx
child_base_a2:
  name: base.A
  bar: xxx
a_type: A
child_a_type: child.A
child_any_type: child.A
base_a_type: base.A
child_base_a1_type: base.A
child_base_a2_type: base.A
child_base_a_type: base.A
child_child_a_type: child.A

=== Fast eval mode ===
a:
  name: main.A
  main: '123'
child_a:
  name: child.A
  foo: test
child_any:
  name: child.A
  foo: test
base_a:
  name: base.A
  bar: xxx
child_base_a1:
  name: base.A
  bar: xxx
child_base_a2:
  name: base.A
  bar: xxx
a_type: A
child_a_type: child.A
child_any_type: child.A
base_a_type: base.A
child_base_a1_type: base.A
child_base_a2_type: base.A
child_base_a_type: base.A
child_child_a_type: child.A

3. What did you see instead (Required)

Runtime error check failure:

4. What is your KCL components version? (Required)

Checked on:

Version: 0.9.2-c020ab3eb4b9179219d6837a57f5d323
Platform: x86_64-unknown-linux-gnu
GitCommit: VERGEN_IDEMPOTENT_OUTPUT
Version: 0.10.0-alpha.2-c020ab3eb4b9179219d6837a57f5d323
Platform: x86_64-unknown-linux-gnu
GitCommit: 96b6ae8c82e4ed123d5f5f1cc05deca937c6d3e4
Peefy commented 1 month ago

Hello @bozaro

I guess you mean expecting to see runtime type checking errors instead of getting the correct YAML output, right? I will improve it later. Thank you very much for your detailed information.

bozaro commented 1 month ago

A very unexpected fix...

I tried to figure out this problem on my own and got to the following:

I was unable to find the place where AST SchemaType became to the short type name for the execution tree.

I expected that in the execution tree, the full type name would be input for the as operator...

Peefy commented 1 month ago

Hello @bozaro Sorry, I didn't understand what you meant. Are you saying that I made the wrong fix? Can you provide some specific examples?