Skip to content

CFE allows implicit tear-offs on nullable receivers #39710

@leafpetersen

Description

@leafpetersen

The CFE currently allows an implicit call method tear-off even when the receiver of the implicit tear-off is a nullable type:

class C {
  int call() => 0;
}

void main() {
  int Function() c0 = (null as C?);   // Should be an error
  int Function()? c1 = (null as C?); // Should be an error
  print(c0);
  print(c1);
}

Note that the first line is simply unsound. Both statements seem to evaluate to null.

For reference, I am proposing the following addition to the NNBD spec to cover this:

The implicit tear-off conversion which converts uses of instances of classes
with call methods to the tear-off of their `.call` method when the context type
is a function type is performed when the context type is a function type, or the
nullable version of a function type.  

Implicit tear-off conversion is *not* performed on objects of nullable type,
regardless of the context type. 

cc @stefantsov @johnniwinther

Metadata

Metadata

Labels

NNBDIssues related to NNBD Releaselegacy-area-front-endLegacy: Use area-dart-model instead.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions