mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
Break up solve/solve into smaller modules
This commit is contained in:
parent
d1dad56331
commit
8314d44650
11 changed files with 2115 additions and 2057 deletions
|
@ -21,8 +21,7 @@ use roc_types::types::{AliasKind, Category, MemberImpl, PatternCategory, Polarit
|
|||
use roc_unify::unify::{Env, MustImplementConstraints};
|
||||
use roc_unify::unify::{MustImplementAbility, Obligated};
|
||||
|
||||
use crate::solve::type_to_var;
|
||||
use crate::solve::{Aliases, Pools};
|
||||
use crate::{aliases::Aliases, pools::Pools, to_var::type_to_var};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum AbilityImplError {
|
||||
|
|
343
crates/compiler/solve/src/aliases.rs
Normal file
343
crates/compiler/solve/src/aliases.rs
Normal file
|
@ -0,0 +1,343 @@
|
|||
use roc_can::abilities::AbilitiesStore;
|
||||
use roc_collections::{soa::Index, MutMap};
|
||||
use roc_error_macros::internal_error;
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_solve_problem::TypeError;
|
||||
use roc_types::{
|
||||
subs::{AliasVariables, Content, FlatType, Rank, Subs, SubsSlice, TagExt, UnionTags, Variable},
|
||||
types::{Alias, AliasKind, OptAbleVar, Type, TypeTag, Types},
|
||||
};
|
||||
|
||||
use crate::ability::ObligationCache;
|
||||
use crate::pools::Pools;
|
||||
use crate::solve::register;
|
||||
use crate::to_var::type_to_var_help;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct DelayedAliasVariables {
|
||||
start: u32,
|
||||
type_variables_len: u8,
|
||||
lambda_set_variables_len: u8,
|
||||
recursion_variables_len: u8,
|
||||
infer_ext_in_output_variables_len: u8,
|
||||
}
|
||||
|
||||
impl DelayedAliasVariables {
|
||||
fn recursion_variables(self, variables: &mut [OptAbleVar]) -> &mut [OptAbleVar] {
|
||||
let start = self.start as usize
|
||||
+ (self.type_variables_len + self.lambda_set_variables_len) as usize;
|
||||
let length = self.recursion_variables_len as usize;
|
||||
|
||||
&mut variables[start..][..length]
|
||||
}
|
||||
|
||||
fn lambda_set_variables(self, variables: &mut [OptAbleVar]) -> &mut [OptAbleVar] {
|
||||
let start = self.start as usize + self.type_variables_len as usize;
|
||||
let length = self.lambda_set_variables_len as usize;
|
||||
|
||||
&mut variables[start..][..length]
|
||||
}
|
||||
|
||||
fn type_variables(self, variables: &mut [OptAbleVar]) -> &mut [OptAbleVar] {
|
||||
let start = self.start as usize;
|
||||
let length = self.type_variables_len as usize;
|
||||
|
||||
&mut variables[start..][..length]
|
||||
}
|
||||
|
||||
fn infer_ext_in_output_variables(self, variables: &mut [OptAbleVar]) -> &mut [OptAbleVar] {
|
||||
let start = self.start as usize
|
||||
+ (self.type_variables_len
|
||||
+ self.lambda_set_variables_len
|
||||
+ self.recursion_variables_len) as usize;
|
||||
let length = self.infer_ext_in_output_variables_len as usize;
|
||||
|
||||
&mut variables[start..][..length]
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Aliases {
|
||||
aliases: Vec<(Symbol, Index<TypeTag>, DelayedAliasVariables, AliasKind)>,
|
||||
variables: Vec<OptAbleVar>,
|
||||
}
|
||||
|
||||
impl Aliases {
|
||||
pub fn with_capacity(cap: usize) -> Self {
|
||||
Self {
|
||||
aliases: Vec::with_capacity(cap),
|
||||
variables: Vec::with_capacity(cap * 2),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, types: &mut Types, symbol: Symbol, alias: Alias) {
|
||||
let alias_variables =
|
||||
{
|
||||
let start = self.variables.len() as _;
|
||||
|
||||
self.variables.extend(
|
||||
alias
|
||||
.type_variables
|
||||
.iter()
|
||||
.map(|x| OptAbleVar::from(&x.value)),
|
||||
);
|
||||
|
||||
self.variables.extend(alias.lambda_set_variables.iter().map(
|
||||
|x| match x.as_inner() {
|
||||
Type::Variable(v) => OptAbleVar::unbound(*v),
|
||||
_ => unreachable!("lambda set type is not a variable"),
|
||||
},
|
||||
));
|
||||
|
||||
let recursion_variables_len = alias.recursion_variables.len() as _;
|
||||
self.variables.extend(
|
||||
alias
|
||||
.recursion_variables
|
||||
.iter()
|
||||
.copied()
|
||||
.map(OptAbleVar::unbound),
|
||||
);
|
||||
|
||||
self.variables.extend(
|
||||
alias
|
||||
.infer_ext_in_output_variables
|
||||
.iter()
|
||||
.map(|v| OptAbleVar::unbound(*v)),
|
||||
);
|
||||
|
||||
DelayedAliasVariables {
|
||||
start,
|
||||
type_variables_len: alias.type_variables.len() as _,
|
||||
lambda_set_variables_len: alias.lambda_set_variables.len() as _,
|
||||
recursion_variables_len,
|
||||
infer_ext_in_output_variables_len: alias.infer_ext_in_output_variables.len()
|
||||
as _,
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: can we construct Aliases from TypeTag directly?
|
||||
let alias_typ = types.from_old_type(&alias.typ);
|
||||
|
||||
self.aliases
|
||||
.push((symbol, alias_typ, alias_variables, alias.kind));
|
||||
}
|
||||
|
||||
fn instantiate_result_result(
|
||||
subs: &mut Subs,
|
||||
rank: Rank,
|
||||
pools: &mut Pools,
|
||||
alias_variables: AliasVariables,
|
||||
) -> Variable {
|
||||
let tag_names_slice = Subs::RESULT_TAG_NAMES;
|
||||
|
||||
let err_slice = SubsSlice::new(alias_variables.variables_start + 1, 1);
|
||||
let ok_slice = SubsSlice::new(alias_variables.variables_start, 1);
|
||||
|
||||
let variable_slices =
|
||||
SubsSlice::extend_new(&mut subs.variable_slices, [err_slice, ok_slice]);
|
||||
|
||||
let union_tags = UnionTags::from_slices(tag_names_slice, variable_slices);
|
||||
let ext_var = TagExt::Any(Variable::EMPTY_TAG_UNION);
|
||||
let flat_type = FlatType::TagUnion(union_tags, ext_var);
|
||||
let content = Content::Structure(flat_type);
|
||||
|
||||
register(subs, rank, pools, content)
|
||||
}
|
||||
|
||||
/// Build an alias of the form `Num range := range`
|
||||
fn build_num_opaque(
|
||||
subs: &mut Subs,
|
||||
rank: Rank,
|
||||
pools: &mut Pools,
|
||||
symbol: Symbol,
|
||||
range_var: Variable,
|
||||
) -> Variable {
|
||||
let content = Content::Alias(
|
||||
symbol,
|
||||
AliasVariables::insert_into_subs(subs, [range_var], [], []),
|
||||
range_var,
|
||||
AliasKind::Opaque,
|
||||
);
|
||||
|
||||
register(subs, rank, pools, content)
|
||||
}
|
||||
|
||||
fn instantiate_builtin_aliases_real_var(
|
||||
&mut self,
|
||||
subs: &mut Subs,
|
||||
rank: Rank,
|
||||
pools: &mut Pools,
|
||||
symbol: Symbol,
|
||||
alias_variables: AliasVariables,
|
||||
) -> Option<(Variable, AliasKind)> {
|
||||
match symbol {
|
||||
Symbol::RESULT_RESULT => {
|
||||
let var = Self::instantiate_result_result(subs, rank, pools, alias_variables);
|
||||
|
||||
Some((var, AliasKind::Structural))
|
||||
}
|
||||
Symbol::NUM_NUM | Symbol::NUM_INTEGER | Symbol::NUM_FLOATINGPOINT => {
|
||||
// Num range := range | Integer range := range | FloatingPoint range := range
|
||||
let range_var = subs.variables[alias_variables.variables_start as usize];
|
||||
Some((range_var, AliasKind::Opaque))
|
||||
}
|
||||
Symbol::NUM_INT => {
|
||||
// Int range : Num (Integer range)
|
||||
//
|
||||
// build `Integer range := range`
|
||||
let integer_content_var = Self::build_num_opaque(
|
||||
subs,
|
||||
rank,
|
||||
pools,
|
||||
Symbol::NUM_INTEGER,
|
||||
subs.variables[alias_variables.variables_start as usize],
|
||||
);
|
||||
|
||||
// build `Num (Integer range) := Integer range`
|
||||
let num_content_var =
|
||||
Self::build_num_opaque(subs, rank, pools, Symbol::NUM_NUM, integer_content_var);
|
||||
|
||||
Some((num_content_var, AliasKind::Structural))
|
||||
}
|
||||
Symbol::NUM_FRAC => {
|
||||
// Frac range : Num (FloatingPoint range)
|
||||
//
|
||||
// build `FloatingPoint range := range`
|
||||
let fpoint_content_var = Self::build_num_opaque(
|
||||
subs,
|
||||
rank,
|
||||
pools,
|
||||
Symbol::NUM_FLOATINGPOINT,
|
||||
subs.variables[alias_variables.variables_start as usize],
|
||||
);
|
||||
|
||||
// build `Num (FloatingPoint range) := FloatingPoint range`
|
||||
let num_content_var =
|
||||
Self::build_num_opaque(subs, rank, pools, Symbol::NUM_NUM, fpoint_content_var);
|
||||
|
||||
Some((num_content_var, AliasKind::Structural))
|
||||
}
|
||||
Symbol::NUM_SIGNED8 => Some((Variable::SIGNED8, AliasKind::Opaque)),
|
||||
Symbol::NUM_SIGNED16 => Some((Variable::SIGNED16, AliasKind::Opaque)),
|
||||
Symbol::NUM_SIGNED32 => Some((Variable::SIGNED32, AliasKind::Opaque)),
|
||||
Symbol::NUM_SIGNED64 => Some((Variable::SIGNED64, AliasKind::Opaque)),
|
||||
Symbol::NUM_SIGNED128 => Some((Variable::SIGNED128, AliasKind::Opaque)),
|
||||
Symbol::NUM_UNSIGNED8 => Some((Variable::UNSIGNED8, AliasKind::Opaque)),
|
||||
Symbol::NUM_UNSIGNED16 => Some((Variable::UNSIGNED16, AliasKind::Opaque)),
|
||||
Symbol::NUM_UNSIGNED32 => Some((Variable::UNSIGNED32, AliasKind::Opaque)),
|
||||
Symbol::NUM_UNSIGNED64 => Some((Variable::UNSIGNED64, AliasKind::Opaque)),
|
||||
Symbol::NUM_UNSIGNED128 => Some((Variable::UNSIGNED128, AliasKind::Opaque)),
|
||||
Symbol::NUM_BINARY32 => Some((Variable::BINARY32, AliasKind::Opaque)),
|
||||
Symbol::NUM_BINARY64 => Some((Variable::BINARY64, AliasKind::Opaque)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn instantiate_real_var(
|
||||
&mut self,
|
||||
subs: &mut Subs,
|
||||
rank: Rank,
|
||||
pools: &mut Pools,
|
||||
problems: &mut Vec<TypeError>,
|
||||
abilities_store: &AbilitiesStore,
|
||||
obligation_cache: &mut ObligationCache,
|
||||
arena: &bumpalo::Bump,
|
||||
types: &mut Types,
|
||||
symbol: Symbol,
|
||||
alias_variables: AliasVariables,
|
||||
) -> (Variable, AliasKind) {
|
||||
// hardcoded instantiations for builtin aliases
|
||||
if let Some((var, kind)) = Self::instantiate_builtin_aliases_real_var(
|
||||
self,
|
||||
subs,
|
||||
rank,
|
||||
pools,
|
||||
symbol,
|
||||
alias_variables,
|
||||
) {
|
||||
return (var, kind);
|
||||
}
|
||||
|
||||
let (typ, delayed_variables, kind) =
|
||||
match self.aliases.iter().find(|(s, _, _, _)| *s == symbol) {
|
||||
None => internal_error!(
|
||||
"Alias {:?} not registered in delayed aliases! {:?}",
|
||||
symbol,
|
||||
&self.aliases
|
||||
),
|
||||
Some(&(_, typ, delayed_variables, kind)) => (typ, delayed_variables, kind),
|
||||
};
|
||||
|
||||
let mut substitutions: MutMap<_, _> = Default::default();
|
||||
|
||||
let old_type_variables = delayed_variables.type_variables(&mut self.variables);
|
||||
let new_type_variables = &subs.variables[alias_variables.type_variables().indices()];
|
||||
|
||||
for (old, new) in old_type_variables.iter_mut().zip(new_type_variables) {
|
||||
// if constraint gen duplicated a type these variables could be the same
|
||||
// (happens very often in practice)
|
||||
if old.var != *new {
|
||||
substitutions.insert(old.var, *new);
|
||||
}
|
||||
}
|
||||
|
||||
for OptAbleVar {
|
||||
var: rec_var,
|
||||
opt_abilities,
|
||||
} in delayed_variables
|
||||
.recursion_variables(&mut self.variables)
|
||||
.iter_mut()
|
||||
{
|
||||
debug_assert!(opt_abilities.is_none());
|
||||
let new_var = subs.fresh_unnamed_flex_var();
|
||||
substitutions.insert(*rec_var, new_var);
|
||||
}
|
||||
|
||||
let old_lambda_set_variables = delayed_variables.lambda_set_variables(&mut self.variables);
|
||||
let new_lambda_set_variables =
|
||||
&subs.variables[alias_variables.lambda_set_variables().indices()];
|
||||
|
||||
for (old, new) in old_lambda_set_variables
|
||||
.iter_mut()
|
||||
.zip(new_lambda_set_variables)
|
||||
{
|
||||
debug_assert!(old.opt_abilities.is_none());
|
||||
if old.var != *new {
|
||||
substitutions.insert(old.var, *new);
|
||||
}
|
||||
}
|
||||
|
||||
let old_infer_ext_vars =
|
||||
delayed_variables.infer_ext_in_output_variables(&mut self.variables);
|
||||
let new_infer_ext_vars =
|
||||
&subs.variables[alias_variables.infer_ext_in_output_variables().indices()];
|
||||
|
||||
for (old, new) in old_infer_ext_vars.iter_mut().zip(new_infer_ext_vars) {
|
||||
debug_assert!(old.opt_abilities.is_none());
|
||||
if old.var != *new {
|
||||
substitutions.insert(old.var, *new);
|
||||
}
|
||||
}
|
||||
|
||||
let typ = if !substitutions.is_empty() {
|
||||
types.clone_with_variable_substitutions(typ, &substitutions)
|
||||
} else {
|
||||
typ
|
||||
};
|
||||
|
||||
let alias_variable = type_to_var_help(
|
||||
subs,
|
||||
rank,
|
||||
pools,
|
||||
problems,
|
||||
abilities_store,
|
||||
obligation_cache,
|
||||
arena,
|
||||
self,
|
||||
types,
|
||||
typ,
|
||||
false,
|
||||
);
|
||||
(alias_variable, kind)
|
||||
}
|
||||
}
|
375
crates/compiler/solve/src/deep_copy.rs
Normal file
375
crates/compiler/solve/src/deep_copy.rs
Normal file
|
@ -0,0 +1,375 @@
|
|||
use std::ops::ControlFlow;
|
||||
|
||||
use bumpalo::Bump;
|
||||
use roc_error_macros::internal_error;
|
||||
use roc_types::{
|
||||
subs::{
|
||||
self, AliasVariables, Content, Descriptor, FlatType, GetSubsSlice, Mark, OptVariable, Rank,
|
||||
RecordFields, Subs, SubsSlice, TagExt, TupleElems, UnionLabels, Variable,
|
||||
},
|
||||
types::{RecordField, Uls},
|
||||
};
|
||||
|
||||
use crate::pools::Pools;
|
||||
|
||||
pub(crate) fn deep_copy_var_in(
|
||||
subs: &mut Subs,
|
||||
rank: Rank,
|
||||
pools: &mut Pools,
|
||||
var: Variable,
|
||||
arena: &Bump,
|
||||
) -> Variable {
|
||||
let mut visited = bumpalo::collections::Vec::with_capacity_in(256, arena);
|
||||
|
||||
let pool = pools.get_mut(rank);
|
||||
|
||||
let var = subs.get_root_key(var);
|
||||
match deep_copy_var_decision(subs, rank, var) {
|
||||
ControlFlow::Break(copy) => copy,
|
||||
ControlFlow::Continue(copy) => {
|
||||
deep_copy_var_help(subs, rank, pool, &mut visited, var, copy);
|
||||
|
||||
// we have tracked all visited variables, and can now traverse them
|
||||
// in one go (without looking at the UnificationTable) and clear the copy field
|
||||
for var in visited {
|
||||
subs.set_copy_unchecked(var, OptVariable::NONE);
|
||||
}
|
||||
|
||||
copy
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn has_trivial_copy(subs: &Subs, root_var: Variable) -> Option<Variable> {
|
||||
let existing_copy = subs.get_copy_unchecked(root_var);
|
||||
|
||||
if let Some(copy) = existing_copy.into_variable() {
|
||||
Some(copy)
|
||||
} else if subs.get_rank_unchecked(root_var) != Rank::GENERALIZED {
|
||||
Some(root_var)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn deep_copy_var_decision(
|
||||
subs: &mut Subs,
|
||||
max_rank: Rank,
|
||||
var: Variable,
|
||||
) -> ControlFlow<Variable, Variable> {
|
||||
let var = subs.get_root_key(var);
|
||||
if let Some(copy) = has_trivial_copy(subs, var) {
|
||||
ControlFlow::Break(copy)
|
||||
} else {
|
||||
let copy_descriptor = Descriptor {
|
||||
content: Content::Structure(FlatType::EmptyTagUnion),
|
||||
rank: max_rank,
|
||||
mark: Mark::NONE,
|
||||
copy: OptVariable::NONE,
|
||||
};
|
||||
|
||||
let copy = subs.fresh(copy_descriptor);
|
||||
|
||||
// Link the original variable to the new variable. This lets us
|
||||
// avoid making multiple copies of the variable we are instantiating.
|
||||
//
|
||||
// Need to do this before recursively copying to avoid looping.
|
||||
subs.set_mark_unchecked(var, Mark::NONE);
|
||||
subs.set_copy_unchecked(var, copy.into());
|
||||
|
||||
ControlFlow::Continue(copy)
|
||||
}
|
||||
}
|
||||
|
||||
fn deep_copy_var_help(
|
||||
subs: &mut Subs,
|
||||
max_rank: Rank,
|
||||
pool: &mut Vec<Variable>,
|
||||
visited: &mut bumpalo::collections::Vec<'_, Variable>,
|
||||
initial_source: Variable,
|
||||
initial_copy: Variable,
|
||||
) -> Variable {
|
||||
use roc_types::subs::Content::*;
|
||||
use roc_types::subs::FlatType::*;
|
||||
|
||||
struct DeepCopyVarWork {
|
||||
source: Variable,
|
||||
copy: Variable,
|
||||
}
|
||||
|
||||
let initial = DeepCopyVarWork {
|
||||
source: initial_source,
|
||||
copy: initial_copy,
|
||||
};
|
||||
let mut stack = vec![initial];
|
||||
|
||||
macro_rules! work {
|
||||
($variable:expr) => {{
|
||||
let var = subs.get_root_key($variable);
|
||||
match deep_copy_var_decision(subs, max_rank, var) {
|
||||
ControlFlow::Break(copy) => copy,
|
||||
ControlFlow::Continue(copy) => {
|
||||
stack.push(DeepCopyVarWork { source: var, copy });
|
||||
|
||||
copy
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! copy_sequence {
|
||||
($length:expr, $variables:expr) => {{
|
||||
let new_variables = SubsSlice::reserve_into_subs(subs, $length as _);
|
||||
for (target_index, var_index) in (new_variables.indices()).zip($variables) {
|
||||
let var = subs[var_index];
|
||||
let copy_var = work!(var);
|
||||
subs.variables[target_index] = copy_var;
|
||||
}
|
||||
|
||||
new_variables
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! copy_union {
|
||||
($tags:expr) => {{
|
||||
let new_variable_slices = SubsSlice::reserve_variable_slices(subs, $tags.len());
|
||||
|
||||
let it = (new_variable_slices.indices()).zip($tags.variables());
|
||||
for (target_index, index) in it {
|
||||
let slice = subs[index];
|
||||
|
||||
let new_variables = copy_sequence!(slice.len(), slice);
|
||||
subs.variable_slices[target_index] = new_variables;
|
||||
}
|
||||
|
||||
UnionLabels::from_slices($tags.labels(), new_variable_slices)
|
||||
}};
|
||||
}
|
||||
|
||||
// When generalizing annotations with `Openness` extensions
|
||||
// we want to promote them to `Any`, so that usages at
|
||||
// specialized sites can grow unboundedly and are not bound to
|
||||
// openness-polymorphism.
|
||||
macro_rules! copy_tag_ext {
|
||||
($ext:expr) => {
|
||||
TagExt::Any(work!($ext.var()))
|
||||
};
|
||||
}
|
||||
|
||||
while let Some(DeepCopyVarWork { source: var, copy }) = stack.pop() {
|
||||
visited.push(var);
|
||||
pool.push(copy);
|
||||
|
||||
let content = *subs.get_content_unchecked(var);
|
||||
|
||||
// Now we recursively copy the content of the variable.
|
||||
// We have already marked the variable as copied, so we
|
||||
// will not repeat this work or crawl this variable again.
|
||||
match content {
|
||||
Structure(flat_type) => {
|
||||
let new_flat_type = match flat_type {
|
||||
Apply(symbol, arguments) => {
|
||||
let new_arguments = copy_sequence!(arguments.len(), arguments);
|
||||
|
||||
Apply(symbol, new_arguments)
|
||||
}
|
||||
|
||||
Func(arguments, closure_var, ret_var) => {
|
||||
let new_ret_var = work!(ret_var);
|
||||
let new_closure_var = work!(closure_var);
|
||||
|
||||
let new_arguments = copy_sequence!(arguments.len(), arguments);
|
||||
|
||||
Func(new_arguments, new_closure_var, new_ret_var)
|
||||
}
|
||||
|
||||
same @ EmptyRecord | same @ EmptyTuple | same @ EmptyTagUnion => same,
|
||||
|
||||
Record(fields, ext_var) => {
|
||||
let record_fields = {
|
||||
let new_variables =
|
||||
copy_sequence!(fields.len(), fields.iter_variables());
|
||||
|
||||
// When copying a let-generalized record to a specialized region, rigid
|
||||
// optionals just become optionals.
|
||||
let field_types = subs.get_subs_slice(fields.record_fields());
|
||||
let has_rigid_optional_field = field_types
|
||||
.iter()
|
||||
.any(|f| matches!(f, RecordField::RigidOptional(..)));
|
||||
|
||||
let new_field_types_start = if has_rigid_optional_field {
|
||||
let field_types = field_types.to_vec();
|
||||
let slice = SubsSlice::extend_new(
|
||||
&mut subs.record_fields,
|
||||
field_types.into_iter().map(|f| match f {
|
||||
RecordField::RigidOptional(())
|
||||
| RecordField::RigidRequired(()) => internal_error!("Rigid optional/required should be generalized to non-rigid by this point"),
|
||||
|
||||
RecordField::Demanded(_)
|
||||
| RecordField::Required(_)
|
||||
| RecordField::Optional(_) => f,
|
||||
}),
|
||||
);
|
||||
slice.start
|
||||
} else {
|
||||
fields.field_types_start
|
||||
};
|
||||
|
||||
RecordFields {
|
||||
length: fields.length,
|
||||
field_names_start: fields.field_names_start,
|
||||
variables_start: new_variables.start,
|
||||
field_types_start: new_field_types_start,
|
||||
}
|
||||
};
|
||||
|
||||
Record(record_fields, work!(ext_var))
|
||||
}
|
||||
|
||||
Tuple(elems, ext_var) => {
|
||||
let tuple_elems = {
|
||||
let new_variables = copy_sequence!(elems.len(), elems.iter_variables());
|
||||
|
||||
TupleElems {
|
||||
length: elems.length,
|
||||
variables_start: new_variables.start,
|
||||
elem_index_start: elems.elem_index_start,
|
||||
}
|
||||
};
|
||||
|
||||
Tuple(tuple_elems, work!(ext_var))
|
||||
}
|
||||
|
||||
TagUnion(tags, ext_var) => {
|
||||
let union_tags = copy_union!(tags);
|
||||
|
||||
TagUnion(union_tags, copy_tag_ext!(ext_var))
|
||||
}
|
||||
|
||||
FunctionOrTagUnion(tag_name, symbol, ext_var) => {
|
||||
FunctionOrTagUnion(tag_name, symbol, copy_tag_ext!(ext_var))
|
||||
}
|
||||
|
||||
RecursiveTagUnion(rec_var, tags, ext_var) => {
|
||||
let union_tags = copy_union!(tags);
|
||||
|
||||
RecursiveTagUnion(work!(rec_var), union_tags, copy_tag_ext!(ext_var))
|
||||
}
|
||||
};
|
||||
|
||||
subs.set_content_unchecked(copy, Structure(new_flat_type));
|
||||
}
|
||||
|
||||
FlexVar(_) | FlexAbleVar(_, _) | Error => {
|
||||
subs.set_content_unchecked(copy, content);
|
||||
}
|
||||
|
||||
RecursionVar {
|
||||
opt_name,
|
||||
structure,
|
||||
} => {
|
||||
let content = RecursionVar {
|
||||
opt_name,
|
||||
structure: work!(structure),
|
||||
};
|
||||
|
||||
subs.set_content_unchecked(copy, content);
|
||||
}
|
||||
|
||||
RigidVar(name) => {
|
||||
subs.set_content_unchecked(copy, FlexVar(Some(name)));
|
||||
}
|
||||
|
||||
RigidAbleVar(name, ability) => {
|
||||
subs.set_content_unchecked(copy, FlexAbleVar(Some(name), ability));
|
||||
}
|
||||
|
||||
Alias(symbol, arguments, real_type_var, kind) => {
|
||||
let new_variables =
|
||||
copy_sequence!(arguments.all_variables_len, arguments.all_variables());
|
||||
|
||||
let new_arguments = AliasVariables {
|
||||
variables_start: new_variables.start,
|
||||
..arguments
|
||||
};
|
||||
|
||||
let new_real_type_var = work!(real_type_var);
|
||||
let new_content = Alias(symbol, new_arguments, new_real_type_var, kind);
|
||||
|
||||
subs.set_content_unchecked(copy, new_content);
|
||||
}
|
||||
|
||||
LambdaSet(subs::LambdaSet {
|
||||
solved,
|
||||
recursion_var,
|
||||
unspecialized,
|
||||
ambient_function: ambient_function_var,
|
||||
}) => {
|
||||
let lambda_set_var = copy;
|
||||
|
||||
let new_solved = copy_union!(solved);
|
||||
let new_rec_var = recursion_var.map(|v| work!(v));
|
||||
let new_unspecialized = SubsSlice::reserve_uls_slice(subs, unspecialized.len());
|
||||
|
||||
for (new_uls_index, uls_index) in
|
||||
(new_unspecialized.into_iter()).zip(unspecialized.into_iter())
|
||||
{
|
||||
let Uls(var, sym, region) = subs[uls_index];
|
||||
let new_var = work!(var);
|
||||
|
||||
deep_copy_uls_precondition(subs, var, new_var);
|
||||
|
||||
subs[new_uls_index] = Uls(new_var, sym, region);
|
||||
|
||||
subs.uls_of_var.add(new_var, lambda_set_var);
|
||||
}
|
||||
|
||||
let new_ambient_function_var = work!(ambient_function_var);
|
||||
debug_assert_ne!(
|
||||
ambient_function_var, new_ambient_function_var,
|
||||
"lambda set cloned but its ambient function wasn't?"
|
||||
);
|
||||
|
||||
subs.set_content_unchecked(
|
||||
lambda_set_var,
|
||||
LambdaSet(subs::LambdaSet {
|
||||
solved: new_solved,
|
||||
recursion_var: new_rec_var,
|
||||
unspecialized: new_unspecialized,
|
||||
ambient_function: new_ambient_function_var,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
RangedNumber(range) => {
|
||||
let new_content = RangedNumber(range);
|
||||
|
||||
subs.set_content_unchecked(copy, new_content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initial_copy
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn deep_copy_uls_precondition(subs: &Subs, original_var: Variable, new_var: Variable) {
|
||||
if cfg!(debug_assertions) {
|
||||
let content = subs.get_content_without_compacting(original_var);
|
||||
|
||||
debug_assert!(
|
||||
matches!(
|
||||
content,
|
||||
Content::FlexAbleVar(..) | Content::RigidAbleVar(..)
|
||||
),
|
||||
"var in unspecialized lamba set is not bound to an ability, it is {:?}",
|
||||
roc_types::subs::SubsFmtContent(content, subs)
|
||||
);
|
||||
debug_assert!(
|
||||
original_var != new_var,
|
||||
"unspecialized lamba set var was not instantiated"
|
||||
);
|
||||
}
|
||||
}
|
|
@ -8,3 +8,10 @@ pub mod ability;
|
|||
pub mod module;
|
||||
pub mod solve;
|
||||
pub mod specialize;
|
||||
|
||||
mod aliases;
|
||||
pub use aliases::Aliases;
|
||||
mod deep_copy;
|
||||
mod pools;
|
||||
pub use pools::Pools;
|
||||
mod to_var;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::solve::{self, Aliases};
|
||||
use crate::{aliases::Aliases, solve};
|
||||
use roc_can::abilities::{AbilitiesStore, ResolvedImpl};
|
||||
use roc_can::constraint::{Constraint, Constraints};
|
||||
use roc_can::expr::PendingDerives;
|
||||
|
|
59
crates/compiler/solve/src/pools.rs
Normal file
59
crates/compiler/solve/src/pools.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use roc_types::subs::{Rank, Variable};
|
||||
|
||||
const DEFAULT_POOLS: usize = 8;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Pools(Vec<Vec<Variable>>);
|
||||
|
||||
impl Default for Pools {
|
||||
fn default() -> Self {
|
||||
Pools::new(DEFAULT_POOLS)
|
||||
}
|
||||
}
|
||||
|
||||
impl Pools {
|
||||
pub fn new(num_pools: usize) -> Self {
|
||||
Pools(vec![Vec::new(); num_pools])
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, rank: Rank) -> &mut Vec<Variable> {
|
||||
match self.0.get_mut(rank.into_usize()) {
|
||||
Some(reference) => reference,
|
||||
None => panic!("Compiler bug: could not find pool at rank {}", rank),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, rank: Rank) -> &Vec<Variable> {
|
||||
match self.0.get(rank.into_usize()) {
|
||||
Some(reference) => reference,
|
||||
None => panic!("Compiler bug: could not find pool at rank {}", rank),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<'_, Vec<Variable>> {
|
||||
self.0.iter()
|
||||
}
|
||||
|
||||
pub fn split_last(mut self) -> (Vec<Variable>, Vec<Vec<Variable>>) {
|
||||
let last = self
|
||||
.0
|
||||
.pop()
|
||||
.unwrap_or_else(|| panic!("Attempted to split_last() on non-empty Pools"));
|
||||
|
||||
(last, self.0)
|
||||
}
|
||||
|
||||
pub fn extend_to(&mut self, n: usize) {
|
||||
for _ in self.len()..n {
|
||||
self.0.push(Vec::new());
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -25,8 +25,8 @@ use roc_types::{
|
|||
use roc_unify::unify::{unify, Env as UEnv, Mode, MustImplementConstraints};
|
||||
|
||||
use crate::{
|
||||
ability::builtin_module_with_unlisted_ability_impl,
|
||||
solve::{deep_copy_var_in, introduce, Pools},
|
||||
ability::builtin_module_with_unlisted_ability_impl, deep_copy::deep_copy_var_in, pools::Pools,
|
||||
solve::introduce,
|
||||
};
|
||||
|
||||
/// What phase in the compiler is reaching out to specialize lambda sets?
|
||||
|
|
1313
crates/compiler/solve/src/to_var.rs
Normal file
1313
crates/compiler/solve/src/to_var.rs
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue