mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-19 03:20:14 +00:00
Use more structs instead of tuples
This commit is contained in:
parent
a9f7961b52
commit
683b586f60
3 changed files with 128 additions and 103 deletions
|
@ -1,6 +1,6 @@
|
|||
use crate::solve::{self, Aliases};
|
||||
use roc_can::abilities::{AbilitiesStore, ResolvedImpl};
|
||||
use roc_can::constraint::{Constraint as ConstraintSoa, Constraints};
|
||||
use roc_can::constraint::{Constraint, Constraints};
|
||||
use roc_can::expr::PendingDerives;
|
||||
use roc_can::module::{ExposedByModule, ResolvedImplementations, RigidVariables};
|
||||
use roc_collections::all::MutMap;
|
||||
|
@ -53,20 +53,41 @@ pub struct SolvedModule {
|
|||
pub exposed_types: ExposedTypesStorageSubs,
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)] // TODO: put params in a context/env var
|
||||
pub struct SolveCtx<'a> {
|
||||
/// The module we are solving.
|
||||
pub home: ModuleId,
|
||||
pub constraints: &'a Constraints,
|
||||
pub root_constraint: Constraint,
|
||||
/// All types introduced in the module. Canonicalized, but not necessarily yet associated with
|
||||
/// a variable substitution.
|
||||
pub types: Types,
|
||||
/// Table of types introduced in this module that claim to derive an ability implementation.
|
||||
/// Due for checking and instantiation after the solver runs over the module.
|
||||
pub pending_derives: PendingDerives,
|
||||
/// Types exposed by other modules.
|
||||
/// Available for late instantiation of imports, lambda sets, or ability types.
|
||||
pub exposed_by_module: &'a ExposedByModule,
|
||||
/// The unique `#Derived` module, used to generate and retrieve derived ability
|
||||
/// implementations.
|
||||
/// Needed during solving to resolve lambda sets from derived implementations that escape into
|
||||
/// the user module.
|
||||
pub derived_module: SharedDerivedModule,
|
||||
}
|
||||
|
||||
pub struct SolveOutput {
|
||||
pub subs: Solved<Subs>,
|
||||
pub env: solve::Env,
|
||||
pub errors: Vec<TypeError>,
|
||||
pub resolved_abilities_store: AbilitiesStore,
|
||||
}
|
||||
|
||||
pub fn run_solve(
|
||||
home: ModuleId,
|
||||
types: Types,
|
||||
constraints: &Constraints,
|
||||
constraint: ConstraintSoa,
|
||||
ctx: SolveCtx<'_>,
|
||||
rigid_variables: RigidVariables,
|
||||
mut subs: Subs,
|
||||
mut aliases: Aliases,
|
||||
mut abilities_store: AbilitiesStore,
|
||||
pending_derives: PendingDerives,
|
||||
exposed_by_module: &ExposedByModule,
|
||||
derived_module: SharedDerivedModule,
|
||||
) -> (Solved<Subs>, solve::Env, Vec<TypeError>, AbilitiesStore) {
|
||||
) -> SolveOutput {
|
||||
for (var, name) in rigid_variables.named {
|
||||
subs.rigid_var(var, name);
|
||||
}
|
||||
|
@ -84,21 +105,15 @@ pub fn run_solve(
|
|||
let mut problems = Vec::new();
|
||||
|
||||
// Run the solver to populate Subs.
|
||||
let (solved_subs, solved_env) = solve::run(
|
||||
home,
|
||||
types,
|
||||
constraints,
|
||||
&mut problems,
|
||||
subs,
|
||||
&mut aliases,
|
||||
&constraint,
|
||||
pending_derives,
|
||||
&mut abilities_store,
|
||||
exposed_by_module,
|
||||
derived_module,
|
||||
);
|
||||
let (solved_subs, solved_env) =
|
||||
solve::run(ctx, &mut problems, subs, &mut aliases, &mut abilities_store);
|
||||
|
||||
(solved_subs, solved_env, problems, abilities_store)
|
||||
SolveOutput {
|
||||
subs: solved_subs,
|
||||
env: solved_env,
|
||||
errors: problems,
|
||||
resolved_abilities_store: abilities_store,
|
||||
}
|
||||
}
|
||||
|
||||
/// Copies exposed types and all ability specializations, which may be implicitly exposed.
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::ability::{
|
|||
resolve_ability_specialization, type_implementing_specialization, AbilityImplError,
|
||||
CheckedDerives, ObligationCache, PendingDerivesTable, Resolved,
|
||||
};
|
||||
use crate::module::Solved;
|
||||
use crate::module::{SolveCtx, Solved};
|
||||
use crate::specialize::{
|
||||
compact_lambda_sets_of_vars, AwaitingSpecializations, CompactionResult, DerivedEnv, SolvePhase,
|
||||
};
|
||||
|
@ -13,17 +13,14 @@ use roc_can::abilities::{AbilitiesStore, MemberSpecializationInfo};
|
|||
use roc_can::constraint::Constraint::{self, *};
|
||||
use roc_can::constraint::{Constraints, Cycle, LetConstraint, OpportunisticResolve, TypeOrVar};
|
||||
use roc_can::expected::{Expected, PExpected};
|
||||
use roc_can::expr::PendingDerives;
|
||||
use roc_can::module::ExposedByModule;
|
||||
use roc_collections::all::MutMap;
|
||||
use roc_collections::soa::{Index, Slice};
|
||||
use roc_debug_flags::dbg_do;
|
||||
#[cfg(debug_assertions)]
|
||||
use roc_debug_flags::ROC_VERIFY_RIGID_LET_GENERALIZED;
|
||||
use roc_derive::SharedDerivedModule;
|
||||
use roc_error_macros::internal_error;
|
||||
use roc_module::ident::TagName;
|
||||
use roc_module::symbol::{ModuleId, Symbol};
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_problem::can::CycleEntry;
|
||||
use roc_region::all::Loc;
|
||||
use roc_solve_problem::TypeError;
|
||||
|
@ -524,33 +521,14 @@ struct State {
|
|||
mark: Mark,
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)] // TODO: put params in a context/env var
|
||||
pub fn run(
|
||||
home: ModuleId,
|
||||
types: Types,
|
||||
constraints: &Constraints,
|
||||
ctx: SolveCtx,
|
||||
problems: &mut Vec<TypeError>,
|
||||
mut subs: Subs,
|
||||
aliases: &mut Aliases,
|
||||
constraint: &Constraint,
|
||||
pending_derives: PendingDerives,
|
||||
abilities_store: &mut AbilitiesStore,
|
||||
exposed_by_module: &ExposedByModule,
|
||||
derived_module: SharedDerivedModule,
|
||||
) -> (Solved<Subs>, Env) {
|
||||
let env = run_in_place(
|
||||
home,
|
||||
types,
|
||||
constraints,
|
||||
problems,
|
||||
&mut subs,
|
||||
aliases,
|
||||
constraint,
|
||||
pending_derives,
|
||||
abilities_store,
|
||||
exposed_by_module,
|
||||
derived_module,
|
||||
);
|
||||
let env = run_in_place(ctx, problems, &mut subs, aliases, abilities_store);
|
||||
|
||||
(Solved(subs), env)
|
||||
}
|
||||
|
@ -558,18 +536,22 @@ pub fn run(
|
|||
/// Modify an existing subs in-place instead
|
||||
#[allow(clippy::too_many_arguments)] // TODO: put params in a context/env var
|
||||
fn run_in_place(
|
||||
_home: ModuleId, // TODO: remove me?
|
||||
mut types: Types,
|
||||
constraints: &Constraints,
|
||||
ctx: SolveCtx,
|
||||
problems: &mut Vec<TypeError>,
|
||||
subs: &mut Subs,
|
||||
aliases: &mut Aliases,
|
||||
constraint: &Constraint,
|
||||
pending_derives: PendingDerives,
|
||||
abilities_store: &mut AbilitiesStore,
|
||||
exposed_by_module: &ExposedByModule,
|
||||
derived_module: SharedDerivedModule,
|
||||
) -> Env {
|
||||
let SolveCtx {
|
||||
home: _,
|
||||
constraints,
|
||||
root_constraint,
|
||||
mut types,
|
||||
pending_derives,
|
||||
exposed_by_module,
|
||||
derived_module,
|
||||
} = ctx;
|
||||
|
||||
let mut pools = Pools::default();
|
||||
|
||||
let state = State {
|
||||
|
@ -602,21 +584,25 @@ fn run_in_place(
|
|||
exposed_types: exposed_by_module,
|
||||
};
|
||||
|
||||
let state = solve(
|
||||
&arena,
|
||||
types,
|
||||
let reified_ctx = ReifiedSolveCtx {
|
||||
arena: &arena,
|
||||
constraints,
|
||||
derived_env: &derived_env,
|
||||
};
|
||||
|
||||
let state = solve(
|
||||
reified_ctx,
|
||||
types,
|
||||
state,
|
||||
rank,
|
||||
&mut pools,
|
||||
problems,
|
||||
aliases,
|
||||
subs,
|
||||
constraint,
|
||||
&root_constraint,
|
||||
abilities_store,
|
||||
&mut obligation_cache,
|
||||
&mut awaiting_specializations,
|
||||
&derived_env,
|
||||
);
|
||||
|
||||
state.env
|
||||
|
@ -659,11 +645,15 @@ enum Work<'a> {
|
|||
},
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
struct ReifiedSolveCtx<'a> {
|
||||
arena: &'a Bump,
|
||||
constraints: &'a Constraints,
|
||||
derived_env: &'a DerivedEnv<'a>,
|
||||
}
|
||||
|
||||
fn solve(
|
||||
arena: &Bump,
|
||||
ctx: ReifiedSolveCtx,
|
||||
mut can_types: Types,
|
||||
constraints: &Constraints,
|
||||
mut state: State,
|
||||
rank: Rank,
|
||||
pools: &mut Pools,
|
||||
|
@ -674,8 +664,13 @@ fn solve(
|
|||
abilities_store: &mut AbilitiesStore,
|
||||
obligation_cache: &mut ObligationCache,
|
||||
awaiting_specializations: &mut AwaitingSpecializations,
|
||||
derived_env: &DerivedEnv,
|
||||
) -> State {
|
||||
let ReifiedSolveCtx {
|
||||
arena,
|
||||
constraints,
|
||||
derived_env,
|
||||
} = ctx;
|
||||
|
||||
let initial = Work::Constraint {
|
||||
env: &Env::default(),
|
||||
rank,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue