Skip to content

Incorrect JS outputs when pattern matching with empty list  #7235

Closed
@dsiu

Description

@dsiu

Starting from alpha.5 (up to the current alpha.7), the following code generates incorrect js output:

let isEmpty: 'a. list<'a> => bool = x =>
  switch x {
  | list{} => true
  | _ => false
  }

let isNotEmpty: 'a. list<'a> => bool = xs => !isEmpty(xs)

Incorrect JS output (Playground):

// Generated by ReScript, PLEASE EDIT WITH CARE

function isEmpty(x) {
  return !x;
}

function isNotEmpty(xs) {
  return xs;
}

export {
  isEmpty,
  isNotEmpty,
}
/* No side effect */

Whereas alphe.4 and before generates correctly (Playground):

// Generated by ReScript, PLEASE EDIT WITH CARE


function isEmpty(x) {
  if (x) {
    return false;
  } else {
    return true;
  }
}

function isNotEmpty(xs) {
  return !(
    xs ? false : true
  );
}

export {
  isEmpty,
  isNotEmpty,
}
/* No side effect */

I found out this problem when porting [Relude](https://github.com/dsiu/rescript-relude) to rescript@v12. The following Jest tests fails:

 test("isNotEmpty is false for empty list", () => expect(List.isNotEmpty(list{}))->toBe(false))

 test("isNotEmpty is true for non-empty list", () => expect(List.isNotEmpty(list{1}))->toBe(true))
Summary of all failing tests
 FAIL  __tests__/Relude_List_test.bs.js
  ● List › isNotEmpty is false for empty list

    expect(received).toBe(expected) // Object.is equality

    Expected: false
    Received: 0

      at affirm (node_modules/@glennsl/rescript-jest/src/jest.bs.js:73:36)
      at Object.<anonymous> (node_modules/@glennsl/rescript-jest/src/jest.bs.js:220:5)

  ● List › isNotEmpty is true for non-empty list

    expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: {"hd": 1, "tl": 0}

      at affirm (node_modules/@glennsl/rescript-jest/src/jest.bs.js:73:36)
      at Object.<anonymous> (node_modules/@glennsl/rescript-jest/src/jest.bs.js:220:5)

Thank you for filing! Check list:

  • Is it a bug? Usage questions should often be asked in the forum instead.
  • Concise, focused, friendly issue title & description.
  • A minimal, reproducible example.
  • OS and browser versions, if relevant.
  • Is it already fixed in master? Instructions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions