Skip to content

Consider taking final classes into account for "impossible cast" warnings #29548

@leafpetersen

Description

@leafpetersen

#29547 proposes extending strong mode impossible cast warnings to explicit casts as well as implicit casts. For implicit casts, we are at least guaranteed that the types are subtype related, but for explicit casts we allow casts between non-subtype related classes, since there may be a type somewhere which implements both. However, if either class is final and they are not subtype related, then the only way the cast can succeed is if the value is null, which is highly unlikely to be what the user intended. We should consider taking this into account and issuing an error.

void test() {
 int x = 3;
 bool y = (x as bool); // Make this an error
}

cc @Hixie @lrhn @munificent @eernstg @floitschG

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).language-strong-mode-polish

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions