Skip to content

Commit

Permalink
fix record constructors in guards using non-aliased names
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Jul 28, 2024
1 parent 31840e2 commit 6b3c286
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
7 changes: 4 additions & 3 deletions compiler-core/src/nix/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,10 +1227,11 @@ pub(crate) fn guard_constant_expression<'a>(
Constant::Record { typ, .. } if typ.is_nil() => Ok("null".to_doc()),

Constant::Record {
tag,
typ,
args,
module,
name,
tag,
typ,
..
} => {
if typ.is_result() {
Expand All @@ -1244,7 +1245,7 @@ pub(crate) fn guard_constant_expression<'a>(
.iter()
.map(|arg| wrap_child_guard_constant_expression(assignments, tracker, &arg.value))
.try_collect()?;
Ok(construct_record(module.as_deref(), tag, field_values))
Ok(construct_record(module.as_deref(), name, field_values))
}

Constant::BitArray { segments, .. } => {
Expand Down
49 changes: 49 additions & 0 deletions compiler-core/src/nix/tests/case_clause_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,52 @@ fn not_two() {
"#,
);
}

#[test]
fn custom_type_constructor_imported_and_aliased() {
assert_nix!(
("package", "other_module", "pub type T { A }"),
r#"import other_module.{A as B}
fn func() {
case B {
x if x == B -> True
_ -> False
}
}
"#,
);
}

#[test]
fn imported_aliased_ok() {
assert_nix!(
r#"import gleam.{Ok as Y}
pub type X {
Ok
}
fn func() {
case Y {
y if y == Y -> True
_ -> False
}
}
"#,
);
}

#[test]
fn imported_ok() {
assert_nix!(
r#"import gleam
pub type X {
Ok
}
fn func(x) {
case gleam.Ok {
_ if [] == [ gleam.Ok ] -> True
_ -> False
}
}
"#,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: compiler-core/src/nix/tests/case_clause_guards.rs
expression: "import other_module.{A as B}\nfn func() {\n case B {\n x if x == B -> True\n _ -> False\n }\n}\n"
---
let
other_module' = builtins.import ./../../package/other_module.nix;
B = (builtins.import ./../../package/other_module.nix).A;

func =
{ }: let _pat' = B; in if _pat' == B then let x = _pat'; in true else false;
in
{ }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: compiler-core/src/nix/tests/case_clause_guards.rs
expression: "import gleam.{Ok as Y}\npub type X {\n Ok\n}\nfn func() {\n case Y {\n y if y == Y -> True\n _ -> False\n }\n}\n"
---
let
gleam' = builtins.import ./../gleam.nix;
Y = (builtins.import ./../gleam.nix).Ok;

Ok = { __gleamTag = "Ok"; };

func =
{ }: let _pat' = Y; in if _pat' == Y then let y = _pat'; in true else false;
in
{ inherit Ok; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: compiler-core/src/nix/tests/case_clause_guards.rs
expression: "import gleam\npub type X {\n Ok\n}\nfn func(x) {\n case gleam.Ok {\n _ if [] == [ gleam.Ok ] -> True\n _ -> False\n }\n}\n"
---
let
gleam' = builtins.import ./../gleam.nix;
inherit (builtins.import ./../gleam.nix) toList;

Ok = { __gleamTag = "Ok"; };

func =
x:
let
_pat' = gleam'.Ok;
in
if (toList [ ]) == (toList [ gleam'.Ok ]) then true
else false;
in
{ inherit Ok; }

0 comments on commit 6b3c286

Please sign in to comment.