Skip to content

Commit

Permalink
Merge pull request #7109 from roc-lang/desugar-param-patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
agu-z committed Sep 20, 2024
2 parents 9678046 + 5054c99 commit 6c846a5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
19 changes: 19 additions & 0 deletions crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 8 additions & 0 deletions crates/cli/tests/module_params/MultilineParams.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module {
sendHttpReq,
getEnvVar
} -> [hi]

hi : Str
hi =
"hi"
11 changes: 11 additions & 0 deletions crates/cli/tests/module_params/multiline_params.roc
Original file line number Diff line number Diff line change
@@ -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
29 changes: 18 additions & 11 deletions crates/compiler/can/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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<Pattern<'a>>>,
) -> Collection<'a, Loc<Pattern<'a>>> {
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:
/// (
Expand Down
6 changes: 5 additions & 1 deletion crates/compiler/can/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
);
Expand Down
5 changes: 4 additions & 1 deletion crates/compiler/can/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
}
}

Expand Down

0 comments on commit 6c846a5

Please sign in to comment.