Skip to content

Commit

Permalink
Optimize literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Feb 28, 2024
1 parent b3d024c commit 7b9b6d2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pil-analyzer/src/type_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ impl TypeChecker {
Some(TypeName::Int) => Type::Int,
Some(TypeName::Fe) => Type::Fe,
Some(TypeName::Expr) => Type::Expr,
Some(TypeName::TypeVar(tv)) => Type::TypeVar(tv.clone()),
Some(t) => panic!("Type name annotation for number is not supported: {t}"),
None => {
let tv = self.new_type_var_name();
Expand Down Expand Up @@ -579,6 +580,17 @@ impl TypeChecker {
expected_type: &Type,
expr: &mut Expression<T>,
) -> Result<(), String> {
// For literals, we try to store the type here already.
// This avoids creating tons of type variables for large arrays.
if let Expression::Number(_, annotated_type @ None) = expr {
match expected_type {
Type::Int => *annotated_type = Some(TypeName::Int),
Type::Fe => *annotated_type = Some(TypeName::Fe),
Type::Expr => *annotated_type = Some(TypeName::Expr),
Type::TypeVar(tv) => *annotated_type = Some(TypeName::TypeVar(tv.clone())),
_ => {}
};
}
let inferred_type = self.process_expression(expr)?;
self.unifier
.unify_types(inferred_type.clone(), expected_type.clone())
Expand Down

0 comments on commit 7b9b6d2

Please sign in to comment.