Skip to content

Update the kind of error the CFE produces for the failing patterns in irrefutable contexts #51720

@chloestefantsova

Description

@chloestefantsova

The runtime error that the CFE produces on a failed pattern match in an irrefutable context should be changed. Currently, the CFE uses ReachabilityError as a placeholder.

The following is a relevant and very helpful fragment from an offline conversation with @lrhn. Thank you for the clear and elaborate guidance, as always, Lasse!

First of all, a pattern in an irrefutable context cannot fail to match. That should be a compile-time error.
If it fails anyway, it's either because a ! or as failed, and then it's a TypeError as usual.
Or it's a list or map pattern which doesn't have the required elements/keys, in which case ... I'd be fine with just doing list[2] and letting it throw, but map["key"] won't throw, it just returns null, and we should throw if the key isn't there, even if the value pattern would accept null. So we do need to decide what to throw in that case.

Not inventing a new error would be nice. ReachabilityError is definitely a strawman name 🙂

I'd probably go with StateError. The state of the object being matched does not support the operation required (having a [2] element or ["key"] entry).
It's not an ArgumentError, because there is no function and no argument.
It's could be UnsupportedError, but that's usually reserved for an object which will never support that operation, and a list or map can be updated to have the value.
So, StateError matches, a state-based inability to support the operation, not an inherent inability like adding to an unmodifiable list.

That means it requires a message. I'd go with:

  StateError("List pattern requires ${_patternMinLength} elements, list has only ${list.length}")
  StateError("Map pattern requires key not provided by map value.")

Metadata

Metadata

Labels

P2A bug or feature request we're likely to work onlegacy-area-front-endLegacy: Use area-dart-model instead.model-messagesPoor/undesirable messaging in errors/warnings emitted by the analyzer and/or CFE.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions