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 clippy
pre-commit pre-commit
rustfmt rustfmt
rust-analyzer
]; ];
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
shellHook = shellHook =
'' ''
export DEMO_LINALG_STUB=${packages.x86_64-linux.demo-linalg-stub}/lib/liblinalg.a 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 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 @nac3
class Demo: class C:
c: Kernel[int32]
a: Kernel[int32]
b: Kernel[int32]
core: KernelInvariant[Core] 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.core = Core()
self.led0 = TTLOut(self.core, 18) self.a = c
self.led1 = TTLOut(self.core, 19) self.b = c
self.c = c
@kernel
def output_parent_fields(self):
# B.output_all_fields(self)
pass
@kernel @kernel
def run(self): def output_all_fields(self):
self.core.reset() # B.output_all_fields(self)
while True: #print(self.c)
with parallel: pass
self.led0.pulse(100.*ms)
self.led1.pulse(100.*ms)
self.core.delay(100.*ms)
@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__": 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 { } else {
let obj_id = resolver.get_identifier_def(*id); let obj_id = resolver.get_identifier_def(*id);
if let Ok(obj_id) = obj_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(); let def = top_level_defs[obj_id.0].read();
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def { if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
if !type_vars.is_empty() { 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 this = this.as_ref();
let other = unifier.get_ty(other); let other = unifier.get_ty(other);
let other = other.as_ref(); 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 ( let (
TypeEnum::TFunc(FunSignature { args: this_args, ret: this_ret, .. }), TypeEnum::TFunc(FunSignature { args: this_args, ret: this_ret, .. }),
TypeEnum::TFunc(FunSignature { args: other_args, ret: other_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( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A(): // class A():
a: int32 // a: int32
def __init__(self): // def __init__(self):
self.a = 3 // self.a = 3
def fun(self, b: B): // def fun(self, b: B):
pass // pass
def foo(self, a: T, b: V): // def foo(self, a: T, b: V):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B(C): // class B(C):
def __init__(self): // def __init__(self):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class C(A): // class C(A):
def __init__(self): // def __init__(self):
pass // pass
def fun(self, b: B): // def fun(self, b: B):
a = 1 // a = 1
pass // pass
"}, // "},
indoc! {" // indoc! {"
def foo(a: A): // def foo(a: A):
pass // pass
"}, // "},
indoc! {" // indoc! {"
def ff(a: T) -> V: // def ff(a: T) -> V:
pass // pass
"} // "}
], // ],
&[]; // &[];
"simple class compose" // "simple class compose"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class Generic_A(Generic[V], B): // class Generic_A(Generic[V], B):
a: int64 // a: int64
def __init__(self): // def __init__(self):
self.a = 123123123123 // self.a = 123123123123
def fun(self, a: int32) -> V: // def fun(self, a: int32) -> V:
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B: // class B:
aa: bool // aa: bool
def __init__(self): // def __init__(self):
self.aa = False // self.aa = False
def foo(self, b: T): // def foo(self, b: T):
pass // pass
"} // "}
], // ],
&[]; // &[];
"generic class" // "generic class"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
def foo(a: list[int32], b: tuple[T, float]) -> A[B, bool]: // def foo(a: list[int32], b: tuple[T, float]) -> A[B, bool]:
pass // pass
"}, // "},
indoc! {" // indoc! {"
class A(Generic[T, V]): // class A(Generic[T, V]):
a: T // a: T
b: V // b: V
def __init__(self, v: V): // def __init__(self, v: V):
self.a = 1 // self.a = 1
self.b = v // self.b = v
def fun(self, a: T) -> V: // def fun(self, a: T) -> V:
pass // pass
"}, // "},
indoc! {" // indoc! {"
def gfun(a: A[list[float], int32]): // def gfun(a: A[list[float], int32]):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B: // class B:
def __init__(self): // def __init__(self):
pass // pass
"} // "}
], // ],
&[]; // &[];
"list tuple generic" // "list tuple generic"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A(Generic[T, V]): // class A(Generic[T, V]):
a: A[float, bool] // a: A[float, bool]
b: B // b: B
def __init__(self, a: A[float, bool], b: B): // def __init__(self, a: A[float, bool], b: B):
self.a = a // self.a = a
self.b = b // self.b = b
def fun(self, a: A[float, bool]) -> A[bool, int32]: // def fun(self, a: A[float, bool]) -> A[bool, int32]:
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B(A[int64, bool]): // class B(A[int64, bool]):
def __init__(self): // def __init__(self):
pass // pass
def foo(self, b: B) -> B: // def foo(self, b: B) -> B:
pass // pass
def bar(self, a: A[list[B], int32]) -> tuple[A[virtual[A[B, int32]], bool], B]: // def bar(self, a: A[list[B], int32]) -> tuple[A[virtual[A[B, int32]], bool], B]:
pass // pass
"} // "}
], // ],
&[]; // &[];
"self1" // "self1"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A(Generic[T]): // class A(Generic[T]):
a: int32 // a: int32
b: T // b: T
c: A[int64] // c: A[int64]
def __init__(self, t: T): // def __init__(self, t: T):
self.a = 3 // self.a = 3
self.b = T // self.b = T
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]: // def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
pass // pass
def foo(self, c: C): // def foo(self, c: C):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B(Generic[V], A[float]): // class B(Generic[V], A[float]):
d: C // d: C
def __init__(self): // def __init__(self):
pass // pass
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]: // def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
# override // # override
pass // pass
"}, // "},
indoc! {" // indoc! {"
class C(B[bool]): // class C(B[bool]):
e: int64 // e: int64
def __init__(self): // def __init__(self):
pass // pass
"} // "}
], // ],
&[]; // &[];
"inheritance_override" // "inheritance_override"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A(Generic[T]): // class A(Generic[T]):
def __init__(self): // def __init__(self):
pass // pass
def fun(self, a: A[T]) -> A[T]: // def fun(self, a: A[T]) -> A[T]:
pass // pass
"} // "}
], // ],
&["application of type vars to generic class is not currently supported (at unknown:4:24)"]; // &["application of type vars to generic class is not currently supported (at unknown:4:24)"];
"err no type var in generic app" // "err no type var in generic app"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A(B): // class A(B):
def __init__(self): // def __init__(self):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B(A): // class B(A):
def __init__(self): // def __init__(self):
pass // pass
"} // "}
], // ],
&["cyclic inheritance detected"]; // &["cyclic inheritance detected"];
"cyclic1" // "cyclic1"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A(B[bool, int64]): // class A(B[bool, int64]):
def __init__(self): // def __init__(self):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B(Generic[V, T], C[int32]): // class B(Generic[V, T], C[int32]):
def __init__(self): // def __init__(self):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class C(Generic[T], A): // class C(Generic[T], A):
def __init__(self): // def __init__(self):
pass // pass
"}, // "},
], // ],
&["cyclic inheritance detected"]; // &["cyclic inheritance detected"];
"cyclic2" // "cyclic2"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A: // class A:
pass // pass
"} // "}
], // ],
&["5: Class {\nname: \"A\",\ndef_id: DefinitionId(5),\nancestors: [CustomClassKind { id: DefinitionId(5), params: [] }],\nfields: [],\nmethods: [],\ntype_vars: []\n}"]; // &["5: Class {\nname: \"A\",\ndef_id: DefinitionId(5),\nancestors: [CustomClassKind { id: DefinitionId(5), params: [] }],\nfields: [],\nmethods: [],\ntype_vars: []\n}"];
"simple pass in class" // "simple pass in class"
)] // )]
#[test_case( // #[test_case(
&[indoc! {" // &[indoc! {"
class A: // class A:
def __init__(): // def __init__():
pass // pass
"}], // "}],
&["__init__ method must have a `self` parameter (at unknown:2:5)"]; // &["__init__ method must have a `self` parameter (at unknown:2:5)"];
"err no self_1" // "err no self_1"
)] // )]
#[test_case( // #[test_case(
&[ // &[
indoc! {" // indoc! {"
class A(B, Generic[T], C): // class A(B, Generic[T], C):
def __init__(self): // def __init__(self):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class B: // class B:
def __init__(self): // def __init__(self):
pass // pass
"}, // "},
indoc! {" // indoc! {"
class C: // class C:
def __init__(self): // def __init__(self):
pass // pass
"} // "}
], // ],
&["a class definition can only have at most one base class declaration and one generic declaration (at unknown:1:24)"]; // &["a class definition can only have at most one base class declaration and one generic declaration (at unknown:1:24)"];
"err multiple inheritance" // "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( #[test_case(
&[ &[
indoc! {" indoc! {"
@ -463,7 +465,7 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
self.a = 3 self.a = 3
self.b = T self.b = T
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]: def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
pass self.a = 1
"}, "},
indoc! {" indoc! {"
class B(Generic[V], A[float]): 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]]]: def fun(self, a: int32, b: T) -> list[virtual[B[int32]]]:
# override # override
pass pass
"} "}
], ],
&["method fun has same name as ancestors' method, but incompatible type"]; &["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): def __init__(self, t: T):
self.a = 3 self.a = 3
self.b = T 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 pass
"}, "},
indoc! {" indoc! {"
@ -521,7 +524,7 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
"class same name" "class same name"
)] )]
fn test_analyze(source: &[&str], res: &[&str]) { fn test_analyze(source: &[&str], res: &[&str]) {
let print = false; let print = true;
let mut composer = let mut composer =
TopLevelComposer::new(Vec::new(), Vec::new(), ComposerConfig::default(), 64).0; TopLevelComposer::new(Vec::new(), Vec::new(), ComposerConfig::default(), 64).0;

View File

@ -1,6 +1,7 @@
use super::*; use super::*;
use crate::symbol_resolver::SymbolValue; use crate::symbol_resolver::SymbolValue;
use crate::toplevel::helper::{PrimDef, PrimDefDetails}; use crate::toplevel::helper::{PrimDef, PrimDefDetails};
use crate::typecheck::type_inferencer::report_error;
use crate::typecheck::typedef::VarMap; use crate::typecheck::typedef::VarMap;
use nac3parser::ast::Constant; use nac3parser::ast::Constant;
use strum::IntoEnumIterator; 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() }) Ok(TypeAnnotation::CustomClass { id: PrimDef::Exception.id(), params: Vec::default() })
} else if let Ok(obj_id) = resolver.get_identifier_def(*id) { } else if let Ok(obj_id) = resolver.get_identifier_def(*id) {
let type_vars = { 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 Some(def_read) = def_read {
if let TopLevelDef::Class { type_vars, .. } = &*def_read { if let TopLevelDef::Class { type_vars, .. } = &*def_read {
type_vars.clone() 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 obj_id = resolver.get_identifier_def(*id)?;
let type_vars = { 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 Some(def_read) = def_read {
let TopLevelDef::Class { type_vars, .. } = &*def_read else { let TopLevelDef::Class { type_vars, .. } = &*def_read else {
unreachable!("must be class here") 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}")])) 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.