Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Overridden private fields trigger runtime error #370

Open

Description

@vsmenon

The following code:

class A {
  int _x = 42;
}

class B extends A {
  int _x = 95;
}

main() {
  var a = new B();
  print(a._x);
}

generates this error:

Uncaught TypeError: Cannot convert a Symbol value to a string
virtualField @ _classes.js:289

Activity

  1. jmesserly commented on Oct 22, 2015

    @jmesserly
    Contributor

    @vsmenon -- that's an illegal field override with field, right?

  2. jmesserly commented on Oct 22, 2015

    @jmesserly
    Contributor

    in other words, is this a dupe of #52

  3. vsmenon commented on Oct 22, 2015

    @vsmenon
    ContributorAuthor

    Right, if we statically disallow, we could make this a "won't fix".

  4. jmesserly commented on Oct 22, 2015

    @jmesserly
    Contributor

    Nice. I'll dupe it as the other bug covers either fix option. I think we're leaning towards restriction first. While runtime fix is pretty easy, it has some bad implications on the generated code.

  5. vsmenon commented on Oct 22, 2015

    @vsmenon
    ContributorAuthor

    I'm hitting a different error if I change the override to a getter:

    class A {
      int _x = 42;
    }
    
    class B extends A {
      int get _x => 32;
    }
    
    main() {
      var a = new B();
      print(a._x);
    }

    Error is:

    Uncaught TypeError: Cannot set property Symbol(_x) of [object Object] which has only a getter
    _isolate_helper.js:685
    
  6. vsmenon commented on Oct 22, 2015

    @vsmenon
    ContributorAuthor

    We could statically disallow this as well though.

  7. jmesserly commented on Oct 22, 2015

    @jmesserly
    Contributor

    good catch! I forgot if that was supposed to be supported or not :)

    does it work differently with public fields (I guess probably not)?

  8. jmesserly commented on Oct 22, 2015

    @jmesserly
    Contributor

    Looking at the comments, it looks like we didn't intend to support overriding fields at all, even with getter/setter. (It doesn't interact well with separate compilation. Basically has the same issue.)

    I wonder if this works, though:

    class A {
      int get _x => 42;
    }
    
    class B extends A {
      int _x = 32;
    }
    
    main() {
      var a = new B();
      print(a._x);
    }

    That one should work, but might not, because this code looks pretty fishy:
    https://github.com/dart-lang/dev_compiler/blob/ed47365eaa6985199555289616b242d2c2878a43/lib/runtime/_classes.js#L289

    FWIW, that string concat is semantically meaningless ... the string is only to help debugging.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions