Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix string prefix assignments in nested patterns #3895

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
pipelines that would be safe to remove.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- Fixed a bug where assigning the prefix of a string pattern to a variable
nested inside another pattern would produce invalid code on Javascript.
([yoshi](https://github.com/joshi-monster))

## v1.6.1 - 2024-11-19

### Bug fixed
Expand Down
11 changes: 10 additions & 1 deletion compiler-core/src/javascript/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,16 @@ impl<'module_ctx, 'expression_gen, 'a> Generator<'module_ctx, 'expression_gen, '
// let prefix = "wibble";
// ^^^^^^^^^^^^^^^^^^^^^ we're adding this assignment inside the if clause
// the case branch gets translated into.
self.push_assignment(expression::string(left_side_string), left);
//
// We also want to push this assignment without using push_assignment, since we
// do _not_ want to access the current path on the static string!
let var = self.next_local_var(left);
self.assignments.push(Assignment {
subject: expression::string(left_side_string),
path: docvec![],
name: left,
var,
});
}
Ok(())
}
Expand Down
17 changes: 17 additions & 0 deletions compiler-core/src/javascript/tests/assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ pub fn main() {
);
}

// https://github.com/gleam-lang/gleam/issues/3894
#[test]
fn let_assert_nested_string_prefix() {
assert_js!(
r#"
type Wibble {
Wibble(wibble: String)
}

pub fn main() {
let assert Wibble(wibble: "w" as prefix <> rest) = Wibble("wibble")
prefix <> rest
}
"#
);
}

// https://github.com/gleam-lang/gleam/issues/2931
#[test]
fn keyword_assignment() {
Expand Down
45 changes: 45 additions & 0 deletions compiler-core/src/javascript/tests/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,48 @@ pub fn main() {
"#
)
}

// https://github.com/gleam-lang/gleam/issues/3894
#[test]
fn nested_string_prefix_assignment() {
assert_js!(
r#"
type Wibble {
Wibble(wobble: String)
}

pub fn main() {
let tmp = Wibble(wobble: "wibble")
case tmp {
Wibble(wobble: "w" as wibble <> rest) -> wibble <> rest
_ -> panic
}
}
"#
)
}

#[test]
fn deeply_nested_string_prefix_assignment() {
assert_js!(
r#"
type Wibble {
Wibble(Wobble)
}
type Wobble {
Wobble(wabble: Wabble)
}
type Wabble {
Wabble(tuple: #(Int, String))
}

pub fn main() {
let tmp = Wibble(Wobble(Wabble(#(42, "wibble"))))
case tmp {
Wibble(Wobble(Wabble(#(_int, "w" as wibble <> rest)))) -> wibble <> rest
_ -> panic
}
}
"#
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: compiler-core/src/javascript/tests/assignments.rs
expression: "\ntype Wibble {\n Wibble(wibble: String)\n}\n\npub fn main() {\n let assert Wibble(wibble: \"w\" as prefix <> rest) = Wibble(\"wibble\")\n prefix <> rest\n}\n"
---
----- SOURCE CODE

type Wibble {
Wibble(wibble: String)
}

pub fn main() {
let assert Wibble(wibble: "w" as prefix <> rest) = Wibble("wibble")
prefix <> rest
}


----- COMPILED JAVASCRIPT
import { CustomType as $CustomType, makeError } from "../gleam.mjs";

class Wibble extends $CustomType {
constructor(wibble) {
super();
this.wibble = wibble;
}
}

export function main() {
let $ = new Wibble("wibble");
if (!($ instanceof Wibble) || !$.wibble.startsWith("w")) {
throw makeError(
"let_assert",
"my/mod",
7,
"main",
"Pattern match failed, no pattern matched the value.",
{ value: $ }
)
}
let rest = $.wibble.slice(1);
let prefix = "w";
return prefix + rest;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
source: compiler-core/src/javascript/tests/case.rs
expression: "\ntype Wibble {\n Wibble(Wobble)\n} \ntype Wobble {\n Wobble(wabble: Wabble)\n}\ntype Wabble {\n Wabble(tuple: #(Int, String))\n}\n\npub fn main() {\n let tmp = Wibble(Wobble(Wabble(#(42, \"wibble\"))))\n case tmp {\n Wibble(Wobble(Wabble(#(_int, \"w\" as wibble <> rest)))) -> wibble <> rest\n _ -> panic\n }\n}\n"
---
----- SOURCE CODE

type Wibble {
Wibble(Wobble)
}
type Wobble {
Wobble(wabble: Wabble)
}
type Wabble {
Wabble(tuple: #(Int, String))
}

pub fn main() {
let tmp = Wibble(Wobble(Wabble(#(42, "wibble"))))
case tmp {
Wibble(Wobble(Wabble(#(_int, "w" as wibble <> rest)))) -> wibble <> rest
_ -> panic
}
}


----- COMPILED JAVASCRIPT
import { CustomType as $CustomType, makeError } from "../gleam.mjs";

class Wibble extends $CustomType {
constructor(x0) {
super();
this[0] = x0;
}
}

class Wobble extends $CustomType {
constructor(wabble) {
super();
this.wabble = wabble;
}
}

class Wabble extends $CustomType {
constructor(tuple) {
super();
this.tuple = tuple;
}
}

export function main() {
let tmp = new Wibble(new Wobble(new Wabble([42, "wibble"])));
if (tmp instanceof Wibble &&
tmp[0] instanceof Wobble &&
tmp[0].wabble instanceof Wabble &&
tmp[0].wabble.tuple[1].startsWith("w")) {
let rest = tmp[0].wabble.tuple[1].slice(1);
let wibble = "w";
return wibble + rest;
} else {
throw makeError(
"panic",
"my/mod",
16,
"main",
"`panic` expression evaluated.",
{}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
source: compiler-core/src/javascript/tests/case.rs
expression: "\ntype Wibble {\n Wibble(wobble: String)\n}\n\npub fn main() {\n let tmp = Wibble(wobble: \"wibble\")\n case tmp {\n Wibble(wobble: \"w\" as wibble <> rest) -> wibble <> rest\n _ -> panic\n }\n}\n"
---
----- SOURCE CODE

type Wibble {
Wibble(wobble: String)
}

pub fn main() {
let tmp = Wibble(wobble: "wibble")
case tmp {
Wibble(wobble: "w" as wibble <> rest) -> wibble <> rest
_ -> panic
}
}


----- COMPILED JAVASCRIPT
import { CustomType as $CustomType, makeError } from "../gleam.mjs";

class Wibble extends $CustomType {
constructor(wobble) {
super();
this.wobble = wobble;
}
}

export function main() {
let tmp = new Wibble("wibble");
if (tmp instanceof Wibble && tmp.wobble.startsWith("w")) {
let rest = tmp.wobble.slice(1);
let wibble = "w";
return wibble + rest;
} else {
throw makeError(
"panic",
"my/mod",
10,
"main",
"`panic` expression evaluated.",
{}
)
}
}
Loading