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

Runtime error on static field named 'name' #587

Closed
vsmenon opened this issue Jun 10, 2016 · 6 comments
Closed

Runtime error on static field named 'name' #587

vsmenon opened this issue Jun 10, 2016 · 6 comments

Comments

@vsmenon
Copy link
Contributor

vsmenon commented Jun 10, 2016

class Field {
  static const name = 'Foo';
}

void main() {
  print(Field.name);
}

triggers this at runtime:

TypeError: Cannot assign to read only property 'name' of function 'class Field extends core.Object {}'
@vsmenon
Copy link
Contributor Author

vsmenon commented Jun 10, 2016

Looks like we don't check statics for conflicts in _emitMemberName.

@jmesserly
Copy link
Contributor

Huh. I could swear this used to work. :)

We need to use Object.defineProperty to define it. I think we used to send these down the lazy field path, which uses defineProperty, so it worked. It would also be fine to Object.defineProperty(Field.prototype, 'name', { value: 'Foo' }); ... the laziness is not important, only that we use defineProperty.

@vsmenon
Copy link
Contributor Author

vsmenon commented Jun 13, 2016

Should we be overwriting name? We could rename it (as we'd do with constructor I think).

Alternatively, we could revisit putting statics here.

@jmesserly
Copy link
Contributor

jmesserly commented Jun 13, 2016

Yeah we shouldn't rename it. It's equivalent to this perfectly legal ES6 code:

class Field {
  static get name() { return 'Foo'; }
}

@vsmenon vsmenon assigned vsmenon and smanilov and unassigned vsmenon Jun 15, 2016
@smanilov
Copy link

smanilov commented Jun 15, 2016

I'm not sure I can reproduce this. (Update: I can, leaving this comment for future reference.)

I'm invoking DDC like this:

dartdevc.dart compile -o foo bug.dart

where bug.dart contains the code from the initial comment.

@smanilov
Copy link

smanilov commented Jun 15, 2016

My problem was I was not actually running the code.

Explanation on how to run the code here: #485

Running the code as explained in the linked bug reproduces the error.

smanilov added a commit that referenced this issue Jun 15, 2016
Now one can overwrite fields like 'name' as 'static const' fields in
their class definition and not get runtime errors.

BUG= #587
R=jmesserly@google.com

Review URL: https://codereview.chromium.org/2071593003 .
smanilov added a commit that referenced this issue Jun 15, 2016
nex3 pushed a commit to dart-lang/sdk that referenced this issue Aug 31, 2016
Now one can overwrite fields like 'name' as 'static const' fields in
their class definition and not get runtime errors.

BUG= dart-archive/dev_compiler#587
R=jmesserly@google.com

Review URL: https://codereview.chromium.org/2071593003 .
nex3 pushed a commit to dart-lang/sdk that referenced this issue Aug 31, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants