Chapel: Exporting function with generic array argument should throw user error

Created on 6 Aug 2017  Â·  13Comments  Â·  Source: chapel-lang/chapel

As noted in https://github.com/chapel-lang/pychapel/issues/80 trying to do an export on a function with an array argument as in

  proc flip_bit(v: [?D] int) {
    v[0] = -v[0];
    return v;
  }

causes an internal error

(pychapel) buddha314@notes:~/pychapel/tmp$ python call.py
/tmp/temp-lnjLSb.chpl:10: In function 'flip_bit':
/tmp/temp-lnjLSb.chpl:11: internal error: CAL0126 chpl Version 1.16.0 pre-release (c3663a54f0)

Internal errors indicate a bug in the Chapel compiler ("It's us, not you"),
and we're sorry for the hassle.  We would appreciate your reporting this bug --
please see http://chapel.cray.com/bugs.html for instructions.  In the meantime,
the filename + line number above may be useful in working around the issue.

g++: error: /tmp/tmpEB8cif.a: No such file or directory
Traceback (most recent call last):
  File "call.py", line 12, in <module>
    shout_out()
  File "/home/buddha314/.virtualenvs/pychapel/local/lib/python2.7/site-packages/pych/extern.py", line 212, in wrapped_f
    raise MaterializationError(self)
pych.exceptions.MaterializationError: Failed materializing ({'anames': [],
 'atypes': [],
 'bfile': None,
 'dec_fp': '/home/buddha314/pychapel/tmp/response.chpl',
 'dec_hs': '7ecfac2d168f3423f7104eeb38057ac3',
 'dec_ts': 1501804367,
 'doc': None,
 'efunc': None,
 'ename': 'shout_out',
 'lib': 'sfile-chapel-7ecfac2d168f3423f7104eeb38057ac3-1501804367.so',
 'module_dirs': [],
 'pfunc': <function shout_out at 0x7fe326f428c0>,
 'pname': 'shout_out',
 'rtype': None,
 'sfile': '/home/buddha314/pychapel/tmp/response.chpl',
 'slang': 'chapel',
 'source': None}).

So... whuz up wid dat?

Compiler good first issue Error Message user issue

Most helpful comment

So... whuz up wid dat?

I think the docs say it all:

As mentioned above, this feature is not very sturdy. Please refer to Reporting Chapel Issues if any problems are encountered.

Thanks for reporting so that we can fix this!

All 13 comments

So... whuz up wid dat?

I think the docs say it all:

As mentioned above, this feature is not very sturdy. Please refer to Reporting Chapel Issues if any problems are encountered.

Thanks for reporting so that we can fix this!

I took a stab at quick-fixing this in PR #6950.

(and by "fixing" it I meant "generating a useful user-facing error message).

For loose definitions if "fixing", I suppose! Let me know when and I'll try it again.

@buddha314: To head off some disappointment, my "fix" won't make anything about your program above work better than it currently does — it'll just create a user error message indicating that exported functions can't have generic arguments like v instead of the internal error message that was tripping over just that.

As a workaround, I'd suggest:

type vect = [0..0] int;

export
proc flip_bit(v: vect) {
  v[0] = -v[0];
  return v;
}

Essentially, I've made the routine completely non-generic by explicitly declaring the argument's type.

Unfortunately, you'd think the following should also work:

export
proc flip_bit(v: [0..0] int) {
  v[0] = -v[0];
  return v;
}

yet it doesn't because we consider such formal types to be generic as well (though arguably we shouldn't in an export declaration...).

Who votes for Brad for head of the Spirit Squad?! WOO-HOO!

I'll try the work-around, and I'll send you some pictures of kittens to cheer you up a bit.

I just merged PR #6950 which I think arguably closes this issue, but I'll wait for @buddha314 to confirm before doing so.

So I should pull from master and re-try these scripts? What is the new expected behavior?

In place of the internal error you got before, you're going to get an error message saying that you can't export flip_bit() because it's generic due to argument v (i.e., you'll still want to use the proposed workaround above to make progress).

Exported functions can't be generic because the way Chapel implements generics is to examine the calls to the function in order to determine what generic instantiations are needed (in your code, this corresponds to "what domains D are the array arguments that are passed in declared over?"). But since calls to an exported function are outside of Chapel (in this case, in Python), the compiler can't examine the callsites and make that determination. So such arguments have to be concrete (have known type).

(Also, note that, like the SO question I added a note to yesterday, the internal error above is completely independent of using Python / pyChapel -- it would also be triggered simply by compiling the Chapel program itself using chpl).

It's a rather complicated process to rebuild Chapel with PyChapel on Ubuntu https://github.com/chapel-lang/pychapel/issues/75 so give me a moment, I'm working it.

Okay! New error. With

response.chpl

proc flip_bit(v: [?D] int) {
  v[0] = -v[0];
  return v;
}

I get

> chpl reponse.chpl
response.chpl:2: error: exported function `flip_bit` can't be generic
response.chpl:2: note:    formal argument 'v' causes it to be

CWOSE DAT TIKUT, BWAD!

happy-kitten-kittens-5890512-1600-1200

:D

Was this page helpful?
0 / 5 - 0 ratings