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

Invoking emulated functions implemented using noSuchMethod sometimes fails in DDC #29904

Closed
greglittlefield-wf opened this issue Jun 16, 2017 · 5 comments
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler

Comments

@greglittlefield-wf
Copy link
Contributor

In the following code, invoking f2 throws an error.

class FunctionEmulator implements Function {
  call(a, b);
  noSuchMethod(i) => 'NSM ${i.positionalArguments}';
}

getF() => new FunctionEmulator();

main() {
  var f1 = new FunctionEmulator();
  print(f1(1, 2));

  var f2 = getF();
  print(f2(3, 4));
}

Here's the full output of executing this program:

NSM [1, 2]
require.js:143 Uncaught TypeError: Cannot read property 'noSuchMethod' of undefined
    at call (example__list__main.js:20)
    at Object.dart._checkAndCall (dart_sdk.js:2269)
    at Object.dart.dcall (dart_sdk.js:2293)
    at Object.list__main.main (example__list__main.js:42)
    at main.dart.bootstrap.js:97
    at Object.execCb (require.js:1696)
    at Module.check (require.js:878)
    at Module.<anonymous> (require.js:1139)
    at require.js:134
    at require.js:1189

Looking at the output, it seems that the second one uses dart.dcall:

    let f1 = new list__main.FunctionEmulator.new();
    core.print(f1(1, 2));
    let f2 = list__main.getF();
    core.print(dart.dcall(f2, 3, 4));

It's worth noting that adding a return type getF results in dart.dcall not being used, and the example works as expected:

-getF() => new FunctionEmulator();
+FunctionEmulator getF() => new FunctionEmulator();

This issue is present in Dart 1.24.0 and 1.25.0-dev.1.0

@greglittlefield-wf
Copy link
Contributor Author

Also, it's worth noting that while I can avoid dart.dcall being outputted here, I can't in other places, so it'd be nice if this worked.

@vsmenon vsmenon added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Jun 19, 2017
@vsmenon
Copy link
Member

vsmenon commented Jun 19, 2017

Yes, it looks like a bug in dynamic dispatch.

Note, you'd avoid the dcall if you typed getF - e.g.,

Object Function(Object, Object) getF() => new FunctionEmulator();

@greglittlefield-wf
Copy link
Contributor Author

greglittlefield-wf commented Jun 22, 2017

Another common case I find myself hitting this error is when initializing var variables in separate expressions:

class FunctionEmulator implements Function {
  call(a, b);
  noSuchMethod(i) => 'NSM ${i.positionalArguments}';
}

void main() {
  var condition = new DateTime.now().weekday == DateTime.THURSDAY;

  var f;
  if (condition) {
    f = new FunctionEmulator();
  } else {
    f = new FunctionEmulator();
  }

  print(f(1, 2));
}

Typing f as FunctionEmulator fixes the issue, as does using a ternary, but hitting the issue in this case can be quite confusing.

@vsmenon
Copy link
Member

vsmenon commented Jul 24, 2017

@jmesserly - is this fixed by your recent CL?

@jmesserly
Copy link

yup! Running the program above now prints NSM [1, 2]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler
Projects
None yet
Development

No branches or pull requests

4 participants