Capture params in lowered passed fns from importer side

This commit is contained in:
Agus Zubiaga 2024-08-27 00:02:21 -03:00
parent b667753a32
commit 3c6a2f11d7
No known key found for this signature in database
3 changed files with 18 additions and 3 deletions

View file

@ -753,6 +753,7 @@ mod cli_run {
App2.getCompanies [5, 6]: ["http://api.example.com/two/companies/5", "http://api.example.com/two/companies/6"]
usersApp1: ["https://api.example.com/one/users/1", "https://api.example.com/one/users/2", "https://api.example.com/one/users/3"]
getUserApp3Nested 3: https://api.example.com/three/users/3
usersApp3Passed: ["https://api.example.com/three/users/1", "https://api.example.com/three/users/2", "https://api.example.com/three/users/3"]
"#
),
UseValgrind::No,

View file

@ -21,6 +21,10 @@ main =
# use captured params def
App3.getUser userId
usersApp3Passed =
# pass top-level fn in a nested def
List.map [1, 2, 3] App3.getUser
"""
App1.baseUrl: $(App1.baseUrl)
App2.baseUrl: $(App2.baseUrl)
@ -42,4 +46,5 @@ main =
App2.getCompanies [5, 6]: $(Inspect.toStr (App2.getCompanies [5, 6]))
usersApp1: $(Inspect.toStr usersApp1)
getUserApp3Nested 3: $(getUserApp3Nested 3)
usersApp3Passed: $(Inspect.toStr usersApp3Passed)
"""

View file

@ -9,7 +9,7 @@ use roc_can::{
pattern::Pattern,
};
use roc_collections::VecMap;
use roc_module::symbol::{IdentIds, ModuleId, Symbol};
use roc_module::symbol::{IdentId, IdentIds, ModuleId, Symbol};
use roc_region::all::Loc;
use roc_types::subs::{VarStore, Variable};
use roc_types::types::Type;
@ -20,6 +20,7 @@ struct LowerParams<'a> {
imported_params: VecMap<ModuleId, ModuleParams>,
var_store: &'a mut VarStore,
ident_ids: &'a mut IdentIds,
top_level_idents: Vec<IdentId>,
}
pub fn lower(
@ -30,12 +31,19 @@ pub fn lower(
ident_ids: &mut IdentIds,
var_store: &mut VarStore,
) {
let top_level_idents = decls
.symbols
.iter()
.map(|symbol| symbol.value.ident_id())
.collect();
let mut env = LowerParams {
home_id,
home_params,
imported_params,
ident_ids,
var_store,
top_level_idents,
};
env.lower_decls(decls);
@ -432,10 +440,11 @@ impl<'a> LowerParams<'a> {
roc_module::called_via::CalledVia::Space,
);
let captured_symbols = if symbol.module_id() == self.home_id {
let captured_symbols = if symbol.module_id() == self.home_id
|| !self.top_level_idents.contains(&params_symbol.ident_id())
{
vec![(params_symbol, params_var)]
} else {
// todo: capture if import is not top-level
vec![]
};