Compare commits

..

No commits in common. "27011f385b504fb6f81985f60e337b603a0259e6" and "fef4b2a5ce5c0685f597695ef5c640ce2f1625ce" have entirely different histories.

View File

@ -3,7 +3,7 @@ use crate::typecheck::typedef::TypeEnum;
use super::type_inferencer::Inferencer; use super::type_inferencer::Inferencer;
use super::typedef::Type; use super::typedef::Type;
use nac3parser::ast::{self, Constant, Expr, ExprKind, Operator::{LShift, RShift}, Stmt, StmtKind, StrRef}; use nac3parser::ast::{self, Constant, Expr, ExprKind, Operator::{LShift, RShift}, Stmt, StmtKind, StrRef};
use std::{collections::HashSet, iter::once}; use std::{collections::HashSet, iter::once, ops::Not};
impl<'a> Inferencer<'a> { impl<'a> Inferencer<'a> {
fn should_have_value(&mut self, expr: &Expr<Option<Type>>) -> Result<(), HashSet<String>> { fn should_have_value(&mut self, expr: &Expr<Option<Type>>) -> Result<(), HashSet<String>> {
@ -208,27 +208,6 @@ impl<'a> Inferencer<'a> {
Ok(()) Ok(())
} }
/// Check that the return value is a non-`alloca` type, effectively only allowing primitive types.
///
/// This is a workaround preventing the caller from using a variable `alloca`-ed in the body, which
/// is freed when the function returns.
fn check_return_value_ty(&mut self, ret_ty: Type) -> bool {
match &*self.unifier.get_ty_immutable(ret_ty) {
TypeEnum::TObj { .. } => {
[
self.primitives.int32,
self.primitives.int64,
self.primitives.uint32,
self.primitives.uint64,
self.primitives.float,
self.primitives.bool,
].iter().any(|allowed_ty| self.unifier.unioned(ret_ty, *allowed_ty))
}
TypeEnum::TTuple { ty } => ty.iter().all(|t| self.check_return_value_ty(*t)),
_ => false,
}
}
// check statements for proper identifier def-use and return on all paths // check statements for proper identifier def-use and return on all paths
fn check_stmt( fn check_stmt(
&mut self, &mut self,
@ -328,20 +307,24 @@ impl<'a> Inferencer<'a> {
// This is a workaround preventing the caller from using a variable `alloca`-ed in the body, which // This is a workaround preventing the caller from using a variable `alloca`-ed in the body, which
// is freed when the function returns. // is freed when the function returns.
if let Some(ret_ty) = value.custom { if let Some(ret_ty) = value.custom {
// Explicitly allow ellipsis as a return value, as the type of the ellipsis is contextually if [
// inferred and just generates an unconditional assertion self.primitives.int32,
if matches!(value.node, ExprKind::Constant { value: Constant::Ellipsis, .. }) { self.primitives.int64,
return Ok(true) self.primitives.uint32,
} self.primitives.uint64,
self.primitives.float,
if !self.check_return_value_ty(ret_ty) { self.primitives.bool,
return Err(HashSet::from([ ].iter().any(|allowed_ty| self.unifier.unioned(ret_ty, *allowed_ty)).not() {
format!( // Explicitly allow ellipsis as a return value, as the type of the ellipsis is contextually
"return value of type {} must be a primitive of a tuple of primitives at {:?}", // inferred and just generates an unconditional assertion
self.unifier.stringify(ret_ty), if matches!(value.node, ExprKind::Constant { value: Constant::Ellipsis, .. }).not() {
value.location, return Err(HashSet::from([
), format!(
])) "return value of type {} must be a primitive",
self.unifier.stringify(ret_ty),
),
]))
}
} }
} }
} }