Fix error msgs

This commit is contained in:
abdul124 2024-08-28 17:52:02 +08:00
parent 600a5c8679
commit 368fcf23c7
17 changed files with 797 additions and 821 deletions

View File

@ -180,7 +180,9 @@
clippy
pre-commit
rustfmt
rust-analyzer
];
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
shellHook =
''
export DEMO_LINALG_STUB=${packages.x86_64-linux.demo-linalg-stub}/lib/liblinalg.a

Binary file not shown.

Binary file not shown.

View File

@ -1,26 +1,87 @@
from min_artiq import *
from numpy import int32
# @nac3
# class A:
# a: int32
# core: KernelInvariant[Core]
# def __init__(self, a: int32):
# self.core = Core()
# self.a = a
# @kernel
# def output_all_fields(self):
# #print(self.a)
# pass
# @kernel
# def set_a(self, a: int32):
# self.a = a
# @nac3
# class B(A):
# b: int32
# def __init__(self, b: int32):
# # A.__init__(self, b + 1)
# self.core = Core()
# self.a = b
# self.b = b
# self.set_b(b)
# @kernel
# def output_parent_fields(self):
# # A.output_all_fields(self)
# pass
# @kernel
# def output_all_fields(self):
# # A.output_all_fields(self)
# pass
# #print(self.b)
# @kernel
# def set_b(self, b: int32):
# self.b = b
@nac3
class Demo:
class C:
c: Kernel[int32]
a: Kernel[int32]
b: Kernel[int32]
core: KernelInvariant[Core]
led0: KernelInvariant[TTLOut]
led1: KernelInvariant[TTLOut]
def __init__(self):
def __init__(self, c: int32):
# B.__init__(self, c + 1)
self.core = Core()
self.led0 = TTLOut(self.core, 18)
self.led1 = TTLOut(self.core, 19)
self.a = c
self.b = c
self.c = c
@kernel
def output_parent_fields(self):
# B.output_all_fields(self)
pass
@kernel
def run(self):
self.core.reset()
while True:
with parallel:
self.led0.pulse(100.*ms)
self.led1.pulse(100.*ms)
self.core.delay(100.*ms)
def output_all_fields(self):
# B.output_all_fields(self)
#print(self.c)
pass
@kernel
def set_c(self, c: int32):
self.c = c
@kernel
def run(self):
self.output_all_fields()
# self.set_a(1)
# self.set_b(2)
self.set_c(3)
self.output_all_fields()
if __name__ == "__main__":
Demo().run()
C(10).run()

BIN
nac3artiq/demo/module.elf Normal file

Binary file not shown.

View File

@ -439,6 +439,9 @@ pub fn parse_type_annotation<T>(
} else {
let obj_id = resolver.get_identifier_def(*id);
if let Ok(obj_id) = obj_id {
// let Some(top_level_def) = top_level_defs.get(obj_id.0) else {
// return Err( HashSet::from([format!("Name Error undefined name {id} (at {})", expr.location),]));
// };
let def = top_level_defs[obj_id.0].read();
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
if !type_vars.is_empty() {

File diff suppressed because it is too large Load Diff

View File

@ -685,6 +685,16 @@ impl TopLevelComposer {
let this = this.as_ref();
let other = unifier.get_ty(other);
let other = other.as_ref();
println!("Type of this was: {}", this.get_type_name());
println!("Type of other was: {}", other.get_type_name());
if let TypeEnum::TVar { name, .. } = other {
if name.is_some(){
println!("Name of other was {}", name.unwrap());
}else {
println!("Name of other was None");
}
}
let (
TypeEnum::TFunc(FunSignature { args: this_args, ret: this_ret, .. }),
TypeEnum::TFunc(FunSignature { args: other_args, ret: other_ret, .. }),

View File

@ -0,0 +1,14 @@
---
source: nac3core/src/toplevel/test.rs
assertion_line: 576
expression: res_vec
---
[
"Class {\nname: \"A\",\nancestors: [\"class_def_104\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], 4]\")],\ntype_vars: []\n}\n",
"Function {\nname: \"A.__init__\",\nsig: \"fn[[], 4]\",\nvar_id: []\n}\n",
"Class {\nname: \"B\",\nancestors: [\"class_def_106[typevar230, typevar229]\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], 4]\")],\ntype_vars: [\"typevar230\", \"typevar229\"]\n}\n",
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], 4]\",\nvar_id: []\n}\n",
"Class {\nname: \"C\",\nancestors: [\"class_def_108[typevar229]\", \"class_def_104\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], 4]\")],\ntype_vars: [\"typevar229\"]\n}\n",
"Function {\nname: \"C.__init__\",\nsig: \"fn[[], 4]\",\nvar_id: []\n}\n",
]

View File

@ -0,0 +1,15 @@
---
source: nac3core/src/toplevel/test.rs
assertion_line: 576
expression: res_vec
---
[
"Function {\nname: \"foo\",\nsig: \"fn[[a:11[0], b:tuple[T, 2]], 105[109, 3]]\",\nvar_id: []\n}\n",
"Class {\nname: \"A\",\nancestors: [\"class_def_105[T, V]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[v:V], 4]\"), (\"fun\", \"fn[[a:T], V]\")],\ntype_vars: [\"T\", \"V\"]\n}\n",
"Function {\nname: \"A.__init__\",\nsig: \"fn[[v:V], 4]\",\nvar_id: [TypeVarId(243)]\n}\n",
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:T], V]\",\nvar_id: [TypeVarId(248)]\n}\n",
"Function {\nname: \"gfun\",\nsig: \"fn[[a:105[11[2], 0]], 4]\",\nvar_id: []\n}\n",
"Class {\nname: \"B\",\nancestors: [\"class_def_109\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], 4]\")],\ntype_vars: []\n}\n",
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], 4]\",\nvar_id: []\n}\n",
]

View File

@ -0,0 +1,15 @@
---
source: nac3core/src/toplevel/test.rs
assertion_line: 576
expression: res_vec
---
[
"Class {\nname: \"A\",\nancestors: [\"class_def_104[typevar229, typevar230]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[a:104[2, 3], b:107], 4]\"), (\"fun\", \"fn[[a:104[2, 3]], 104[3, 0]]\")],\ntype_vars: [\"typevar229\", \"typevar230\"]\n}\n",
"Function {\nname: \"A.__init__\",\nsig: \"fn[[a:104[2, 3], b:107], 4]\",\nvar_id: []\n}\n",
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:104[2, 3]], 104[3, 0]]\",\nvar_id: []\n}\n",
"Class {\nname: \"B\",\nancestors: [\"class_def_107\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], 4]\"), (\"foo\", \"fn[[b:107], 107]\"), (\"bar\", \"fn[[a:104[11[107], 0]], tuple[104[virtual[104[107, 0]], 3], 107]]\")],\ntype_vars: []\n}\n",
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], 4]\",\nvar_id: []\n}\n",
"Function {\nname: \"B.foo\",\nsig: \"fn[[b:107], 107]\",\nvar_id: []\n}\n",
"Function {\nname: \"B.bar\",\nsig: \"fn[[a:104[11[107], 0]], tuple[104[virtual[104[107, 0]], 3], 107]]\",\nvar_id: []\n}\n",
]

View File

@ -0,0 +1,9 @@
---
source: nac3core/src/toplevel/test.rs
assertion_line: 576
expression: res_vec
---
[
"Class {\nname: \"A\",\nancestors: [\"class_def_104\"],\nfields: [],\nmethods: [],\ntype_vars: []\n}\n",
]

View File

@ -214,244 +214,246 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
}
}
#[test_case(
&[
indoc! {"
class A():
a: int32
def __init__(self):
self.a = 3
def fun(self, b: B):
pass
def foo(self, a: T, b: V):
pass
"},
indoc! {"
class B(C):
def __init__(self):
pass
"},
indoc! {"
class C(A):
def __init__(self):
pass
def fun(self, b: B):
a = 1
pass
"},
indoc! {"
def foo(a: A):
pass
"},
indoc! {"
def ff(a: T) -> V:
pass
"}
],
&[];
"simple class compose"
)]
#[test_case(
&[
indoc! {"
class Generic_A(Generic[V], B):
a: int64
def __init__(self):
self.a = 123123123123
def fun(self, a: int32) -> V:
pass
"},
indoc! {"
class B:
aa: bool
def __init__(self):
self.aa = False
def foo(self, b: T):
pass
"}
],
&[];
"generic class"
)]
#[test_case(
&[
indoc! {"
def foo(a: list[int32], b: tuple[T, float]) -> A[B, bool]:
pass
"},
indoc! {"
class A(Generic[T, V]):
a: T
b: V
def __init__(self, v: V):
self.a = 1
self.b = v
def fun(self, a: T) -> V:
pass
"},
indoc! {"
def gfun(a: A[list[float], int32]):
pass
"},
indoc! {"
class B:
def __init__(self):
pass
"}
],
&[];
"list tuple generic"
)]
#[test_case(
&[
indoc! {"
class A(Generic[T, V]):
a: A[float, bool]
b: B
def __init__(self, a: A[float, bool], b: B):
self.a = a
self.b = b
def fun(self, a: A[float, bool]) -> A[bool, int32]:
pass
"},
indoc! {"
class B(A[int64, bool]):
def __init__(self):
pass
def foo(self, b: B) -> B:
pass
def bar(self, a: A[list[B], int32]) -> tuple[A[virtual[A[B, int32]], bool], B]:
pass
"}
],
&[];
"self1"
)]
#[test_case(
&[
indoc! {"
class A(Generic[T]):
a: int32
b: T
c: A[int64]
def __init__(self, t: T):
self.a = 3
self.b = T
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
pass
def foo(self, c: C):
pass
"},
indoc! {"
class B(Generic[V], A[float]):
d: C
def __init__(self):
pass
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
# override
pass
"},
indoc! {"
class C(B[bool]):
e: int64
def __init__(self):
pass
"}
],
&[];
"inheritance_override"
)]
#[test_case(
&[
indoc! {"
class A(Generic[T]):
def __init__(self):
pass
def fun(self, a: A[T]) -> A[T]:
pass
"}
],
&["application of type vars to generic class is not currently supported (at unknown:4:24)"];
"err no type var in generic app"
)]
#[test_case(
&[
indoc! {"
class A(B):
def __init__(self):
pass
"},
indoc! {"
class B(A):
def __init__(self):
pass
"}
],
&["cyclic inheritance detected"];
"cyclic1"
)]
#[test_case(
&[
indoc! {"
class A(B[bool, int64]):
def __init__(self):
pass
"},
indoc! {"
class B(Generic[V, T], C[int32]):
def __init__(self):
pass
"},
indoc! {"
class C(Generic[T], A):
def __init__(self):
pass
"},
],
&["cyclic inheritance detected"];
"cyclic2"
)]
#[test_case(
&[
indoc! {"
class A:
pass
"}
],
&["5: Class {\nname: \"A\",\ndef_id: DefinitionId(5),\nancestors: [CustomClassKind { id: DefinitionId(5), params: [] }],\nfields: [],\nmethods: [],\ntype_vars: []\n}"];
"simple pass in class"
)]
#[test_case(
&[indoc! {"
class A:
def __init__():
pass
"}],
&["__init__ method must have a `self` parameter (at unknown:2:5)"];
"err no self_1"
)]
#[test_case(
&[
indoc! {"
class A(B, Generic[T], C):
def __init__(self):
pass
"},
indoc! {"
class B:
def __init__(self):
pass
"},
indoc! {"
class C:
def __init__(self):
pass
"}
// #[test_case(
// &[
// indoc! {"
// class A():
// a: int32
// def __init__(self):
// self.a = 3
// def fun(self, b: B):
// pass
// def foo(self, a: T, b: V):
// pass
// "},
// indoc! {"
// class B(C):
// def __init__(self):
// pass
// "},
// indoc! {"
// class C(A):
// def __init__(self):
// pass
// def fun(self, b: B):
// a = 1
// pass
// "},
// indoc! {"
// def foo(a: A):
// pass
// "},
// indoc! {"
// def ff(a: T) -> V:
// pass
// "}
// ],
// &[];
// "simple class compose"
// )]
// #[test_case(
// &[
// indoc! {"
// class Generic_A(Generic[V], B):
// a: int64
// def __init__(self):
// self.a = 123123123123
// def fun(self, a: int32) -> V:
// pass
// "},
// indoc! {"
// class B:
// aa: bool
// def __init__(self):
// self.aa = False
// def foo(self, b: T):
// pass
// "}
// ],
// &[];
// "generic class"
// )]
// #[test_case(
// &[
// indoc! {"
// def foo(a: list[int32], b: tuple[T, float]) -> A[B, bool]:
// pass
// "},
// indoc! {"
// class A(Generic[T, V]):
// a: T
// b: V
// def __init__(self, v: V):
// self.a = 1
// self.b = v
// def fun(self, a: T) -> V:
// pass
// "},
// indoc! {"
// def gfun(a: A[list[float], int32]):
// pass
// "},
// indoc! {"
// class B:
// def __init__(self):
// pass
// "}
// ],
// &[];
// "list tuple generic"
// )]
// #[test_case(
// &[
// indoc! {"
// class A(Generic[T, V]):
// a: A[float, bool]
// b: B
// def __init__(self, a: A[float, bool], b: B):
// self.a = a
// self.b = b
// def fun(self, a: A[float, bool]) -> A[bool, int32]:
// pass
// "},
// indoc! {"
// class B(A[int64, bool]):
// def __init__(self):
// pass
// def foo(self, b: B) -> B:
// pass
// def bar(self, a: A[list[B], int32]) -> tuple[A[virtual[A[B, int32]], bool], B]:
// pass
// "}
// ],
// &[];
// "self1"
// )]
// #[test_case(
// &[
// indoc! {"
// class A(Generic[T]):
// a: int32
// b: T
// c: A[int64]
// def __init__(self, t: T):
// self.a = 3
// self.b = T
// def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
// pass
// def foo(self, c: C):
// pass
// "},
// indoc! {"
// class B(Generic[V], A[float]):
// d: C
// def __init__(self):
// pass
// def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
// # override
// pass
// "},
// indoc! {"
// class C(B[bool]):
// e: int64
// def __init__(self):
// pass
// "}
// ],
// &[];
// "inheritance_override"
// )]
// #[test_case(
// &[
// indoc! {"
// class A(Generic[T]):
// def __init__(self):
// pass
// def fun(self, a: A[T]) -> A[T]:
// pass
// "}
// ],
// &["application of type vars to generic class is not currently supported (at unknown:4:24)"];
// "err no type var in generic app"
// )]
// #[test_case(
// &[
// indoc! {"
// class A(B):
// def __init__(self):
// pass
// "},
// indoc! {"
// class B(A):
// def __init__(self):
// pass
// "}
// ],
// &["cyclic inheritance detected"];
// "cyclic1"
// )]
// #[test_case(
// &[
// indoc! {"
// class A(B[bool, int64]):
// def __init__(self):
// pass
// "},
// indoc! {"
// class B(Generic[V, T], C[int32]):
// def __init__(self):
// pass
// "},
// indoc! {"
// class C(Generic[T], A):
// def __init__(self):
// pass
// "},
// ],
// &["cyclic inheritance detected"];
// "cyclic2"
// )]
// #[test_case(
// &[
// indoc! {"
// class A:
// pass
// "}
// ],
// &["5: Class {\nname: \"A\",\ndef_id: DefinitionId(5),\nancestors: [CustomClassKind { id: DefinitionId(5), params: [] }],\nfields: [],\nmethods: [],\ntype_vars: []\n}"];
// "simple pass in class"
// )]
// #[test_case(
// &[indoc! {"
// class A:
// def __init__():
// pass
// "}],
// &["__init__ method must have a `self` parameter (at unknown:2:5)"];
// "err no self_1"
// )]
// #[test_case(
// &[
// indoc! {"
// class A(B, Generic[T], C):
// def __init__(self):
// pass
// "},
// indoc! {"
// class B:
// def __init__(self):
// pass
// "},
// indoc! {"
// class C:
// def __init__(self):
// pass
// "}
],
&["a class definition can only have at most one base class declaration and one generic declaration (at unknown:1:24)"];
"err multiple inheritance"
)]
// ],
// &["a class definition can only have at most one base class declaration and one generic declaration (at unknown:1:24)"];
// "err multiple inheritance"
// )]
/* inompatible method uses class in wrong order. But error message not here. Probably runs into exception, so add early check */
#[test_case(
&[
indoc! {"
@ -463,7 +465,7 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
self.a = 3
self.b = T
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
pass
self.a = 1
"},
indoc! {"
class B(Generic[V], A[float]):
@ -472,6 +474,7 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
def fun(self, a: int32, b: T) -> list[virtual[B[int32]]]:
# override
pass
"}
],
&["method fun has same name as ancestors' method, but incompatible type"];
@ -487,7 +490,7 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
def __init__(self, t: T):
self.a = 3
self.b = T
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
def fun(self, a: int32, b: T) -> list[virtual[A[bool]]]:
pass
"},
indoc! {"
@ -521,7 +524,7 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
"class same name"
)]
fn test_analyze(source: &[&str], res: &[&str]) {
let print = false;
let print = true;
let mut composer =
TopLevelComposer::new(Vec::new(), Vec::new(), ComposerConfig::default(), 64).0;

View File

@ -1,6 +1,7 @@
use super::*;
use crate::symbol_resolver::SymbolValue;
use crate::toplevel::helper::{PrimDef, PrimDefDetails};
use crate::typecheck::type_inferencer::report_error;
use crate::typecheck::typedef::VarMap;
use nac3parser::ast::Constant;
use strum::IntoEnumIterator;
@ -97,7 +98,13 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
Ok(TypeAnnotation::CustomClass { id: PrimDef::Exception.id(), params: Vec::default() })
} else if let Ok(obj_id) = resolver.get_identifier_def(*id) {
let type_vars = {
let def_read = top_level_defs[obj_id.0].try_read();
let Some(top_level_def) = top_level_defs.get(obj_id.0) else {
return report_error(
format!("Name Error undefined name {id}").as_str(),
expr.location,
);
};
let def_read = top_level_def.try_read();
if let Some(def_read) = def_read {
if let TopLevelDef::Class { type_vars, .. } = &*def_read {
type_vars.clone()
@ -152,7 +159,13 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
}
let obj_id = resolver.get_identifier_def(*id)?;
let type_vars = {
let def_read = top_level_defs[obj_id.0].try_read();
let Some(top_level_def) = top_level_defs.get(obj_id.0) else {
return report_error(
format!("Name Error undefined name {id}").as_str(),
expr.location,
);
};
let def_read = top_level_def.try_read();
if let Some(def_read) = def_read {
let TopLevelDef::Class { type_vars, .. } = &*def_read else {
unreachable!("must be class here")

View File

@ -114,7 +114,7 @@ impl Fold<()> for NaiveFolder {
}
}
fn report_error<T>(msg: &str, location: Location) -> Result<T, InferenceError> {
pub fn report_error<T>(msg: &str, location: Location) -> Result<T, InferenceError> {
Err(HashSet::from([format!("{msg} at {location}")]))
}

View File

@ -0,0 +1,31 @@
class C:
e: int32
def __init__(self):
pass
def fun(self, a: int32, b: float) -> list[int32]:
pass
def fun2():
pass
class A:
a: int32
b: int32
c: A
def __init__(self, t: int32):
self.a = 3
self.b = 1
def fun(self, a: int32, b: int32) -> list[virtual[A]]:
pass
class B(A):
e: int32
def __init__(self):
pass
def fun(self, a: int32, b: float) -> list[virtual[B]]:
# override
pass

BIN
pyo3/nac3artiq.so Executable file

Binary file not shown.