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 crashing when Main.create has an undefined method #2508

Closed
winksaville opened this issue Jan 20, 2018 · 5 comments
Closed

Compiler crashing when Main.create has an undefined method #2508

winksaville opened this issue Jan 20, 2018 · 5 comments

Comments

@winksaville
Copy link
Contributor

The code below and here on playground crashes the compiler:

type Children is (Child1 | Child2)

class Parent
  let env: Env
  var child1: (Child1 | None) = None
  var child2: (Child2 | None) = None
  var cur_data: (Children | None) = None

  new create(env': Env) =>
    env = env'
    child1 = Child1(this)
    child2 = Child2(this)
    change(child1)


  fun ref change(child: Children) =>
    cur_data = child

  fun ref doIt() =>
    match cur_data
    | let c: Children => c.doIt()
    end


class Child1
  let _parent: Parent

  new create(parent: Parent ref) =>
    _parent = parent

  fun getParent(): this->Parent =>
    _parent

  fun doIt() =>
    _parent.env.out.print("Child1.doIt()")



class Child2
  let _parent: Parent

  new create(parent: Parent ref) =>
    _parent = parent

  fun getParent(): this->Parent =>
    _parent

  fun doIt() =>
    _parent.env.out.print("Child2.doIt()")


actor Main
  var data: Parent

  new create(env: Env) =>
    data = Parent(env)
    doit()

  be doIt() =>
    data.doIt()

Here is the stack trace:

$ ponyc .
Building builtin -> /usr/local/lib/pony/0.21.3-42b521798/packages/builtin
Building . -> /home/wink/prgs/pony/parent-child-initialization
src/libponyc/pass/refer.c:228: suggest_alt_name: Assertion `ast_id(id) == TK_ID` failed.

Backtrace:
  ponyc(ponyint_assert_fail+0xf1) [0x55ceec6c93b1]
  ponyc(+0x6961f) [0x55ceec5f961f]
  ponyc(refer_reference+0xb9) [0x55ceec5f9821]
  ponyc(pass_refer+0x6f) [0x55ceec5fc484]
  ponyc(ast_visit+0x279) [0x55ceec5e0d36]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(ast_visit+0x1c5) [0x55ceec5e0c82]
  ponyc(+0x50341) [0x55ceec5e0341]
  ponyc(+0x506d4) [0x55ceec5e06d4]
  ponyc(ast_passes_program+0x28) [0x55ceec5e089c]
  ponyc(program_load+0xc1) [0x55ceec637648]
  ponyc(+0x4fc34) [0x55ceec5dfc34]
  ponyc(main+0x19c) [0x55ceec5dfe65]
  /usr/lib/libc.so.6(__libc_start_main+0xea) [0x7f1f41c77f4a]
  ponyc(_start+0x2a) [0x55ceec5dfaaa]
Aborted (core dumped)

This is from a debug version of current master as of today:

$ ponyc --version
0.21.3-42b521798 [debug]
compiled with: llvm 5.0.1 -- cc (GCC) 7.2.1 20171224
Defaults: pic=true openssl=openssl_1.1.0

I'll be working on getting it to compile and updating this issue as I learn more.

@winksaville
Copy link
Contributor Author

So here is a simpler version that crashes the compiler, the problem is I misspelled doIt():

actor Main
  new create(env: Env) =>
    doit() // crash compiler
    //doIt() // OK

  be doIt() =>
    None

Here is the crash

$ ponyc .
Building builtin -> /usr/local/lib/pony/0.21.3-42b521798/packages/builtin
Building . -> /home/wink/prgs/pony/parent-child-initialization
src/libponyc/pass/refer.c:228: suggest_alt_name: Assertion `ast_id(id) == TK_ID` failed.

Backtrace:
  ponyc(ponyint_assert_fail+0xf1) [0x561282b343b1]
  ponyc(+0x6961f) [0x561282a6461f]
  ponyc(refer_reference+0xb9) [0x561282a64821]
  ponyc(pass_refer+0x6f) [0x561282a67484]
  ponyc(ast_visit+0x279) [0x561282a4bd36]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(ast_visit+0x1c5) [0x561282a4bc82]
  ponyc(+0x50341) [0x561282a4b341]
  ponyc(+0x506d4) [0x561282a4b6d4]
  ponyc(ast_passes_program+0x28) [0x561282a4b89c]
  ponyc(program_load+0xc1) [0x561282aa2648]
  ponyc(+0x4fc34) [0x561282a4ac34]
  ponyc(main+0x19c) [0x561282a4ae65]
  /usr/lib/libc.so.6(__libc_start_main+0xea) [0x7f3fbd83ff4a]
  ponyc(_start+0x2a) [0x561282a4aaaa]
Aborted (core dumped)

@winksaville winksaville changed the title ponyc crash ponyc crashing with invoking a undefined method Jan 20, 2018
@winksaville winksaville changed the title ponyc crashing with invoking a undefined method Compiler crashing when invoking an undefined method Jan 20, 2018
@winksaville winksaville changed the title Compiler crashing when invoking an undefined method Compiler crashing when Main.create has an undefined method Jan 20, 2018
@Theodus
Copy link
Contributor

Theodus commented Jan 24, 2018

I can reproduce with latest ponyc

@Theodus
Copy link
Contributor

Theodus commented Jan 24, 2018

This is a problem with the compiler name suggestions (assertion at line 228):

static const char* suggest_alt_name(ast_t* ast, const char* name)
{
pony_assert(ast != NULL);
pony_assert(name != NULL);
size_t name_len = strlen(name);
if(is_name_private(name))
{
// Try without leading underscore
const char* try_name = stringtab(name + 1);
if(ast_get(ast, try_name, NULL) != NULL)
return try_name;
}
else
{
// Try with a leading underscore
char* buf = (char*)ponyint_pool_alloc_size(name_len + 2);
buf[0] = '_';
strncpy(buf + 1, name, name_len + 1);
const char* try_name = stringtab_consume(buf, name_len + 2);
if(ast_get(ast, try_name, NULL) != NULL)
return try_name;
}
// Try with a different case (without crossing type/value boundary)
ast_t* case_ast = ast_get_case(ast, name, NULL);
if(case_ast != NULL)
{
ast_t* id = case_ast;
if(ast_id(id) != TK_ID)
id = ast_child(id);
pony_assert(ast_id(id) == TK_ID);
const char* try_name = ast_name(id);
if(ast_get(ast, try_name, NULL) != NULL)
return try_name;
}

@winksaville
Copy link
Contributor Author

PR #2552 is a possible fix for this issue.

jemc pushed a commit that referenced this issue Feb 28, 2018
…2552)

* Fix for issue 2508

The pony_assert(ast_id(id == TK_ID) assumes that ast_id(id)) is
always a TK_ID, but that is not the true if the first child of case_ast
is a function or behavior.

This fix checks for a TK_ID for both first and second children of
case_ast and gives up if neither is a TK_ID instead of asserting.

* Add tests for suggest_alt_name

There are three cases tested plus a test for nothing can be suggested.
@winksaville
Copy link
Contributor Author

Fixed with #2552 being merged.

dipinhora pushed a commit to dipinhora/ponyc that referenced this issue Jun 5, 2018
…#2508). (ponylang#2552)

* Fix for issue 2508

The pony_assert(ast_id(id == TK_ID) assumes that ast_id(id)) is
always a TK_ID, but that is not the true if the first child of case_ast
is a function or behavior.

This fix checks for a TK_ID for both first and second children of
case_ast and gives up if neither is a TK_ID instead of asserting.

* Add tests for suggest_alt_name

There are three cases tested plus a test for nothing can be suggested.
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