From 3eadc03e28a9e05f5e284c598e86f9a9827d4270 Mon Sep 17 00:00:00 2001 From: Agus Zubiaga Date: Fri, 20 Sep 2024 16:19:56 -0300 Subject: [PATCH 1/3] Add failing test for multiline params --- crates/cli/tests/cli_run.rs | 19 +++++++++++++++++++ .../tests/module_params/MultilineParams.roc | 8 ++++++++ .../tests/module_params/multiline_params.roc | 11 +++++++++++ crates/compiler/can/src/pattern.rs | 5 ++++- 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 crates/cli/tests/module_params/MultilineParams.roc create mode 100644 crates/cli/tests/module_params/multiline_params.roc diff --git a/crates/cli/tests/cli_run.rs b/crates/cli/tests/cli_run.rs index 1544a888873..a27aa457ab0 100644 --- a/crates/cli/tests/cli_run.rs +++ b/crates/cli/tests/cli_run.rs @@ -922,6 +922,25 @@ mod cli_run { ); } + #[test] + #[cfg_attr(windows, ignore)] + fn module_params_multiline_pattern() { + test_roc_app( + "crates/cli/tests/module_params", + "multiline_params.roc", + &[], + &[], + &[], + indoc!( + r#" + hi + "# + ), + UseValgrind::No, + TestCliCommands::Dev, + ); + } + #[test] #[cfg_attr(windows, ignore)] fn transitive_expects() { diff --git a/crates/cli/tests/module_params/MultilineParams.roc b/crates/cli/tests/module_params/MultilineParams.roc new file mode 100644 index 00000000000..631b88473be --- /dev/null +++ b/crates/cli/tests/module_params/MultilineParams.roc @@ -0,0 +1,8 @@ +module { + sendHttpReq, + getEnvVar +} -> [hi] + +hi : Str +hi = + "hi" diff --git a/crates/cli/tests/module_params/multiline_params.roc b/crates/cli/tests/module_params/multiline_params.roc new file mode 100644 index 00000000000..531721c9258 --- /dev/null +++ b/crates/cli/tests/module_params/multiline_params.roc @@ -0,0 +1,11 @@ +app [main] { + pf: platform "../fixtures/multi-dep-str/platform/main.roc", +} + +import MultilineParams { + sendHttpReq: \_ -> crash "todo", + getEnvVar: \_ -> crash "todo", +} + +main = + MultilineParams.hi \ No newline at end of file diff --git a/crates/compiler/can/src/pattern.rs b/crates/compiler/can/src/pattern.rs index ddd6efda308..ffd097e3ae8 100644 --- a/crates/compiler/can/src/pattern.rs +++ b/crates/compiler/can/src/pattern.rs @@ -914,7 +914,10 @@ pub fn canonicalize_record_destructs<'a>( } }; } - _ => unreachable!("Any other pattern should have given a parse error"), + _ => unreachable!( + "Any other pattern should have given a parse error: {:?}", + loc_pattern.value + ), } } From 3f9e957c9cba8fa5e2b6c15b6926dedabd8434c4 Mon Sep 17 00:00:00 2001 From: Agus Zubiaga Date: Fri, 20 Sep 2024 16:30:59 -0300 Subject: [PATCH 2/3] Extract record destructure desugaring into its own fn --- crates/compiler/can/src/desugar.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/compiler/can/src/desugar.rs b/crates/compiler/can/src/desugar.rs index 3f4872366e4..65599c21783 100644 --- a/crates/compiler/can/src/desugar.rs +++ b/crates/compiler/can/src/desugar.rs @@ -1226,17 +1226,7 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a Apply(tag, desugared_arg_patterns.into_bump_slice()) } RecordDestructure(field_patterns) => { - let mut allocated = Vec::with_capacity_in(field_patterns.len(), env.arena); - for field_pattern in field_patterns.iter() { - let value = desugar_pattern(env, scope, field_pattern.value); - allocated.push(Loc { - value, - region: field_pattern.region, - }); - } - let field_patterns = field_patterns.replace_items(allocated.into_bump_slice()); - - RecordDestructure(field_patterns) + RecordDestructure(desugar_record_destructures(env, scope, field_patterns)) } RequiredField(name, field_pattern) => { RequiredField(name, desugar_loc_pattern(env, scope, field_pattern)) @@ -1274,6 +1264,23 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a } } +pub fn desugar_record_destructures<'a>( + env: &mut Env<'a>, + scope: &mut Scope, + field_patterns: Collection<'a, Loc>>, +) -> Collection<'a, Loc>> { + let mut allocated = Vec::with_capacity_in(field_patterns.len(), env.arena); + for field_pattern in field_patterns.iter() { + let value = desugar_pattern(env, scope, field_pattern.value); + allocated.push(Loc { + value, + region: field_pattern.region, + }); + } + + field_patterns.replace_items(allocated.into_bump_slice()) +} + /// Desugars a `dbg expr` expression into a statement block that prints and returns the /// value produced by `expr`. Essentially: /// ( From 5054c9990edb08982eb99a7aea4e8525d36bf6e3 Mon Sep 17 00:00:00 2001 From: Agus Zubiaga Date: Fri, 20 Sep 2024 16:35:18 -0300 Subject: [PATCH 3/3] Desugar module param patterns before canonicalizing --- crates/compiler/can/src/module.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/compiler/can/src/module.rs b/crates/compiler/can/src/module.rs index 14dc2f827b8..3a45b6ba912 100644 --- a/crates/compiler/can/src/module.rs +++ b/crates/compiler/can/src/module.rs @@ -3,6 +3,7 @@ use std::path::Path; use crate::abilities::{AbilitiesStore, ImplKey, PendingAbilitiesStore, ResolvedImpl}; use crate::annotation::{canonicalize_annotation, AnnotationFor}; use crate::def::{canonicalize_defs, report_unused_imports, Def}; +use crate::desugar::desugar_record_destructures; use crate::env::Env; use crate::expr::{ ClosureData, DbgLookup, Declarations, ExpectLookup, Expr, Output, PendingDerives, @@ -326,13 +327,16 @@ pub fn canonicalize_module_defs<'a>( before_arrow: _, after_arrow: _, }| { + let desugared_patterns = + desugar_record_destructures(&mut env, &mut scope, pattern.value); + let (destructs, _) = canonicalize_record_destructs( &mut env, var_store, &mut scope, &mut output, PatternType::ModuleParams, - &pattern.value, + &desugared_patterns, pattern.region, PermitShadows(false), );