Skip to content

Question: What is the purpose of this cast? #42889

Closed
@nshahan

Description

@nshahan

I noticed simply tearing off a field that contains a function from a generic class produces an AsExpression node when the function uses the generic type argument in the contravariant position. The evaluation of TypeEnvironment.isSubtypeOf(from, to, SubtypeCheckMode.withNullabilities) evaluates to true but since the cast is marked with the TypeError and CovarianceCheck flags ddc will emit a cast that always seems to pass.

Is this cast necessary and is there an example that would cause this cast to fail?

class GenericType<T> {
  var genericFunction = (T t) => null;
}

main() {
  var generic = GenericType<int>();
  var tearOff = generic.genericFunction;
}
main = tes::main;
library from "org-dartlang-app:/test_02.dart" as tes {

  class GenericType<T extends core::Object? = dynamic> extends core::Object {
    field (tes::GenericType::T%) → core::Null? genericFunction = (tes::GenericType::T% t) → core::Null? => null;
    synthetic constructor •() → tes::GenericType<tes::GenericType::T%>
      : super core::Object::•()
      ;
  }
  static method main() → dynamic {
    tes::GenericType<core::int> generic = new tes::GenericType::•<core::int>();
    (core::int) → core::Null? tearOff = generic.{tes::GenericType::genericFunction} as{TypeError,CovarianceCheck,ForNonNullableByDefault} (core::int) → core::Null?;
  }
}

@johnniwinther @eernstg

Metadata

Metadata

Assignees

No one assigned

    Labels

    legacy-area-front-endLegacy: Use area-dart-model instead.type-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions