From ec099bbdec5fc61809a8b76e869e09c143fd59a6 Mon Sep 17 00:00:00 2001 From: Folkert Date: Wed, 2 Mar 2022 14:46:30 +0100 Subject: [PATCH] WIP --- ast/src/constrain.rs | 26 ++++----- compiler/can/src/constraint_soa.rs | 93 ++++++++++-------------------- compiler/collections/src/all.rs | 10 ++-- compiler/collections/src/lib.rs | 1 + compiler/collections/src/soa.rs | 18 ++++++ compiler/constrain/src/expr.rs | 30 +++++----- compiler/constrain/src/lib.rs | 1 + compiler/constrain/src/pattern.rs | 4 +- compiler/exhaustive/src/lib.rs | 4 +- compiler/mono/src/exhaustive.rs | 4 +- compiler/types/src/types.rs | 24 ++++---- reporting/src/error/type.rs | 6 +- 12 files changed, 103 insertions(+), 118 deletions(-) diff --git a/ast/src/constrain.rs b/ast/src/constrain.rs index 6c5af07eaf..bd00bbbda5 100644 --- a/ast/src/constrain.rs +++ b/ast/src/constrain.rs @@ -1,7 +1,7 @@ use bumpalo::{collections::Vec as BumpVec, Bump}; use roc_can::expected::{Expected, PExpected}; -use roc_collections::all::{BumpMap, BumpMapDefault, Index, SendMap}; +use roc_collections::all::{BumpMap, BumpMapDefault, HumanIndex, SendMap}; use roc_module::{ ident::{Lowercase, TagName}, symbol::Symbol, @@ -163,7 +163,7 @@ pub fn constrain_expr<'a>( let elem_expected = Expected::ForReason( Reason::ElemInList { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, list_elem_type.shallow_clone(), region, @@ -339,7 +339,7 @@ pub fn constrain_expr<'a>( let reason = Reason::FnArg { name: opt_symbol, - arg_index: Index::zero_based(index), + arg_index: HumanIndex::zero_based(index), }; let expected_arg = Expected::ForReason(reason, arg_type.shallow_clone(), region); @@ -538,7 +538,7 @@ pub fn constrain_expr<'a>( name.clone(), arity, AnnotationSource::TypedIfBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), num_branches, region: ann_source.region(), }, @@ -559,7 +559,7 @@ pub fn constrain_expr<'a>( name, arity, AnnotationSource::TypedIfBranch { - index: Index::zero_based(branches.len()), + index: HumanIndex::zero_based(branches.len()), num_branches, region: ann_source.region(), }, @@ -596,7 +596,7 @@ pub fn constrain_expr<'a>( body, Expected::ForReason( Reason::IfBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), total_branches: branches.len(), }, Type2::Variable(*expr_var), @@ -616,7 +616,7 @@ pub fn constrain_expr<'a>( final_else_expr, Expected::ForReason( Reason::IfBranch { - index: Index::zero_based(branches.len()), + index: HumanIndex::zero_based(branches.len()), total_branches: branches.len() + 1, }, Type2::Variable(*expr_var), @@ -691,7 +691,7 @@ pub fn constrain_expr<'a>( when_branch, PExpected::ForReason( PReason::WhenMatch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, cond_type.shallow_clone(), pattern_region, @@ -700,7 +700,7 @@ pub fn constrain_expr<'a>( name.clone(), *arity, AnnotationSource::TypedWhenBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), region: ann_source.region(), }, typ.shallow_clone(), @@ -733,14 +733,14 @@ pub fn constrain_expr<'a>( when_branch, PExpected::ForReason( PReason::WhenMatch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, cond_type.shallow_clone(), pattern_region, ), Expected::ForReason( Reason::WhenBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, branch_type.shallow_clone(), // TODO: when_branch.value.region, @@ -1065,7 +1065,7 @@ pub fn constrain_expr<'a>( let reason = Reason::LowLevelOpArg { op: *op, - arg_index: Index::zero_based(index), + arg_index: HumanIndex::zero_based(index), }; let expected_arg = Expected::ForReason(reason, arg_type.shallow_clone(), Region::zero()); @@ -1681,7 +1681,7 @@ fn constrain_tag_pattern<'a>( let expected = PExpected::ForReason( PReason::TagArg { tag_name: tag_name.clone(), - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, pattern_type, region, diff --git a/compiler/can/src/constraint_soa.rs b/compiler/can/src/constraint_soa.rs index 05e1652340..7edde7e960 100644 --- a/compiler/can/src/constraint_soa.rs +++ b/compiler/can/src/constraint_soa.rs @@ -1,7 +1,7 @@ use crate::expected::{Expected, PExpected}; -use roc_collections::all::{MutSet, SendMap}; +use roc_collections::soa::{Index, Slice}; use roc_module::symbol::Symbol; -use roc_region::all::{Located, Region}; +use roc_region::all::{Loc, Region}; use roc_types::types::{Category, PatternCategory, Type}; use roc_types::{subs::Variable, types::VariableDetail}; @@ -9,7 +9,7 @@ pub struct Constraints { constraints: Vec, types: Vec, variables: Vec, - def_types: Vec<(Symbol, Located>)>, + def_types: Vec<(Symbol, Loc>)>, let_constraints: Vec, categories: Vec, pattern_categories: Vec, @@ -18,6 +18,26 @@ pub struct Constraints { } impl Constraints { + pub const EMPTY_RECORD: Index = Index::new(0); + pub const EMPTY_TAG_UNION: Index = Index::new(1); + + pub const CATEGORY_RECORD: Index = Index::new(0); + + pub fn push_type(&mut self, typ: Type) -> Index { + match typ { + Type::EmptyRec => Self::EMPTY_RECORD, + Type::EmptyTagUnion => Self::EMPTY_TAG_UNION, + other => Index::push_new(&mut self.types, other), + } + } + + pub fn push_category(&mut self, category: Category) -> Index { + match category { + Category::Record => Self::CATEGORY_RECORD, + other => Index::push_new(&mut self.categories, other), + } + } + pub fn equal_types( &mut self, typ: Type, @@ -65,19 +85,18 @@ impl Constraints { Slice::new(start as _, length as _) } - fn def_types_slice(&mut self, it: I) -> Slice<(Symbol, Located)> + fn def_types_slice(&mut self, it: I) -> Slice<(Symbol, Loc)> where - I: IntoIterator)>, + I: IntoIterator)>, { let start = self.def_types.len(); for (symbol, loc_type) in it { let type_index = Index::new(self.types.len() as _); - let Located { region, value } = loc_type; + let Loc { region, value } = loc_type; self.types.push(value); - self.def_types - .push((symbol, Located::at(region, type_index))); + self.def_types.push((symbol, Loc::at(region, type_index))); } let length = self.def_types.len() - start; @@ -118,7 +137,7 @@ impl Constraints { where I1: IntoIterator, I2: IntoIterator, - I3: IntoIterator)>, + I3: IntoIterator)>, { let defs_and_ret_constraint = Index::new(self.constraints.len() as _); @@ -163,60 +182,6 @@ pub enum Constraint { pub struct LetConstraint { pub rigid_vars: Slice, pub flex_vars: Slice, - pub def_types: Slice<(Symbol, Located)>, + pub def_types: Slice<(Symbol, Loc)>, pub defs_and_ret_constraint: Index<(Constraint, Constraint)>, } - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Index { - index: u32, - _marker: std::marker::PhantomData, -} - -impl Index { - pub const fn new(index: u32) -> Self { - Self { - index, - _marker: std::marker::PhantomData, - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Slice { - start: u32, - length: u16, - _marker: std::marker::PhantomData, -} - -impl Default for Slice { - fn default() -> Self { - Self::new(0, 0) - } -} - -impl Slice { - pub const fn new(start: u32, length: u16) -> Self { - Self { - start, - length, - _marker: std::marker::PhantomData, - } - } - - pub const fn len(&self) -> usize { - self.length as _ - } - - pub const fn is_empty(&self) -> bool { - self.length == 0 - } - - pub const fn indices(&self) -> std::ops::Range { - self.start as usize..(self.start as usize + self.length as usize) - } - - pub fn into_iter(&self) -> impl Iterator> { - self.indices().map(|i| Index::new(i as _)) - } -} diff --git a/compiler/collections/src/all.rs b/compiler/collections/src/all.rs index efd74751bb..b5874be5bb 100644 --- a/compiler/collections/src/all.rs +++ b/compiler/collections/src/all.rs @@ -161,13 +161,13 @@ where } #[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub struct Index(usize); +pub struct HumanIndex(usize); -impl Index { - pub const FIRST: Self = Index(0); +impl HumanIndex { + pub const FIRST: Self = HumanIndex(0); pub fn zero_based(i: usize) -> Self { - Index(i) + HumanIndex(i) } pub fn to_zero_based(self) -> usize { @@ -175,7 +175,7 @@ impl Index { } pub fn one_based(i: usize) -> Self { - Index(i - 1) + HumanIndex(i - 1) } pub fn ordinal(self) -> std::string::String { diff --git a/compiler/collections/src/lib.rs b/compiler/collections/src/lib.rs index 885d50b458..16f8d165dc 100644 --- a/compiler/collections/src/lib.rs +++ b/compiler/collections/src/lib.rs @@ -3,3 +3,4 @@ #![allow(clippy::large_enum_variant)] pub mod all; +pub mod soa; diff --git a/compiler/collections/src/soa.rs b/compiler/collections/src/soa.rs index 6c9247b020..ad5bae89d0 100644 --- a/compiler/collections/src/soa.rs +++ b/compiler/collections/src/soa.rs @@ -11,6 +11,14 @@ impl Index { _marker: std::marker::PhantomData, } } + + pub fn push_new(vector: &mut Vec, value: T) -> Index { + let index = Self::new(vector.len() as _); + + vector.push(value); + + index + } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -20,6 +28,16 @@ pub struct Slice { _marker: std::marker::PhantomData, } +impl Default for Slice { + fn default() -> Self { + Self { + start: Default::default(), + length: Default::default(), + _marker: Default::default(), + } + } +} + impl Slice { pub const fn new(start: u32, length: u16) -> Self { Self { diff --git a/compiler/constrain/src/expr.rs b/compiler/constrain/src/expr.rs index da99c6c50e..cbced89851 100644 --- a/compiler/constrain/src/expr.rs +++ b/compiler/constrain/src/expr.rs @@ -11,7 +11,7 @@ use roc_can::expected::PExpected; use roc_can::expr::Expr::{self, *}; use roc_can::expr::{ClosureData, Field, WhenBranch}; use roc_can::pattern::Pattern; -use roc_collections::all::{ImMap, Index, MutSet, SendMap}; +use roc_collections::all::{HumanIndex, ImMap, MutSet, SendMap}; use roc_module::ident::{Lowercase, TagName}; use roc_module::symbol::{ModuleId, Symbol}; use roc_region::all::{Loc, Region}; @@ -229,7 +229,7 @@ pub fn constrain_expr( for (index, loc_elem) in loc_elems.iter().enumerate() { let elem_expected = ForReason( Reason::ElemInList { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, list_elem_type.clone(), loc_elem.region, @@ -293,7 +293,7 @@ pub fn constrain_expr( let reason = Reason::FnArg { name: opt_symbol, - arg_index: Index::zero_based(index), + arg_index: HumanIndex::zero_based(index), }; let expected_arg = ForReason(reason, arg_type.clone(), region); let arg_con = constrain_expr(env, loc_arg.region, &loc_arg.value, expected_arg); @@ -462,7 +462,7 @@ pub fn constrain_expr( name.clone(), arity, AnnotationSource::TypedIfBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), num_branches, region: ann_source.region(), }, @@ -482,7 +482,7 @@ pub fn constrain_expr( name, arity, AnnotationSource::TypedIfBranch { - index: Index::zero_based(branches.len()), + index: HumanIndex::zero_based(branches.len()), num_branches, region: ann_source.region(), }, @@ -517,7 +517,7 @@ pub fn constrain_expr( &loc_body.value, ForReason( Reason::IfBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), total_branches: branches.len(), }, Type::Variable(*branch_var), @@ -534,7 +534,7 @@ pub fn constrain_expr( &final_else.value, ForReason( Reason::IfBranch { - index: Index::zero_based(branches.len()), + index: HumanIndex::zero_based(branches.len()), total_branches: branches.len() + 1, }, Type::Variable(*branch_var), @@ -592,7 +592,7 @@ pub fn constrain_expr( when_branch, PExpected::ForReason( PReason::WhenMatch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, cond_type.clone(), pattern_region, @@ -601,7 +601,7 @@ pub fn constrain_expr( name.clone(), *arity, AnnotationSource::TypedWhenBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), region: ann_source.region(), }, typ.clone(), @@ -629,14 +629,14 @@ pub fn constrain_expr( when_branch, PExpected::ForReason( PReason::WhenMatch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, cond_type.clone(), pattern_region, ), ForReason( Reason::WhenBranch { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, branch_type.clone(), when_branch.value.region, @@ -1007,7 +1007,7 @@ pub fn constrain_expr( let mut add_arg = |index, arg_type: Type, arg| { let reason = Reason::LowLevelOpArg { op: *op, - arg_index: Index::zero_based(index), + arg_index: HumanIndex::zero_based(index), }; let expected_arg = ForReason(reason, arg_type.clone(), Region::zero()); let arg_con = constrain_expr(env, Region::zero(), arg, expected_arg); @@ -1053,7 +1053,7 @@ pub fn constrain_expr( let mut add_arg = |index, arg_type: Type, arg| { let reason = Reason::ForeignCallArg { foreign_symbol: foreign_symbol.clone(), - arg_index: Index::zero_based(index), + arg_index: HumanIndex::zero_based(index), }; let expected_arg = ForReason(reason, arg_type.clone(), Region::zero()); let arg_con = constrain_expr(env, Region::zero(), arg, expected_arg); @@ -1320,7 +1320,7 @@ fn constrain_def(env: &Env, def: &Def, body_con: Constraint) -> Constraint { let pattern_expected = PExpected::ForReason( PReason::TypedArg { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), opt_name: opt_label, }, pattern_type.clone(), @@ -1687,7 +1687,7 @@ pub fn rec_defs_help( let pattern_expected = PExpected::ForReason( PReason::TypedArg { - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), opt_name: opt_label, }, pattern_type.clone(), diff --git a/compiler/constrain/src/lib.rs b/compiler/constrain/src/lib.rs index 94067ab076..b95a36ee6d 100644 --- a/compiler/constrain/src/lib.rs +++ b/compiler/constrain/src/lib.rs @@ -5,3 +5,4 @@ pub mod builtins; pub mod expr; pub mod module; pub mod pattern; +pub mod soa_expr; diff --git a/compiler/constrain/src/pattern.rs b/compiler/constrain/src/pattern.rs index 200f38dc74..e7a3f0f7fc 100644 --- a/compiler/constrain/src/pattern.rs +++ b/compiler/constrain/src/pattern.rs @@ -4,7 +4,7 @@ use roc_can::constraint::{Constraint, PresenceConstraint}; use roc_can::expected::{Expected, PExpected}; use roc_can::pattern::Pattern::{self, *}; use roc_can::pattern::{DestructType, RecordDestruct}; -use roc_collections::all::{Index, SendMap}; +use roc_collections::all::{HumanIndex, SendMap}; use roc_module::ident::Lowercase; use roc_module::symbol::Symbol; use roc_region::all::{Loc, Region}; @@ -412,7 +412,7 @@ pub fn constrain_pattern( let expected = PExpected::ForReason( PReason::TagArg { tag_name: tag_name.clone(), - index: Index::zero_based(index), + index: HumanIndex::zero_based(index), }, pattern_type, region, diff --git a/compiler/exhaustive/src/lib.rs b/compiler/exhaustive/src/lib.rs index c80b9b01d0..8ca1c78298 100644 --- a/compiler/exhaustive/src/lib.rs +++ b/compiler/exhaustive/src/lib.rs @@ -1,4 +1,4 @@ -use roc_collections::all::{Index, MutMap}; +use roc_collections::all::{HumanIndex, MutMap}; use roc_module::ident::{Lowercase, TagIdIntType, TagName}; use roc_region::all::Region; use roc_std::RocDec; @@ -70,7 +70,7 @@ pub enum Error { Redundant { overall_region: Region, branch_region: Region, - index: Index, + index: HumanIndex, }, } diff --git a/compiler/mono/src/exhaustive.rs b/compiler/mono/src/exhaustive.rs index cafbf8bbf9..ceaea825c6 100644 --- a/compiler/mono/src/exhaustive.rs +++ b/compiler/mono/src/exhaustive.rs @@ -1,5 +1,5 @@ use crate::ir::DestructType; -use roc_collections::all::Index; +use roc_collections::all::HumanIndex; use roc_exhaustive::{ is_useful, Context, Ctor, Error, Guard, Literal, Pattern, RenderAs, TagId, Union, }; @@ -189,7 +189,7 @@ fn to_nonredundant_rows( return Err(Error::Redundant { overall_region, branch_region: region, - index: Index::zero_based(checked_rows.len()), + index: HumanIndex::zero_based(checked_rows.len()), }); } } diff --git a/compiler/types/src/types.rs b/compiler/types/src/types.rs index 298fd32be8..6bc9b7ed9e 100644 --- a/compiler/types/src/types.rs +++ b/compiler/types/src/types.rs @@ -2,7 +2,7 @@ use crate::pretty_print::Parens; use crate::subs::{ GetSubsSlice, RecordFields, Subs, UnionTags, VarStore, Variable, VariableSubsSlice, }; -use roc_collections::all::{ImMap, ImSet, Index, MutSet, SendMap}; +use roc_collections::all::{HumanIndex, ImMap, ImSet, MutSet, SendMap}; use roc_error_macros::internal_error; use roc_module::called_via::CalledVia; use roc_module::ident::{ForeignSymbol, Ident, Lowercase, TagName}; @@ -1203,14 +1203,14 @@ pub struct TagUnionStructure<'a> { pub enum PReason { TypedArg { opt_name: Option, - index: Index, + index: HumanIndex, }, WhenMatch { - index: Index, + index: HumanIndex, }, TagArg { tag_name: TagName, - index: Index, + index: HumanIndex, }, PatternGuard, OptionalField, @@ -1219,12 +1219,12 @@ pub enum PReason { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AnnotationSource { TypedIfBranch { - index: Index, + index: HumanIndex, num_branches: usize, region: Region, }, TypedWhenBranch { - index: Index, + index: HumanIndex, region: Region, }, TypedBody { @@ -1246,7 +1246,7 @@ impl AnnotationSource { pub enum Reason { FnArg { name: Option, - arg_index: Index, + arg_index: HumanIndex, }, FnCall { name: Option, @@ -1254,28 +1254,28 @@ pub enum Reason { }, LowLevelOpArg { op: LowLevel, - arg_index: Index, + arg_index: HumanIndex, }, ForeignCallArg { foreign_symbol: ForeignSymbol, - arg_index: Index, + arg_index: HumanIndex, }, FloatLiteral, IntLiteral, NumLiteral, StrInterpolation, WhenBranch { - index: Index, + index: HumanIndex, }, WhenGuard, ExpectCondition, IfCondition, IfBranch { - index: Index, + index: HumanIndex, total_branches: usize, }, ElemInList { - index: Index, + index: HumanIndex, }, RecordUpdateValue(Lowercase), RecordUpdateKeys(Symbol, SendMap), diff --git a/reporting/src/error/type.rs b/reporting/src/error/type.rs index ec7597f0aa..5f181c24d4 100644 --- a/reporting/src/error/type.rs +++ b/reporting/src/error/type.rs @@ -1,5 +1,5 @@ use roc_can::expected::{Expected, PExpected}; -use roc_collections::all::{Index, MutSet, SendMap}; +use roc_collections::all::{HumanIndex, MutSet, SendMap}; use roc_module::called_via::{BinOp, CalledVia}; use roc_module::ident::{Ident, IdentStr, Lowercase, TagName}; use roc_module::symbol::Symbol; @@ -350,7 +350,7 @@ fn to_expr_report<'b>( num_branches, .. } if num_branches == 2 => alloc.concat(vec![ - alloc.keyword(if index == Index::FIRST { + alloc.keyword(if index == HumanIndex::FIRST { "then" } else { "else" @@ -1384,7 +1384,7 @@ fn to_pattern_report<'b>( found, expected_type, add_pattern_category( - alloc, + HumanIndexlloc, alloc.text("The first pattern is trying to match"), &category, ),