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

dart2js: does not reports error when using argument to const constructor in const constructor in initializer list #12690

Closed
andersjohnsen opened this issue Aug 23, 2013 · 14 comments
Assignees
Labels
closed-obsolete Closed as the reported issue is no longer relevant P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js

Comments

@andersjohnsen
Copy link

class Const1 {
  final int arg;

  final Const2 c2;

  const Const1({int arg})
    : arg = arg,
       c2 = const Const2(arg: arg);
}

class Const2 {
  final int arg;
  const Const2({int this.arg});
}

void main() {
  const v = const Const1(arg: 1);
}

reports

const.dart': Error: line 9 pos 31: expression must be a compile-time constant
       c2 = const Const2(arg: arg);
                              ^

@lrhn
Copy link
Member

lrhn commented Aug 23, 2013

The VM is correct. The specification is explicit about this: The argument to a const constructor invocation must be a compile time constant. It is not enough that it is a potential compile-time constant.

The bug here is that dart2js do not report an error.

The analyzer may want to check too.

Shorter example:

class Other {
  final x;
  const Other(this.x);
}
class C {
  final Other other;
  const C(int level) : other = const Other(level);
}
main() => const C(42).other.x;


Removed Area-VM label.
Added Area-Dart2JS label.
Changed the title to: "Dart2js does not reports error when using argument to const constructor in const constructor in initializer list".

@kasperl
Copy link

kasperl commented Aug 23, 2013

Adding Lars and Gilad since this is really a question about the language semantics. Clearly, the level parameter is always a compile-time constant if the C constructor is invoked as a const constructor (as in 'const C(42)') but not necessarily if C is invoked using 'new C(x)'.

At some point, we (at least) discussed automatically turning the 'const Other(level)' invocation into 'new Other(level)' when C is invoked as 'new C(x)'.


cc @gbracha.
cc @larsbak.

@DartBot
Copy link

DartBot commented Aug 23, 2013

This comment was originally written by @mhausner


Please don't make this more complicated than it already is.

Also consider the complication that arises if the argument is used in an expression.

class Other {
  final x;
  const Other(this.x);
}
class C {
  final Other other;
  const C(var level) : other = const Other(level + 1);
}

Here level+1 is a compile-time constant only if level is a number, but not if it's some other type.

@gbracha
Copy link
Contributor

gbracha commented Aug 23, 2013

I agree with comment #­3. We should not be fiddling with these rules at this point.

@peter-ahe-google
Copy link
Contributor

IIRC, we decided to drop the "turn const Other into new Other" approach to keep things simple.

@kasperl
Copy link

kasperl commented Sep 18, 2013

Added this to the M7 milestone.

@karlklose
Copy link
Contributor

Removed Priority-Unassigned label.
Added Priority-High label.

@kasperl
Copy link

kasperl commented Sep 25, 2013

Marked this as blocking #5519.

@kasperl
Copy link

kasperl commented Sep 25, 2013

Set owner to @karlklose.
Removed Priority-High label.
Added Priority-Medium label.

@kasperl
Copy link

kasperl commented Sep 25, 2013

Changed the title to: "dart2js: does not reports error when using argument to const constructor in const constructor in initializer list".

@kasperl
Copy link

kasperl commented Oct 2, 2013

Removed this from the M7 milestone.
Added this to the M8 milestone.

@kasperl
Copy link

kasperl commented Jun 4, 2014

Removed this from the M8 milestone.
Added this to the 1.6 milestone.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the 1.6 milestone.
Added Oldschool-Milestone-1.6 label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-1.6 label.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed triaged labels Feb 29, 2016
@sigmundch sigmundch added the closed-obsolete Closed as the reported issue is no longer relevant label Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-obsolete Closed as the reported issue is no longer relevant P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js
Projects
None yet
Development

No branches or pull requests

9 participants