Skip to content

Evaluate Xyz.fromEnvironment() as unknown value. #50045

Open

Description

@scheglov

We don't know which value we will get, so it is fair to say that the value is unknown.

The implementation is relatively straightforward.

But it breaks a couple of language/ tests, e.g. language/type/constants_test/07, which can be reduced to:

const bool fromEnvironment =
    const bool.fromEnvironment("foo", defaultValue: true);

void testSwitch(args) {
  switch (args) {
    case fromEnvironment ? const MyType() : Type: //# 07: compile-time error
  }
}

class MyType implements Type {
  const MyType();
  bool operator ==(Object other) => identical(this, other);
}

We stopped reporting the compile-time error, because the result of evaluating fromEnvironment ? const MyType() : Type is unknown, which does not implement operator ==.

Activity

  1. scheglov commented on Sep 23, 2022

    @scheglov
    ContributorAuthor

    So, I attempted to model unknownCondition ? left : right with EitherState, and ask both branches if they implement operator ==. https://dart-review.googlesource.com/c/sdk/+/260681

    But this breaks google3, specifically Angular. The protobuf generator builds enum-like classes with fields like ApiSharedSetTypePB_Enum ((super) = ProtobufEnum (name = String (Either(left: String (''), right: String ('NEGATIVE_PLACEMENTS'))); value = int (17))). And Angular compiler attempt to do:

      o.Expression _expressionForProtobufEnum(DartObject token) {
        print('[token: $token]');
        return o
            .importExpr(_idFor(token.type!))
            .prop(dart_objects.coerceString(token, 'name')!);
      }
    
    String? coerceString(DartObject? value, String field, {String? defaultTo}) =>
        getField(value, field)?.toStringValue() ?? defaultTo;

    But EitherState does not implement getField(), and cannot not do this - which one should it take - the left, or the right one? The values are different.

  2. scheglov commented on Sep 23, 2022

    @scheglov
    ContributorAuthor

    So, I don't know how to proceed.
    We either:

    1. leave it as is;
    2. or say that the language tests language/type/constants_test/07 and language/type/constants_test/08 are incorrect, and go with a simple version from https://dart-review.googlesource.com/c/sdk/+/260681/1

    @bwilkerson @eernstg @joshualitt

  3. bwilkerson commented on Sep 23, 2022

    @bwilkerson
    Member

    The language tests are valid. The analyzer might be failing those tests, but that doesn't make them incorrect.

    But the Angular compiler is currently broken. It's assuming that we can know the values of all constants at compile-time, but that isn't true. It needs to use hasKnownValue to determine whether the value is known and support the case where it returns false. We need to open an issue to let them know.

    But EitherState does not implement getField(), and cannot not do this ...

    It's perfectly valid for getField to return null, and I think that's probably the right thing for EitherState to do. That would also cause the Angular compiler to use defaultTo as the value of the string.

  4. scheglov commented on Sep 23, 2022

    @scheglov
    ContributorAuthor
  5. added
    P3A lower priority bug or feature request
    on Sep 26, 2022
  6. added
    type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)
    on Mar 14, 2024
  7. added
    area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.
    model-const-evalImplementation of constant evaluation in analyzer/cfe
    and removed on Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestanalyzer-constantsarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-const-evalImplementation of constant evaluation in analyzer/cfetype-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