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

Compiler crash on "_" as argument in match case expression. #1922

Closed
sophiaIC opened this issue May 20, 2017 · 3 comments
Closed

Compiler crash on "_" as argument in match case expression. #1922

sophiaIC opened this issue May 20, 2017 · 3 comments
Assignees

Comments

@sophiaIC
Copy link

sophiaIC commented May 20, 2017

When using patterns which test for values, as described in the tutorial in https://tutorial.ponylang.org/pattern-matching/match.html, the compiler crashes. I know how to program around this, but perhaps this section should be removed from the tutorial.

Here code:

class A

  fun ref test(b' : (B ref | None) ) =>
        match b'
        |  B(_)  => None
        else
          None
        end

class B
  var i : U32

  new create(i': U32) =>
     i = i'

Here the compiler output

src/libponyc/type/subtype.c:1553: is_x_sub_x: Assertion `0` failed.

Backtrace:
  0   ponyc                               0x0000000110c6c573 ponyint_assert_fail + 164
  1   ponyc                               0x0000000110c6423f is_x_sub_x + 1642
  2   ponyc                               0x0000000110c32176 method_application + 1323
  3   ponyc                               0x0000000110c30bb1 expr_call + 505
  4   ponyc                               0x0000000110c42cc6 pass_expr + 304
  5   ponyc                               0x0000000110c4574d ast_visit + 294
  6   ponyc                               0x0000000110c4570c ast_visit + 229
  7   ponyc                               0x0000000110c4570c ast_visit + 229
  8   ponyc                               0x0000000110c4570c ast_visit + 229
  9   ponyc                               0x0000000110c4570c ast_visit + 229
  10  ponyc                               0x0000000110c4570c ast_visit + 229
  11  ponyc                               0x0000000110c4570c ast_visit + 229
  12  ponyc                               0x0000000110c4570c ast_visit + 229
  13  ponyc                               0x0000000110c4570c ast_visit + 229
  14  ponyc                               0x0000000110c4570c ast_visit + 229
  15  ponyc                               0x0000000110c4570c ast_visit + 229
  16  ponyc                               0x0000000110c45b4b visit_pass + 77
  17  ponyc                               0x0000000110c45a72 ast_passes + 464
  18  ponyc                               0x0000000110c56a16 program_load + 157
  19  ponyc                               0x0000000110be7b00 compile_package + 24
  20  ponyc                               0x0000000110be79d4 main + 1108
  21  libdyld.dylib                       0x00007fffdd112235 start + 1
This is an optimised version of ponyc: the backtrace may be imprecise or incorrect.
Use a debug version to get more meaningful information.
Abort trap: 6
@SeanTAllen
Copy link
Member

Prior to commit e84506d, this resulted in the error:

Building builtin -> /usr/local/lib/pony/0.13.0-b71af17/packages/builtin
Building . -> /Users/sean/code/pony-scratch/issue-1922
Error:
/Users/sean/code/pony-scratch/issue-1922/a.pony:4:14: can't read from '_'
        |  B(_)  => None
             ^
Error:
/Users/sean/code/pony-scratch/issue-1922/a.pony:4:13: not a matchable pattern
        |  B(_)  => None
            ^

so, I think i tmakes sense to change the tutorial to a working example and also fix the regression caused by e84506d.

/cc @jemc

@jemc
Copy link
Member

jemc commented May 20, 2017

I can fix the crash, but note that this example would never have worked in Pony - you should be getting the error @SeanTAllen showed.

The Pony tutorial examples should all still work - none of those examples try to call a constructor/method with "don't care" (_) as the argument.

The core reason this doesn't work, is that when you use a value for pattern matching, Pony first has to evaluate the expression to create the value, then compare it to the match operand. The expression B(_) cannot be evaluated, because Pony doesn't know what argument to pass to the constructor of B.

@jemc jemc self-assigned this May 20, 2017
@jemc
Copy link
Member

jemc commented May 20, 2017

You may already know this, since you said "I know how to program around this", but for the benefit of anybody else that comes across this ticket, the correct approach here would be:

class A
  fun ref test(b' : (B ref | None) ) =>
    match b'
    |  let _: B => None
    else
      None
    end

class B
  var i : U32

  new create(i': U32) =>
     i = i'

@jemc jemc changed the title Pattern Matching Crashes the Compiler Compiler crash on "_" as argument in match case expression. May 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants