1
0

Fix type check bug

This commit is contained in:
Jeff 2023-12-31 18:10:42 -05:00
parent d3a9fb3a0e
commit 32127a6cda
2 changed files with 14 additions and 4 deletions

View File

@ -100,7 +100,15 @@ impl AbstractTree for Block {
} }
fn expected_type(&self, context: &Map) -> Result<Type> { fn expected_type(&self, context: &Map) -> Result<Type> {
if let Some(statement) = self.statements.last() { if let Some(statement) = self.statements.iter().find(|statement| {
if let Statement::Return(_) = statement {
true
} else {
false
}
}) {
statement.expected_type(context)
} else if let Some(statement) = self.statements.last() {
statement.expected_type(context) statement.expected_type(context)
} else { } else {
Ok(Type::None) Ok(Type::None)

View File

@ -69,7 +69,9 @@ impl AbstractTree for ValueNode {
let body_node = child.child(child_count - 1).unwrap(); let body_node = child.child(child_count - 1).unwrap();
let body = Block::from_syntax_node(source, body_node, &function_context)?; let body = Block::from_syntax_node(source, body_node, &function_context)?;
return_type.inner().check(&body.expected_type(context)?)?; return_type
.inner()
.check(&body.expected_type(&function_context)?)?;
let r#type = Type::Function { let r#type = Type::Function {
parameter_types, parameter_types,
@ -207,7 +209,7 @@ impl AbstractTree for ValueNode {
} }
fn expected_type(&self, context: &Map) -> Result<Type> { fn expected_type(&self, context: &Map) -> Result<Type> {
let type_definition = match self { let r#type = match self {
ValueNode::Boolean(_) => Type::Boolean, ValueNode::Boolean(_) => Type::Boolean,
ValueNode::Float(_) => Type::Float, ValueNode::Float(_) => Type::Float,
ValueNode::Function(function) => function.r#type().clone(), ValueNode::Function(function) => function.r#type().clone(),
@ -244,6 +246,6 @@ impl AbstractTree for ValueNode {
ValueNode::Map(_) => Type::Map, ValueNode::Map(_) => Type::Map,
}; };
Ok(type_definition) Ok(r#type)
} }
} }