Skip to content

Asserting Duration values between a range in const constructors gives an error only when calling #54632

Open
@FMorschel

Description

@FMorschel

Write this in dartpad.dev:

class A{
  const A(this.d) : assert(d >= Duration.zero);
  
  final Duration d;
}

void main () {
  const a = A(Duration(days: 1));
}

You'll get this error only on line 8:

In constant expressions, operands of this operator must be of type 'num'.

I tested it on my local development machine and still got the same error.

My question is, why can't this error be shown in line 2? I do believe this should appear there since packages that do not have tests would never see this and since this example only requires a single Duration as a parameter (I have a package that could have a future case like this), this class should not be able to be constant.

Second question, why can't this be asserted in constant expressions even if Durations are constant classes? Can't this be converted to Duration.inMicroseconds to assert internally?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestanalyzer-constantsarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-uxtype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions