From abcb3434f8093f23e2df9e58b66385696a6fb871 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Wed, 25 Dec 2024 21:54:32 +0100 Subject: [PATCH] removing unwraps that can cause panics when typechecking Signed-off-by: Jakub Doka --- lang/src/son.rs | 31 ++++++++++++++-------------- lang/tests/son_tests_rs_boy_mess.txt | 0 2 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 lang/tests/son_tests_rs_boy_mess.txt diff --git a/lang/src/son.rs b/lang/src/son.rs index 24f014d..b6e5bf3 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -28,7 +28,7 @@ use { assert_matches::debug_assert_matches, cell::RefCell, fmt::{self, Debug, Display, Write}, - format_args as fa, mem, usize, + format_args as fa, mem, }, hbbytecode::DisasmError, }; @@ -886,7 +886,9 @@ impl<'a> Codegen<'a> { self.error(pos + (literal.len() - bytes.len()) as u32 - 1, message); }; - let char_count = crate::endoce_string(literal, &mut data, report).unwrap(); + let Some(char_count) = crate::endoce_string(literal, &mut data, report) else { + return Value::NEVER; + }; if matches!(expr, Expr::Char { .. }) { if char_count != 1 { @@ -2826,12 +2828,12 @@ impl<'a> Codegen<'a> { if let Some(caller) = &mut caller && let (Some(Arg::Value(ty)), Some(carg)) = (tys.next(self.tys), cargs.next()) { - match (caller.ty.is_pointer(), ty.is_pointer()) { - (true, false) => { - caller.ty = self.tys.base_of(caller.ty).unwrap(); + match (self.tys.base_of(caller.ty), ty.is_pointer()) { + (Some(ty), false) => { + caller.ty = ty; caller.ptr = true; } - (false, true) => { + (None, true) => { caller.ty = self.tys.make_ptr(caller.ty); caller.ptr = false; } @@ -2944,12 +2946,12 @@ impl<'a> Codegen<'a> { if let Some(caller) = &mut caller && let (Some(Arg::Value(ty)), Some(carg)) = (tys.next(self.tys), cargs.next()) { - match (caller.ty.is_pointer(), ty.is_pointer()) { - (true, false) => { - caller.ty = self.tys.base_of(caller.ty).unwrap(); + match (self.tys.base_of(caller.ty), ty.is_pointer()) { + (Some(ty), false) => { + caller.ty = ty; caller.ptr = true; } - (false, true) => { + (None, true) => { caller.ty = self.tys.make_ptr(caller.ty); caller.ptr = false; } @@ -3999,7 +4001,7 @@ impl<'a> Codegen<'a> { fn find_type_low(&mut self, pos: Pos, from_file: Module, parent: ty::Id, id: DeclId) -> ty::Id { let file = match parent.expand() { ty::Kind::Module(m) => m, - _ => self.tys.type_base_of(parent).unwrap().file, + _ => self.tys.type_base_of(parent).expect("parent to be valid").file, }; let ty = if let DeclId::Ident(id) = id @@ -4114,7 +4116,7 @@ impl<'a> Codegen<'a> { let g = &self.tys.ins.globals[g]; if g.ty == ty::Id::TYPE { return ty::Id::from( - u32::from_ne_bytes(g.data.as_slice().try_into().unwrap()) as u64 + g.data.as_slice().try_into().map(u32::from_ne_bytes).unwrap_or(1) as u64, ); } } @@ -4291,11 +4293,10 @@ impl<'a> Codegen<'a> { _ if sc.alloc_const => { let prev_file = mem::replace(&mut self.ci.file, sc.file); let prev_parent = mem::replace(&mut self.ci.parent, sc.parent); - let id = self.expr(expr).unwrap().id; + let id = self.expr(expr); self.ci.file = prev_file; self.ci.parent = prev_parent; - - self.ci.nodes.as_ty(id) + id.map(|v| self.ci.nodes.as_ty(v.id)).unwrap_or(ty::Id::NEVER) } ref e => { self.error_unhandled_ast(e, "bruh"); diff --git a/lang/tests/son_tests_rs_boy_mess.txt b/lang/tests/son_tests_rs_boy_mess.txt new file mode 100644 index 0000000..e69de29