Skip to content

Commit

Permalink
remove clone derive from ty
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 24, 2024
1 parent b048e37 commit 28847a8
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/repr/ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Ty {
Int,
Uint,
Expand Down
2 changes: 1 addition & 1 deletion src/stage/lower_ir/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn lower_block(
value,
..
}) => {
builder.register_scoped(*name, value.get_ty_info().ty);
builder.register_scoped(*name, value.get_ty_info().ty.clone());

let value = lower_expression(compiler, builder, value).unwrap();
builder.add_triple(Triple::Assign(*name, value));
Expand Down
4 changes: 2 additions & 2 deletions src/stage/type_check/expression/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl parse_ast::Assign {

let value = self.value.ty_solve(compiler, scope)?;

let value_ty = value.get_ty_info().ty;
let value_ty = value.get_ty_info().ty.clone();

if value_ty != ty {
return Err(TyError::Mismatch(ty, value_ty));
Expand All @@ -21,7 +21,7 @@ impl parse_ast::Assign {
binding,
ty_info: TyInfo {
ty: Ty::Unit,
return_ty: value.get_ty_info().return_ty,
return_ty: value.get_ty_info().return_ty.clone(),
},
value: Box::new(value),
span: self.span,
Expand Down
4 changes: 2 additions & 2 deletions src/stage/type_check/expression/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ impl parse_ast::Block {
// })
// )
// })
.map(|s| s.get_ty_info().ty)
.map(|s| s.get_ty_info().ty.clone())
.unwrap_or(Ty::Unit),
statements
.iter()
.map(|statement| statement.get_ty_info().return_ty),
.map(|statement| statement.get_ty_info().return_ty.clone()),
))?;

// Leave a scope
Expand Down
6 changes: 3 additions & 3 deletions src/stage/type_check/expression/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl parse_ast::Call {

if !args
.iter()
.map(|arg| arg.get_ty_info().ty)
.map(|arg| arg.get_ty_info().ty.clone())
.zip(&signature.arguments)
.all(|(call, signature)| call == *signature)
{
Expand All @@ -40,9 +40,9 @@ impl parse_ast::Call {
Ok(Call {
ty_info: TyInfo::try_from((
// Function call's resulting type will be whatever the function returns
signature.return_ty,
signature.return_ty.clone(),
// Ensure all the return types from the arguments are correct
args.iter().map(|arg| arg.get_ty_info().return_ty),
args.iter().map(|arg| arg.get_ty_info().return_ty.clone()),
))?,
name: function_idx,
args,
Expand Down
8 changes: 4 additions & 4 deletions src/stage/type_check/expression/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl parse_ast::Cast {
let value = self.value.ty_solve(compiler, scope)?;

// Make sure that the value can be cast to the desired type
match (value.get_ty_info().ty, self.target_ty) {
match (value.get_ty_info().ty.clone(), self.target_ty.clone()) {
// Unsigned integer can become signed
(Ty::Uint, Ty::Int) => (),
// Signed integer can loose sign
Expand All @@ -14,11 +14,11 @@ impl parse_ast::Cast {
}

Ok(Cast {
target_ty: self.target_ty,
target_ty: self.target_ty.clone(),
span: self.span,
ty_info: TyInfo {
ty: self.target_ty,
return_ty: value.get_ty_info().return_ty,
ty: self.target_ty.clone(),
return_ty: value.get_ty_info().return_ty.clone(),
},
value: Box::new(value),
})
Expand Down
12 changes: 6 additions & 6 deletions src/stage/type_check/expression/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl parse_ast::If {
let condition = self.condition.ty_solve(compiler, scope)?;
let condition_ty = condition.get_ty_info();
if !condition_ty.ty.check(&Ty::Boolean) {
return Err(TyError::Mismatch(Ty::Boolean, condition_ty.ty));
return Err(TyError::Mismatch(Ty::Boolean, condition_ty.ty.clone()));
}

let success = self.success.ty_solve(compiler, scope)?;
Expand All @@ -20,19 +20,19 @@ impl parse_ast::If {
let ty_info = TyInfo::try_from((
// Branches must have the same type
[
success.ty_info.ty,
success.ty_info.ty.clone(),
otherwise
.as_ref()
.map(|otherwise| otherwise.ty_info.ty)
.map(|otherwise| otherwise.ty_info.ty.clone())
.unwrap_or(Ty::Unit),
],
// Any potential place for a return statement must be accounted for
[
condition_ty.return_ty,
success.ty_info.return_ty,
condition_ty.return_ty.clone(),
success.ty_info.return_ty.clone(),
otherwise
.as_ref()
.and_then(|otherwise| otherwise.ty_info.return_ty),
.and_then(|otherwise| otherwise.ty_info.return_ty.clone()),
],
))?;

Expand Down
7 changes: 5 additions & 2 deletions src/stage/type_check/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl InfixOperation {
Ok(Ty::Boolean)
}
(And | Or, Ty::Boolean, Ty::Boolean) => Ok(Ty::Boolean),
(_, left, right) => Err(TyError::Mismatch(*left, *right)),
(_, left, right) => Err(TyError::Mismatch(left.clone(), right.clone())),
}
}
}
Expand All @@ -32,7 +32,10 @@ impl parse_ast::Infix {
// Resulting type is whatever the infix operator results in
self.operation
.result_ty(&left_ty_info.ty, &right_ty_info.ty)?,
[left_ty_info.return_ty, right_ty_info.return_ty],
[
left_ty_info.return_ty.clone(),
right_ty_info.return_ty.clone(),
],
))?;

Ok(Infix {
Expand Down
8 changes: 4 additions & 4 deletions src/stage/type_check/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl parse_ast::Function {
let parameters = self
.parameters
.iter()
.map(|(symbol, ty)| (scope.register(*symbol, *ty), *ty))
.map(|(symbol, ty)| (scope.register(*symbol, ty.clone()), ty.clone()))
.collect();

// Type check the body, allowing it to use the function's scope
Expand All @@ -34,9 +34,9 @@ impl parse_ast::Function {
.for_each(|(binding, symbol, ty)| function.register_binding(binding, symbol, ty));

// If the body contains any return statements, they must match the annotated return statement
if let Some(return_ty) = body.ty_info.return_ty {
if !self.return_ty.check(&return_ty) {
return Err(TyError::Mismatch(self.return_ty, return_ty));
if let Some(return_ty) = &body.ty_info.return_ty {
if !self.return_ty.check(return_ty) {
return Err(TyError::Mismatch(self.return_ty, return_ty.clone()));
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/stage/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ pub struct FunctionSignature {
impl<M: AstMetadata> From<&base_ast::Function<M>> for FunctionSignature {
fn from(function: &base_ast::Function<M>) -> Self {
Self {
arguments: function.parameters.iter().map(|(_, ty)| *ty).collect(),
return_ty: function.return_ty,
arguments: function
.parameters
.iter()
.map(|(_, ty)| ty.clone())
.collect(),
return_ty: function.return_ty.clone(),
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/stage/type_check/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ impl parse_ast::LetStatement {
if let Some(ty) = self.ty_info {
let value_ty = value.get_ty_info();
if !ty.check(&value_ty.ty) {
return Err(TyError::Mismatch(ty, value_ty.ty));
return Err(TyError::Mismatch(ty, value_ty.ty.clone()));
}
}

// Record the type
let binding = scope.register(self.binding, value.get_ty_info().ty);
let binding = scope.register(self.binding, value.get_ty_info().ty.clone());

Ok(LetStatement {
ty_info: TyInfo {
ty: Ty::Unit,
return_ty: value.get_ty_info().return_ty,
return_ty: value.get_ty_info().return_ty.clone(),
},
binding,
value,
Expand All @@ -63,7 +63,10 @@ impl parse_ast::ReturnStatement {
Ok(ReturnStatement {
ty_info: TyInfo::try_from((
Ty::Never,
[Some(value.get_ty_info().ty), value.get_ty_info().return_ty],
[
Some(value.get_ty_info().ty.clone()),
value.get_ty_info().return_ty.clone(),
],
))?,
value,
span: self.span,
Expand Down
10 changes: 6 additions & 4 deletions src/util/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ impl Scope {
// Only consider bindings that match the symbol
.filter(|(_, (test_symbol, _))| *test_symbol == symbol)
// Generate the scoped binding representation to track which scope the binding originated from
.map(move |(binding_idx, (_, ty))| (ScopedBinding(scope_idx, binding_idx), *ty))
.map(move |(binding_idx, (_, ty))| {
(ScopedBinding(scope_idx, binding_idx), ty.clone())
})
})
.next_back()
}

/// Get the information for a binding.
pub fn get_binding(&self, binding: &ScopedBinding) -> (Symbol, Ty) {
self.scopes[binding.0].bindings[binding.1]
self.scopes[binding.0].bindings[binding.1].clone()
}

/// Find the currently activated scope identifier.
Expand Down Expand Up @@ -164,13 +166,13 @@ mod test {
// Convert all the identifier numbers into `Symbol`s.
let variables = variables
.iter()
.map(|(symbol, ty)| (Symbol::try_from_usize(*symbol).unwrap(), *ty))
.map(|(symbol, ty)| (Symbol::try_from_usize(*symbol).unwrap(), ty.clone()))
.collect::<Vec<_>>();

// Register all of the variables and capture the bindings
let bindings = variables
.iter()
.map(|(symbol, ty)| scope.register(*symbol, *ty))
.map(|(symbol, ty)| scope.register(*symbol, ty.clone()))
.collect::<Vec<_>>();

// Resolve each of the bindings and verify they match
Expand Down

0 comments on commit 28847a8

Please sign in to comment.