sclang: interpreter bug when using method named 'switch' in custom class #754

Closed
redFrik opened this Issue Mar 5, 2013 · 7 comments

Comments

Projects
None yet
6 participants
Contributor

redFrik commented Mar 5, 2013

the follow code crashes sclang. sc352, sc357, sc363 on osx 10.8.2.

//--test class code
STest {
var <switch;
}
/*
a= STest.new;
a.switch //boom!
*/

Contributor

jamshark70 commented Mar 6, 2013

Compiler issue. It inserts special byte codes if none of the arguments to "switch" are anything other than an atomic literal (integer, float, char or symbol). Apparently, if there are no arguments in the call, then there is nothing to violate the condition.

But you should probably avoid "switch" as a method name anyway. You could write my_object.switch(1, { ... stuff ... }) and the compiler would have no way to know whether you mean "switch" as a control structure (where the special byte codes are an important speed improvement) or as a regular method call. The compiler can't check the class for an implementation of "switch" because the value of the receiver expression is unknown at compile time (so the receiver's class is also unknown).

At the very least, the compiler should not try to inline-optimize an empty argument list. But I can't think of a good way to guarantee that "switch" will dispatch to an object's method, unless we sacrifice the performance optimization and I don't think that would be a good idea either.

Contributor

timblechmann commented Mar 10, 2013

would be nicer to have a warning than a crash, though

scztt added the crash label Apr 19, 2015

brianlheim removed the crash label Jan 16, 2017

Member

brianlheim commented Jan 16, 2017

Tried the test above, I get this:

ERROR: Missing argument in switch statement  in file 'selected text'
  line 1 char 9:

  a.switch 

Can someone else test this, preferably on linux/win?

Member

snappizz commented Jan 16, 2017

@brianlheim Same error here on Linux, no crash.

Member

brianlheim commented Jan 16, 2017

Hmm. I can't name a method switch and have it work, though. So it is an issue with bytecodes. Perhaps switch (and similar) should have highlighting like thisProcess and be un-overrideable?

Also, this means Float:-switch is a no-op.

Contributor

jamshark70 commented Jan 16, 2017

I can't name a method switch and have it work, though. So it is an issue with bytecodes.

Correct, for reasons explained above -- invoking switch is not certain to compile to a normal method dispatch.

It might be worth making it illegal to declare switch anywhere except where it's already defined. Also check case, if, loop, while... I think those are the only ones but I might have forgotten some.

Member

brianlheim commented Jan 16, 2017

It might be worth making it illegal to declare switch anywhere except where it's already defined. Also check case, if, loop, while... I think those are the only ones but I might have forgotten some.

Agreed. I'm going to open an issue for that now. Closing this.

brianlheim closed this Jan 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment