dart-lang / dart-lang/language

Cast pattern executes even when operand subpattern is refuted

Open
#3,529 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

patterns
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Example taken from tests/language/patterns/exhaustiveness/nullable_cast_test.dart in the SDK:

import 'dart:async';

exhaustiveNonNullableFutureOrTypeVariable2<T extends Object>(FutureOr<T?> o) =>
    switch (o) {
      FutureOr<T>() as FutureOr<T> => 0,
    };

main() {
  exhaustiveNonNullableFutureOrTypeVariable2<int>(
      Future<int?>.value(null));
}

This code is currently expected to throw. I find it unintuitive that the cast in a cast pattern will execute even when the operand is refuted. Can we change the semantics so that the cast only occurs if the pattern match on the operand succeeds?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example in tests/language/patterns/exhaustiveness/nullable_cast_test.dart, especially the referenced cast-pattern case. Run the test to confirm the current throw, then read the surrounding test and the linked language issue context to identify the specified cast-pattern semantics. Done means the behavior and corresponding language specification tests reflect whether the operand subpattern succeeds before casting.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.