removing unwraps that can cause panics when typechecking

Signed-off-by: Jakub Doka <jakub.doka2@gmail.com>
This commit is contained in:
Jakub Doka 2024-12-25 21:54:32 +01:00
parent 4ebf1c7996
commit abcb3434f8
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
2 changed files with 16 additions and 15 deletions

View file

@ -28,7 +28,7 @@ use {
assert_matches::debug_assert_matches, assert_matches::debug_assert_matches,
cell::RefCell, cell::RefCell,
fmt::{self, Debug, Display, Write}, fmt::{self, Debug, Display, Write},
format_args as fa, mem, usize, format_args as fa, mem,
}, },
hbbytecode::DisasmError, hbbytecode::DisasmError,
}; };
@ -886,7 +886,9 @@ impl<'a> Codegen<'a> {
self.error(pos + (literal.len() - bytes.len()) as u32 - 1, message); 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 matches!(expr, Expr::Char { .. }) {
if char_count != 1 { if char_count != 1 {
@ -2826,12 +2828,12 @@ impl<'a> Codegen<'a> {
if let Some(caller) = &mut caller if let Some(caller) = &mut caller
&& let (Some(Arg::Value(ty)), Some(carg)) = (tys.next(self.tys), cargs.next()) && let (Some(Arg::Value(ty)), Some(carg)) = (tys.next(self.tys), cargs.next())
{ {
match (caller.ty.is_pointer(), ty.is_pointer()) { match (self.tys.base_of(caller.ty), ty.is_pointer()) {
(true, false) => { (Some(ty), false) => {
caller.ty = self.tys.base_of(caller.ty).unwrap(); caller.ty = ty;
caller.ptr = true; caller.ptr = true;
} }
(false, true) => { (None, true) => {
caller.ty = self.tys.make_ptr(caller.ty); caller.ty = self.tys.make_ptr(caller.ty);
caller.ptr = false; caller.ptr = false;
} }
@ -2944,12 +2946,12 @@ impl<'a> Codegen<'a> {
if let Some(caller) = &mut caller if let Some(caller) = &mut caller
&& let (Some(Arg::Value(ty)), Some(carg)) = (tys.next(self.tys), cargs.next()) && let (Some(Arg::Value(ty)), Some(carg)) = (tys.next(self.tys), cargs.next())
{ {
match (caller.ty.is_pointer(), ty.is_pointer()) { match (self.tys.base_of(caller.ty), ty.is_pointer()) {
(true, false) => { (Some(ty), false) => {
caller.ty = self.tys.base_of(caller.ty).unwrap(); caller.ty = ty;
caller.ptr = true; caller.ptr = true;
} }
(false, true) => { (None, true) => {
caller.ty = self.tys.make_ptr(caller.ty); caller.ty = self.tys.make_ptr(caller.ty);
caller.ptr = false; 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 { fn find_type_low(&mut self, pos: Pos, from_file: Module, parent: ty::Id, id: DeclId) -> ty::Id {
let file = match parent.expand() { let file = match parent.expand() {
ty::Kind::Module(m) => m, 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 let ty = if let DeclId::Ident(id) = id
@ -4114,7 +4116,7 @@ impl<'a> Codegen<'a> {
let g = &self.tys.ins.globals[g]; let g = &self.tys.ins.globals[g];
if g.ty == ty::Id::TYPE { if g.ty == ty::Id::TYPE {
return ty::Id::from( 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 => { _ if sc.alloc_const => {
let prev_file = mem::replace(&mut self.ci.file, sc.file); let prev_file = mem::replace(&mut self.ci.file, sc.file);
let prev_parent = mem::replace(&mut self.ci.parent, sc.parent); 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.file = prev_file;
self.ci.parent = prev_parent; self.ci.parent = prev_parent;
id.map(|v| self.ci.nodes.as_ty(v.id)).unwrap_or(ty::Id::NEVER)
self.ci.nodes.as_ty(id)
} }
ref e => { ref e => {
self.error_unhandled_ast(e, "bruh"); self.error_unhandled_ast(e, "bruh");

View file