From c88daa4800c2cb8d96ee2017f698f3fe0d48c5a1 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Fri, 25 Oct 2024 15:07:39 +0200 Subject: [PATCH] adding better negative number inference --- Cargo.lock | 3 +++ lang/Cargo.toml | 2 +- lang/src/son.rs | 18 +++++++++++------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c0d5fb2..2992a05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -594,6 +594,9 @@ name = "hashbrown" version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +dependencies = [ + "allocator-api2", +] [[package]] name = "hashlink" diff --git a/lang/Cargo.toml b/lang/Cargo.toml index aa08bdd..e80fb1b 100644 --- a/lang/Cargo.toml +++ b/lang/Cargo.toml @@ -8,7 +8,7 @@ name = "hbc" path = "src/main.rs" [dependencies] -hashbrown = { version = "0.15.0", default-features = false, features = ["raw-entry"] } +hashbrown = { version = "0.15.0", default-features = false, features = ["raw-entry", "allocator-api2"] } hbbytecode = { workspace = true, features = ["disasm"] } hbvm = { workspace = true, features = ["nightly"] } log = "0.4.22" diff --git a/lang/src/son.rs b/lang/src/son.rs index 4611905..3570056 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -1715,8 +1715,8 @@ struct Ctx { } impl Ctx { - pub fn with_ty(self, ty: impl Into) -> Self { - Self { ty: Some(ty.into()) } + pub fn with_ty(self, ty: ty::Id) -> Self { + Self { ty: Some(ty) } } } @@ -2255,7 +2255,8 @@ impl<'a> Codegen<'a> { Some(val) } Expr::UnOp { pos, op: op @ TokenKind::Sub, val } => { - let val = self.expr_ctx(val, ctx)?; + let val = + self.expr_ctx(val, Ctx::default().with_ty(ctx.ty.unwrap_or(ty::Id::INT)))?; if !val.ty.is_integer() { self.report(pos, fa!("cant negate '{}'", self.ty_display(val.ty))); } @@ -2360,7 +2361,8 @@ impl<'a> Codegen<'a> { }; let elem = self.tys.ins.slices[s as usize].elem; - let idx = self.expr_ctx(index, Ctx::default().with_ty(ty::Id::INT))?; + let mut idx = self.expr_ctx(index, Ctx::default().with_ty(ty::Id::DEFAULT_INT))?; + self.assert_ty(index.pos(), &mut idx, ty::Id::DEFAULT_INT, "subscript"); let value = self.tys.size_of(elem) as i64; let size = self.ci.nodes.new_node_nop(ty::Id::INT, Kind::CInt { value }, [VOID]); let inps = [VOID, idx.id, size]; @@ -2888,7 +2890,8 @@ impl<'a> Codegen<'a> { continue; } - let value = self.expr_ctx(&field.value, Ctx::default().with_ty(ty))?; + let mut value = self.expr_ctx(&field.value, Ctx::default().with_ty(ty))?; + self.assert_ty(field.pos, &mut value, ty, fa!("field {}", field.name)); let mem = self.offset(mem, offset); self.store_mem(mem, ty, value.id); } @@ -3051,10 +3054,11 @@ impl<'a> Codegen<'a> { Expr::Break { pos } => self.jump_to(pos, 1), Expr::Continue { pos } => self.jump_to(pos, 0), Expr::If { cond, then, else_, .. } => { - let cond = self.expr_ctx(cond, Ctx::default().with_ty(ty::BOOL))?; + let mut cnd = self.expr_ctx(cond, Ctx::default().with_ty(ty::Id::BOOL))?; + self.assert_ty(cond.pos(), &mut cnd, ty::Id::BOOL, "condition"); let if_node = - self.ci.nodes.new_node(ty::Id::VOID, Kind::If, [self.ci.ctrl, cond.id]); + self.ci.nodes.new_node(ty::Id::VOID, Kind::If, [self.ci.ctrl, cnd.id]); 'b: { let branch = match self.tof(if_node).expand().inner() {