remove Content -> SolvedType conversion

This commit is contained in:
Folkert 2022-03-19 12:22:33 +01:00
parent da0c6adff3
commit 5a15a121ff
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
2 changed files with 3 additions and 220 deletions

View file

@ -3,7 +3,7 @@ use roc_can::constraint::{Constraint as ConstraintSoa, Constraints};
use roc_can::module::RigidVariables;
use roc_collections::all::MutMap;
use roc_module::symbol::Symbol;
use roc_types::solved_types::{Solved, SolvedType};
use roc_types::solved_types::Solved;
use roc_types::subs::{StorageSubs, Subs, Variable};
use roc_types::types::Alias;
@ -60,26 +60,6 @@ pub fn run_solve(
(solved_subs, solved_env, problems)
}
pub fn make_solved_types(
solved_subs: &Solved<Subs>,
exposed_vars_by_symbol: &[(Symbol, Variable)],
) -> MutMap<Symbol, SolvedType> {
let mut solved_types = MutMap::default();
// exposed_vars_by_symbol contains the Variables for all the Symbols
// this module exposes. We want to convert those into flat SolvedType
// annotations which are decoupled from our Subs, because that's how
// other modules will generate constraints for imported values
// within the context of their own Subs.
for (symbol, var) in exposed_vars_by_symbol.iter() {
let solved_type = SolvedType::new(solved_subs, *var);
solved_types.insert(*symbol, solved_type);
}
solved_types
}
pub fn exposed_types_storage_subs(
solved_subs: &mut Solved<Subs>,
exposed_vars_by_symbol: &[(Symbol, Variable)],

View file

@ -1,6 +1,6 @@
use crate::subs::{FlatType, GetSubsSlice, Subs, VarId, VarStore, Variable};
use crate::subs::{VarId, VarStore, Variable};
use crate::types::{AliasKind, Problem, RecordField, Type, TypeExtension};
use roc_collections::all::{ImMap, MutSet, SendMap};
use roc_collections::all::{ImMap, SendMap};
use roc_module::ident::{Lowercase, TagName};
use roc_module::symbol::Symbol;
use roc_region::all::{Loc, Region};
@ -73,186 +73,6 @@ pub enum SolvedType {
Error,
}
impl SolvedType {
pub fn new(solved_subs: &Solved<Subs>, var: Variable) -> Self {
Self::from_var(solved_subs.inner(), var)
}
fn from_var(subs: &Subs, var: Variable) -> Self {
let mut seen = RecursionVars::default();
Self::from_var_help(subs, &mut seen, var)
}
fn from_var_help(subs: &Subs, recursion_vars: &mut RecursionVars, var: Variable) -> Self {
use crate::subs::Content::*;
// if this is a recursion var we've seen before, just generate a Flex
// (not doing so would have this function loop forever)
if recursion_vars.contains(subs, var) {
return SolvedType::Flex(VarId::from_var(var, subs));
}
match subs.get_content_without_compacting(var) {
FlexVar(_) => SolvedType::Flex(VarId::from_var(var, subs)),
RecursionVar { structure, .. } => {
// TODO should there be a SolvedType RecursionVar variant?
Self::from_var_help(subs, recursion_vars, *structure)
}
RigidVar(name_index) => {
let name = &subs.field_names[name_index.index as usize];
SolvedType::Rigid(name.clone())
}
Structure(flat_type) => Self::from_flat_type(subs, recursion_vars, flat_type),
Alias(symbol, args, actual_var, kind) => {
let mut new_args = Vec::with_capacity(args.len());
for var_index in args.named_type_arguments() {
let arg_var = subs[var_index];
let node = Self::from_var_help(subs, recursion_vars, arg_var);
// NOTE we fake the lowercase here: the user will never get to see it anyway
new_args.push((Lowercase::default(), node));
}
let mut solved_lambda_sets = Vec::with_capacity(0);
for var_index in args.unnamed_type_arguments() {
let var = subs[var_index];
solved_lambda_sets.push(SolvedLambdaSet(Self::from_var_help(
subs,
recursion_vars,
var,
)));
}
let aliased_to = Self::from_var_help(subs, recursion_vars, *actual_var);
SolvedType::Alias(
*symbol,
new_args,
solved_lambda_sets,
Box::new(aliased_to),
*kind,
)
}
RangedNumber(typ, _range_vars) => Self::from_var_help(subs, recursion_vars, *typ),
Error => SolvedType::Error,
}
}
fn from_flat_type(
subs: &Subs,
recursion_vars: &mut RecursionVars,
flat_type: &FlatType,
) -> Self {
use crate::subs::FlatType::*;
match flat_type {
Apply(symbol, args) => {
let mut new_args = Vec::with_capacity(args.len());
for var in subs.get_subs_slice(*args) {
new_args.push(Self::from_var_help(subs, recursion_vars, *var));
}
SolvedType::Apply(*symbol, new_args)
}
Func(args, closure, ret) => {
let mut new_args = Vec::with_capacity(args.len());
for var in subs.get_subs_slice(*args) {
new_args.push(Self::from_var_help(subs, recursion_vars, *var));
}
let ret = Self::from_var_help(subs, recursion_vars, *ret);
let closure = Self::from_var_help(subs, recursion_vars, *closure);
SolvedType::Func(new_args, Box::new(closure), Box::new(ret))
}
Record(fields, ext_var) => {
let mut new_fields = Vec::with_capacity(fields.len());
for (i1, i2, i3) in fields.iter_all() {
let field_name: Lowercase = subs[i1].clone();
let variable: Variable = subs[i2];
let record_field: RecordField<()> = subs[i3];
let solved_type =
record_field.map(|_| Self::from_var_help(subs, recursion_vars, variable));
new_fields.push((field_name, solved_type));
}
let ext = Self::from_var_help(subs, recursion_vars, *ext_var);
SolvedType::Record {
fields: new_fields,
ext: Box::new(ext),
}
}
TagUnion(tags, ext_var) => {
let mut new_tags = Vec::with_capacity(tags.len());
for (name_index, slice_index) in tags.iter_all() {
let slice = subs[slice_index];
let mut new_args = Vec::with_capacity(slice.len());
for var_index in slice {
let var = subs[var_index];
new_args.push(Self::from_var_help(subs, recursion_vars, var));
}
let tag_name = subs[name_index].clone();
new_tags.push((tag_name.clone(), new_args));
}
let ext = Self::from_var_help(subs, recursion_vars, *ext_var);
SolvedType::TagUnion(new_tags, Box::new(ext))
}
FunctionOrTagUnion(tag_name, symbol, ext_var) => {
let ext = Self::from_var_help(subs, recursion_vars, *ext_var);
SolvedType::FunctionOrTagUnion(subs[*tag_name].clone(), *symbol, Box::new(ext))
}
RecursiveTagUnion(rec_var, tags, ext_var) => {
recursion_vars.insert(subs, *rec_var);
let mut new_tags = Vec::with_capacity(tags.len());
for (name_index, slice_index) in tags.iter_all() {
let slice = subs[slice_index];
let mut new_args = Vec::with_capacity(slice.len());
for var_index in slice {
let var = subs[var_index];
new_args.push(Self::from_var_help(subs, recursion_vars, var));
}
let tag_name = subs[name_index].clone();
new_tags.push((tag_name.clone(), new_args));
}
let ext = Self::from_var_help(subs, recursion_vars, *ext_var);
SolvedType::RecursiveTagUnion(
VarId::from_var(*rec_var, subs),
new_tags,
Box::new(ext),
)
}
EmptyRecord => SolvedType::EmptyRecord,
EmptyTagUnion => SolvedType::EmptyTagUnion,
Erroneous(problem_index) => {
let problem = subs.problems[problem_index.index as usize].clone();
SolvedType::Erroneous(problem)
}
}
}
}
#[derive(Clone, Debug)]
pub struct BuiltinAlias {
pub region: Region,
@ -260,23 +80,6 @@ pub struct BuiltinAlias {
pub typ: SolvedType,
}
#[derive(Default)]
struct RecursionVars(MutSet<Variable>);
impl RecursionVars {
fn contains(&self, subs: &Subs, var: Variable) -> bool {
let var = subs.get_root_key_without_compacting(var);
self.0.contains(&var)
}
fn insert(&mut self, subs: &Subs, var: Variable) {
let var = subs.get_root_key_without_compacting(var);
self.0.insert(var);
}
}
#[derive(Debug, Clone, Default)]
pub struct FreeVars {
pub named_vars: ImMap<Lowercase, Variable>,