Skip to content

Exhaustiveness error when using private mixins #53809

Closed
@craigdfoster

Description

@craigdfoster

I have a flutter project that uses mobX for state management.

MobX augments classes using code generated private mixins taking the following form:

class MyClass = _MyClassBase with _$MyClass; 

abstract class _MyClassBase with Store {...}

// code-gen...
mixin _$MyClass on _MyClassBase, Store {...}

I've augmented this to create a sealed class hierarchy in an attempt to take advantage of switch exhaustiveness:

sealed class Base {...};

class MyClass1 = _MyClass1Base with _$MyClass1;
sealed class _MyClass1Base extends Base with Store {...}

class MyClass2 = _MyClass2Base with _$MyClass2;
sealed class _MyClass2Base extends Base with Store {...}

If I do a switch statement on these classes, the VSCode analyser doesn't report any issues, but in most scenarios they will cause a compiler error.

Error: The type 'Base' is not exhaustively matched by the switch cases since it doesn't match '__$MyClass1&_MyClass1Base&Store()'.

__$MyClass1&_MyClass1Base&Store is the definition for MyClass1, it's private and non-instantiable, so it should be ignored for exhaustiveness right?

There is one scenario where this works, extending the code above...

sealed class Parent = _ParentBase with _$Parent;
sealed class _ParentBase extends Base with Store {...}

class Child1 = _Child1Base with _$Child1;
sealed class _Child1Base extends Parent with Store {...}

class Child2 = _Child2Base with _$Child2;
sealed class _Child2Base extends Parent with Store {...}

Base x = Child1();
Parent y = Child1();

// DOES NOT COMPILE
switch (x) {
  case MyClass1():
  case MyClass2():
  case Child1():
  case Child2():
}

// COMPILES
switch (y) {
  case Child1():
  case Child2():
}

Note the minor difference between Base and Parent.

I have a demo project showing the issue here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-duplicateClosed in favor of an existing report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions