enable recursive definitions at the top-level

This commit is contained in:
Folkert 2020-10-14 01:06:17 +02:00
parent 2da2c25d32
commit 6aaba98d31

View file

@ -2166,11 +2166,59 @@ fn build_pending_specializations<'a>(
// Add modules' decls to Procs // Add modules' decls to Procs
for decl in decls { for decl in decls {
use roc_can::def::Declaration::*; use roc_can::def::Declaration::*;
match decl {
Declare(def) | Builtin(def) => add_def_to_module(
&mut layout_cache,
&mut procs,
&mut mono_env,
def,
&exposed_to_host,
false,
),
DeclareRec(defs) => {
for def in defs {
add_def_to_module(
&mut layout_cache,
&mut procs,
&mut mono_env,
def,
&exposed_to_host,
true,
)
}
}
InvalidCycle(_loc_idents, _regions) => {
todo!("TODO handle InvalidCycle");
}
}
}
let problems = mono_env.problems.to_vec();
Msg::FoundSpecializations {
module_id: home,
solved_subs: roc_types::solved_types::Solved(subs),
ident_ids,
layout_cache,
procs,
problems,
finished_info,
}
}
fn add_def_to_module<'a>(
layout_cache: &mut LayoutCache<'a>,
procs: &mut Procs<'a>,
mono_env: &mut roc_mono::ir::Env<'a, '_>,
def: roc_can::def::Def,
exposed_to_host: &MutSet<Symbol>,
is_recursive: bool,
) {
use roc_can::expr::Expr::*; use roc_can::expr::Expr::*;
use roc_can::pattern::Pattern::*; use roc_can::pattern::Pattern::*;
match decl { match def.loc_pattern.value {
Declare(def) | Builtin(def) => match def.loc_pattern.value {
Identifier(symbol) => { Identifier(symbol) => {
let is_exposed = exposed_to_host.contains(&symbol); let is_exposed = exposed_to_host.contains(&symbol);
@ -2182,8 +2230,6 @@ fn build_pending_specializations<'a>(
loc_body, loc_body,
.. ..
} => { } => {
// this is a non-recursive declaration
let is_tail_recursive = false;
// If this is an exposed symbol, we need to // If this is an exposed symbol, we need to
// register it as such. Otherwise, since it // register it as such. Otherwise, since it
// never gets called by Roc code, it will never // never gets called by Roc code, it will never
@ -2191,7 +2237,7 @@ fn build_pending_specializations<'a>(
if is_exposed { if is_exposed {
let mut pattern_vars = bumpalo::collections::Vec::with_capacity_in( let mut pattern_vars = bumpalo::collections::Vec::with_capacity_in(
loc_args.len(), loc_args.len(),
arena, mono_env.arena,
); );
for (var, _) in loc_args.iter() { for (var, _) in loc_args.iter() {
@ -2214,13 +2260,13 @@ fn build_pending_specializations<'a>(
} }
procs.insert_named( procs.insert_named(
&mut mono_env, mono_env,
&mut layout_cache, layout_cache,
symbol, symbol,
annotation, annotation,
loc_args, loc_args,
*loc_body, *loc_body,
is_tail_recursive, is_recursive,
ret_var, ret_var,
); );
} }
@ -2255,26 +2301,6 @@ fn build_pending_specializations<'a>(
other => { other => {
todo!("TODO gracefully handle Declare({:?})", other); todo!("TODO gracefully handle Declare({:?})", other);
} }
},
DeclareRec(_defs) => {
todo!("TODO support DeclareRec");
}
InvalidCycle(_loc_idents, _regions) => {
todo!("TODO handle InvalidCycle");
}
}
}
let problems = mono_env.problems.to_vec();
Msg::FoundSpecializations {
module_id: home,
solved_subs: roc_types::solved_types::Solved(subs),
ident_ids,
layout_cache,
procs,
problems,
finished_info,
} }
} }