Skip to content

Commit

Permalink
Use panic to denote bugs in the check stage
Browse files Browse the repository at this point in the history
  • Loading branch information
JSAbrahams committed Jan 4, 2023
1 parent 14b36a8 commit f497055
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
10 changes: 4 additions & 6 deletions src/check/constrain/constraint/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::collections::HashMap;
use crate::check::constrain::constraint::Constraint;
use crate::check::constrain::constraint::expected::Expected;
use crate::check::constrain::constraint::iterator::Constraints;
use crate::check::result::{TypeErr, TypeResult};
use crate::common::delimit::comma_delm;
use crate::common::position::Position;

pub type VarMapping = HashMap<String, usize>;

Expand Down Expand Up @@ -74,14 +72,15 @@ impl ConstrBuilder {
///
/// - Error if already top-level.
/// - Error if level greater than ceiling, as we cannot exit non-existent sets.
pub fn exit_set_to(&mut self, level: usize, pos: Position) -> TypeResult<()> {
pub fn exit_set_to(&mut self, level: usize) {
let msg_exit = format!("Exit set to level {}", level - 1);

let level = max(1, level);
if level == 0 {
return Err(vec![TypeErr::new(pos, "Cannot exit top-level set")]);
panic!("Cannot exit top-level set");
} else if level > self.constraints.len() {
return Err(vec![TypeErr::new(pos, "Exiting constraint set which doesn't exist")]);
panic!("Exiting constraint set which doesn't exist\nlevel: {}, constraints: {}, finished: {}",
level, self.constraints.len(), self.finished.len());
}

for i in (level - 1..self.constraints.len()).rev() {
Expand All @@ -90,7 +89,6 @@ impl ConstrBuilder {
}

trace!("{msg_exit}: {} active sets, {} complete sets", self.constraints.len(), self.finished.len());
Ok(())
}

/// Add new constraint to constraint builder with a message.
Expand Down
2 changes: 1 addition & 1 deletion src/check/constrain/generate/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ pub fn constrain_class_body(
let class_env = env.in_class(&name);
gen_vec(statements, &class_env, true, ctx, constr)?;

constr.exit_set_to(class_lvl, ty.pos)?;
constr.exit_set_to(class_lvl);
Ok(env.clone())
}
4 changes: 2 additions & 2 deletions src/check/constrain/generate/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn gen_flow(
let lookup_env = gen_collection_lookup(expr, col, &col_env.is_def_mode(true), constr)?;

generate(body, &lookup_env.in_loop().is_def_mode(is_define_mode), ctx, constr)?;
constr.exit_set_to(loop_lvl, ast.pos)?;
constr.exit_set_to(loop_lvl);
Ok(env.clone())
}
Node::While { cond, body } => {
Expand All @@ -102,7 +102,7 @@ pub fn gen_flow(

generate(cond, env, ctx, constr)?;
generate(body, &env.in_loop(), ctx, constr)?;
constr.exit_set_to(while_lvl, ast.pos)?;
constr.exit_set_to(while_lvl);
Ok(env.clone())
}

Expand Down
2 changes: 1 addition & 1 deletion src/check/constrain/generate/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn gen_def(
return Err(unassigned.iter().map(|msg| TypeErr::new(id.pos, msg)).collect());
}

constr.exit_set_to(fun_lvl, ast.pos)?;
constr.exit_set_to(fun_lvl);
Ok(env.clone())
}
Node::FunArg { .. } => {
Expand Down
4 changes: 2 additions & 2 deletions src/check/constrain/generate/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn gen_resources(
)?;

generate(expr, &resource_env, ctx, constr)?;
constr.exit_set_to(with_lvl, ast.pos)?;
constr.exit_set_to(with_lvl);
Ok(env.clone())
}
Node::With { resource, expr, .. } => {
Expand All @@ -57,7 +57,7 @@ pub fn gen_resources(

let resource_env = generate(resource, env, ctx, constr)?;

constr.exit_set_to(with_lvl, ast.pos)?;
constr.exit_set_to(with_lvl);
generate(expr, &resource_env, ctx, constr)?;
Ok(env.clone())
}
Expand Down

0 comments on commit f497055

Please sign in to comment.