Skip to content

Commit

Permalink
Stable constraint order using Btree set
Browse files Browse the repository at this point in the history
  • Loading branch information
95th committed Oct 18, 2020
1 parent 4a50723 commit 428fec1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
3 changes: 1 addition & 2 deletions a.just
@@ -1,2 +1 @@
let a = 10;
let b: int = if a { true } else { false };
if true { 10 } else { true }
2 changes: 1 addition & 1 deletion src/lex/span.rs
@@ -1,4 +1,4 @@
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Span {
lo: usize,
hi: usize,
Expand Down
23 changes: 8 additions & 15 deletions src/typeck/constraints.rs
@@ -1,4 +1,4 @@
use std::{collections::HashSet, hash::Hash};
use std::collections::BTreeSet;

use crate::{
lex::Span,
Expand All @@ -10,21 +10,14 @@ use super::{
typed_ast::{TypedBlock, TypedExpr, TypedExprKind, TypedStmt},
};

#[derive(Debug)]
#[derive(Debug, PartialOrd, Ord)]
pub struct Constraint {
pub a: Ty,
pub b: Ty,
pub span_a: Span,
pub span_b: Span,
}

impl Hash for Constraint {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.a.hash(state);
self.b.hash(state);
}
}

impl PartialEq for Constraint {
fn eq(&self, other: &Self) -> bool {
self.a == other.a && self.b == other.b
Expand All @@ -33,19 +26,19 @@ impl PartialEq for Constraint {

impl Eq for Constraint {}

pub fn collect(ast: &mut [TypedStmt]) -> HashSet<Constraint> {
let mut set = HashSet::new();
pub fn collect(ast: &mut [TypedStmt]) -> BTreeSet<Constraint> {
let mut set = BTreeSet::new();
collect_stmts(ast, &mut set);
set
}

pub fn collect_stmts(ast: &mut [TypedStmt], set: &mut HashSet<Constraint>) {
pub fn collect_stmts(ast: &mut [TypedStmt], set: &mut BTreeSet<Constraint>) {
for stmt in ast {
collect_stmt(stmt, set);
}
}

fn collect_stmt(stmt: &mut TypedStmt, set: &mut HashSet<Constraint>) {
fn collect_stmt(stmt: &mut TypedStmt, set: &mut BTreeSet<Constraint>) {
match stmt {
TypedStmt::Expr(e) | TypedStmt::SemiExpr(e) => collect_expr(e, set),
TypedStmt::Let { name, ty, init } => {
Expand All @@ -72,7 +65,7 @@ fn collect_stmt(stmt: &mut TypedStmt, set: &mut HashSet<Constraint>) {
}
}

fn collect_expr(expr: &mut TypedExpr, set: &mut HashSet<Constraint>) {
fn collect_expr(expr: &mut TypedExpr, set: &mut BTreeSet<Constraint>) {
match &mut expr.kind {
TypedExprKind::Binary { op, left, right } => {
collect_expr(left, set);
Expand Down Expand Up @@ -216,7 +209,7 @@ fn collect_expr(expr: &mut TypedExpr, set: &mut HashSet<Constraint>) {
}
}

fn collect_block(block: &mut TypedBlock, set: &mut HashSet<Constraint>) {
fn collect_block(block: &mut TypedBlock, set: &mut BTreeSet<Constraint>) {
collect_stmts(&mut block.stmts, set);
if let Some(TypedStmt::Expr(e)) = block.stmts.last_mut() {
set.insert(Constraint {
Expand Down
2 changes: 1 addition & 1 deletion src/typeck/ty.rs
Expand Up @@ -14,7 +14,7 @@ impl TyContext {
}
}

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub enum Ty {
Var(u64),
Unit,
Expand Down

0 comments on commit 428fec1

Please sign in to comment.