sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Runtime type error in dart pad (compiled by dart2js) but not on VM

Open annagrin opened this issue 2 years ago • 2 comments

The following code runs successfully on VM and Chrome (if compiled with DDC), but fails on dart pad with a runtime error (or in chrome when compiled with dart2js):

// @dart = 2.17
import 'dart:async';

void main() {
  final controller = StreamController<Map<String, Object?>>();
  controller.stream.listen(print);
  addFoo(controller.sink);
  print('done');
}

void addFoo(StreamSink<Map<String, Object?>> sink)   {
  if (sink is StreamSink<Map<String, Object>>) throw 'bad sink'; // Removing this line makes the code work
  sink.add({'foo': 'bar'});
}

Expected

_StreamSinkWrapper<Map<String, Object?>>
done
{foo: bar}

Actual

_StreamSinkWrapper<Map<String, Object?>>
Uncaught TypeError: t1.add$1 is not a functionError: TypeError: t1.add$1 is not a function

annagrin avatar Aug 03 '22 18:08 annagrin

Reduced example with no imports:

class Foo<T> {
  void blah() => print("Foo");
}

void main() {
  final foo = Foo<int?>();
  if (foo is Foo<int>) throw 'bad foo';
  foo.blah();
}

fishythefish avatar Aug 03 '22 21:08 fishythefish

The static type computation in dart2js doesn't take nullability into account. I'm working on correcting this.

fishythefish avatar Aug 08 '22 21:08 fishythefish