fix: fix checker

This commit is contained in:
Edgar 2024-02-27 11:23:43 +01:00
parent a2a3bdbb3e
commit 5128f70e7a
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
3 changed files with 39 additions and 70 deletions

View file

@ -174,8 +174,8 @@ pub enum Expression {
StructInit(StructInitExpr), StructInit(StructInitExpr),
Unary(UnaryOp, Box<Self>), Unary(UnaryOp, Box<Self>),
Binary(Box<Self>, BinaryOp, Box<Self>), Binary(Box<Self>, BinaryOp, Box<Self>),
Deref(Box<Self>), Deref(Box<Self>, Span),
AsRef(Box<Self>, bool), AsRef(Box<Self>, bool, Span),
} }
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

View file

@ -548,8 +548,8 @@ fn find_expr_type(builder: &mut BodyBuilder, info: &ast::Expression) -> Option<T
find_expr_type(builder, lhs).or(find_expr_type(builder, rhs))? find_expr_type(builder, lhs).or(find_expr_type(builder, rhs))?
} }
} }
ast::Expression::Deref(_) => todo!(), ast::Expression::Deref(_, _) => todo!(),
ast::Expression::AsRef(_, _) => todo!(), ast::Expression::AsRef(_, _, _) => todo!(),
ast::Expression::StructInit(info) => { ast::Expression::StructInit(info) => {
let id = *builder let id = *builder
.get_module_body() .get_module_body()
@ -571,61 +571,19 @@ fn lower_expr(
ast::Expression::Value(info) => { ast::Expression::Value(info) => {
let (value, ty, span) = lower_value(builder, info, type_hint)?; let (value, ty, span) = lower_value(builder, info, type_hint)?;
if let Some(expected_ty) = type_hint {
if expected_ty.kind != ty {
return Err(LoweringError::UnexpectedType {
span,
found: ty,
expected: expected_ty.clone(),
});
}
}
(ir::RValue::Use(value, span), ty, span) (ir::RValue::Use(value, span), ty, span)
} }
ast::Expression::FnCall(info) => { ast::Expression::FnCall(info) => {
let (value, ty, span) = lower_fn_call(builder, info)?; let (value, ty, span) = lower_fn_call(builder, info)?;
if let Some(expected_ty) = type_hint {
if expected_ty.kind != ty {
return Err(LoweringError::UnexpectedType {
span,
found: ty,
expected: expected_ty.clone(),
});
}
}
(ir::RValue::Use(value, span), ty, span) (ir::RValue::Use(value, span), ty, span)
} }
ast::Expression::Unary(_, _) => todo!(), ast::Expression::Unary(_, _) => todo!(),
ast::Expression::Binary(lhs, op, rhs) => { ast::Expression::Binary(lhs, op, rhs) => {
let result = lower_binary_expr(builder, lhs, op, rhs, type_hint)?; lower_binary_expr(builder, lhs, op, rhs, type_hint)?
if let Some(expected_ty) = type_hint {
if expected_ty.kind != result.1 {
return Err(LoweringError::UnexpectedType {
span: result.2,
found: result.1,
expected: expected_ty.clone(),
});
}
}
result
} }
ast::Expression::Deref(inner) => { ast::Expression::Deref(inner, deref_span) => {
let (value, ty, span) = lower_expr(builder, inner, type_hint)?; let (value, ty, _span) = lower_expr(builder, inner, type_hint)?;
if let Some(expected_ty) = type_hint {
if expected_ty.kind != ty {
return Err(LoweringError::UnexpectedType {
span,
found: ty,
expected: expected_ty.clone(),
});
}
}
// check if its a use directly, to avoid a temporary. // check if its a use directly, to avoid a temporary.
let mut value = match value { let mut value = match value {
@ -639,9 +597,19 @@ fn lower_expr(
value.projection.push(PlaceElem::Deref); value.projection.push(PlaceElem::Deref);
(RValue::Use(Operand::Move(value), span), ty, span) let ty = match ty {
TypeKind::Ptr(_, inner) => *inner,
TypeKind::Ref(_, inner) => *inner,
_ => todo!("proepr error here"),
};
(
RValue::Use(Operand::Move(value), *deref_span),
ty.kind,
*deref_span,
)
} }
ast::Expression::AsRef(inner, mutable) => { ast::Expression::AsRef(inner, mutable, as_ref_span) => {
let type_hint = match type_hint { let type_hint = match type_hint {
Some(inner) => match &inner.kind { Some(inner) => match &inner.kind {
TypeKind::Ref(_, inner) => Some(inner.as_ref()), TypeKind::Ref(_, inner) => Some(inner.as_ref()),
@ -649,21 +617,11 @@ fn lower_expr(
}, },
None => None, None => None,
}; };
let (mut value, ty, span) = lower_expr(builder, inner, type_hint)?; let (mut value, ty, _span) = lower_expr(builder, inner, type_hint)?;
if let Some(expected_ty) = type_hint {
if expected_ty.kind != ty {
return Err(LoweringError::UnexpectedType {
span,
found: ty,
expected: expected_ty.clone(),
});
}
}
// check if its a use directly, to avoid a temporary. // check if its a use directly, to avoid a temporary.
value = match value { value = match value {
RValue::Use(op, _span) => RValue::Ref(*mutable, op, span), RValue::Use(op, _span) => RValue::Ref(*mutable, op, *as_ref_span),
value => { value => {
let inner_local = builder.add_local(Local::temp(ty.clone())); let inner_local = builder.add_local(Local::temp(ty.clone()));
let inner_place = Place { let inner_place = Place {
@ -680,19 +638,19 @@ fn lower_expr(
span: None, span: None,
kind: StatementKind::Assign(inner_place.clone(), value), kind: StatementKind::Assign(inner_place.clone(), value),
}); });
RValue::Ref(*mutable, Operand::Move(inner_place), span) RValue::Ref(*mutable, Operand::Move(inner_place), *as_ref_span)
} }
}; };
let ty = TypeKind::Ref( let ty = TypeKind::Ref(
*mutable, *mutable,
Box::new(TypeInfo { Box::new(TypeInfo {
span: Some(span), span: Some(*as_ref_span),
kind: ty, kind: ty,
}), }),
); );
(value, ty, span) (value, ty, *as_ref_span)
} }
ast::Expression::StructInit(info) => { ast::Expression::StructInit(info) => {
let id = *builder let id = *builder
@ -1098,7 +1056,18 @@ fn lower_return(
return_type: &TypeInfo, return_type: &TypeInfo,
) -> Result<(), LoweringError> { ) -> Result<(), LoweringError> {
if let Some(value_expr) = &info.value { if let Some(value_expr) = &info.value {
let (value, _ty, span) = lower_expr(builder, value_expr, Some(return_type))?; let (value, ty, span) = lower_expr(builder, value_expr, Some(return_type))?;
if return_type.kind != ty {
dbg!("here");
dbg!(value);
return Err(LoweringError::UnexpectedType {
span,
found: ty,
expected: return_type.clone(),
});
}
builder.statements.push(Statement { builder.statements.push(Statement {
span: Some(span), span: Some(span),
kind: StatementKind::Assign( kind: StatementKind::Assign(

View file

@ -317,9 +317,9 @@ pub(crate) Expression: ast::Expression = {
#[precedence(level="0")] #[precedence(level="0")]
<Term>, <Term>,
#[precedence(level="1")] #[assoc(side="left")] #[precedence(level="1")] #[assoc(side="left")]
"&" "mut" <Expression> => ast::Expression::AsRef(Box::new(<>), true), <lo:@L> "&" "mut" <e:Expression> <hi:@R> => ast::Expression::AsRef(Box::new(e), true, ast::Span::new(lo, hi)),
"&" <Expression> => ast::Expression::AsRef(Box::new(<>), false), <lo:@L> "&" <e:Expression> <hi:@R> => ast::Expression::AsRef(Box::new(e), false, ast::Span::new(lo, hi)),
"*" <Expression> => ast::Expression::Deref(Box::new(<>)), <lo:@L> "*" <e:Expression> <hi:@R> => ast::Expression::Deref(Box::new(e), ast::Span::new(lo, hi)),
<op:UnaryOp> <rhs:Expression> => ast::Expression::Unary( <op:UnaryOp> <rhs:Expression> => ast::Expression::Unary(
op, op,
Box::new(rhs) Box::new(rhs)