Skip to content

Commit

Permalink
Remove panics
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jul 26, 2024
1 parent 57c0f69 commit ad7a304
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
5 changes: 4 additions & 1 deletion crates/rue-compiler/src/compiler/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ fn divmod(db: &mut Database, ty: &mut TypeSystem) -> SymbolId {

let type_id = ty.alloc(Type::Unknown);

let parameters = ty.alloc(Type::Pair(ty.std().int, ty.std().nil));
let parameters = ty.alloc(Type::Pair(ty.std().int, parameters));

*ty.get_mut(type_id) = Type::Callable(Callable {
original_type_id: type_id,
parameter_names: indexset!["lhs".to_string(), "rhs".to_string()],
parameters: ty.alloc(Type::Pair(int_pair, ty.std().nil)),
parameters,
rest: Rest::Nil,
return_type: int_pair,
generic_types: Vec::new(),
Expand Down
31 changes: 22 additions & 9 deletions crates/rue-compiler/src/compiler/expr/field_access_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ impl Compiler<'_> {
let type_id = fields[index];

if index == ty.field_names.len() - 1 && ty.rest == Rest::Optional {
todo!();
// TODO: type_id = self.ty.alloc(Type::Optional(type_id));
}

Expand All @@ -56,14 +55,28 @@ impl Compiler<'_> {
Type::Variant(variant) => {
let field_names = variant.field_names.clone().unwrap_or_default();

let fields = variant
.field_names
.as_ref()
.map(|field_names| {
deconstruct_items(self.ty, variant.type_id, field_names.len(), variant.rest)
.expect("invalid struct type")
})
.unwrap_or_default();
let Type::Enum(ty) = self.ty.get(variant.original_enum_type_id) else {
unreachable!();
};

let fields = if ty.has_fields {
let type_id = self
.ty
.get_pair(variant.type_id)
.expect("expected a pair")
.1;

variant
.field_names
.as_ref()
.map(|field_names| {
deconstruct_items(self.ty, type_id, field_names.len(), variant.rest)
.expect("invalid struct type")
})
.unwrap_or_default()
} else {
Vec::new()
};

if let Some(index) = field_names.get_index_of(field_name.text()) {
let type_id = fields[index];
Expand Down
25 changes: 17 additions & 8 deletions crates/rue-compiler/src/compiler/expr/initializer_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ impl Compiler<'_> {
}
Some(Type::Variant(enum_variant)) => {
if let Some(field_names) = enum_variant.field_names {
let fields = deconstruct_items(
self.ty,
enum_variant.type_id,
field_names.len(),
enum_variant.rest,
)
.expect("invalid variant type");
let Type::Enum(enum_type) = self.ty.get(enum_variant.original_enum_type_id)
else {
unreachable!();
};

let fields = if enum_type.has_fields {
let type_id = self
.ty
.get_pair(enum_variant.type_id)
.expect("expected a pair")
.1;

deconstruct_items(self.ty, type_id, field_names.len(), enum_variant.rest)
.expect("invalid struct type")
} else {
Vec::new()
};

let fields_hir_id = self.compile_initializer_fields(
&field_names.into_iter().zip(fields).collect(),
Expand Down Expand Up @@ -111,7 +121,6 @@ impl Compiler<'_> {
{
// TODO: optional |= matches!(self.db.ty(value.type_id), Type::Optional(..));
// value.type_id = self.db.non_undefined(value.type_id);
todo!()
}

// Check the type of the field initializer.
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/compiler/expr/lambda_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Compiler<'_> {
// If the parameter is optional, wrap the type in a possibly undefined type.
// This prevents referencing the parameter until it's checked for undefined.
// TODO: self.ty.alloc(Type::Optional(type_id))
todo!()
type_id
} else {
type_id
};
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/compiler/item/function_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Compiler<'_> {
// If the parameter is optional, wrap the type in a possibly undefined type.
// This prevents referencing the parameter until it's checked for undefined.
// TODO: self.ty.alloc(Type::Optional(type_id))
todo!()
type_id
} else {
// Otherwise, just use the type.
type_id
Expand Down

0 comments on commit ad7a304

Please sign in to comment.