Skip to content

False error reported when computing LUB of sealed class involving mixins #59828

Open
@Mike278

Description

@Mike278
$ dart --version
Dart SDK version: 3.6.0 (stable) (Thu Dec 5 07:46:24 2024 -0800) on "macos_x64"

In the following snippet, VSCode reports the error at the specified location.

mixin Equatable {}

sealed class S {}
class A extends S with Equatable {}
class B extends S with Equatable {}

S f1(bool b) {
  return b ? A() : B();
     //  ^^^^^^^^^^^^^ A value of type 'Object' can't be returned from the function 'f1' because it has a return type of 'S'
}

// control flow analysis can figure it out, no error here:
S f2(bool b) {
  S s;
  if (b) s = A();
  else s = B();
  return s;
}

However dart analyze finds no issues:

$ dart analyze lib/lub.dart 
Analyzing lub.dart...              0.7s
No issues found!

Removing the with Equatable clauses fixes the warning.

Interestingly, adding a top type to S also seems to fix the warning:

mixin Equatable {}

sealed class _Top {}
sealed class S extends _Top {}
class A extends S with Equatable {}
class B extends S with Equatable {}

S f1(bool b) {
  return b ? A() : B();
}
S f2(bool b) {
  S s;
  if (b) s = A();
  else s = B();
  return s;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart-cliUse area-dart-cli for issues related to the 'dart' command like tool.dart-cli-pubIssues related to the 'dart pub' tool

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions