Skip to content

Bug: Analyzer (and maybe runtime?) not properly narrowing when using extensions #2879

Open
@tcf909

Description

@tcf909

Summary:
I'm expecting to be able to narrow generics when using extensions. We are having to use extensions to properly identify a self return type of T1, but that needs to also come with the ability to narrow the generic within the Extension implementation.

I'm sure there is a valid reason why this is not occuring, but the allowed syntax (narrowing) and the implementation resulting from the syntax currently diverges. Either the syntax needs to be disallowed (which I hope is not the course of action) or the extension needs to correctly use the narrowed generic.

Version:

$dart --version
Dart SDK version: 2.18.5 (stable) (Tue Nov 22 15:47:29 2022 +0000) on "macos_arm64"

Reproduce:

main(){

  final otherClass = OtherClass();

  final test = TestClass();

  test.method(otherClass); // !!! This should be an analyzer + runtime error
}

class OtherClass {}

class OtherClassChild extends OtherClass {}

mixin TestMixin<T extends OtherClass> {}

class TestClass with TestMixin<OtherClassChild>{}

extension TestMixinExtension<T1 extends TestMixin<T2>, T2 extends OtherClass> on T1 {

  T1 method(final T2 should_be_narrowed_to_other_class_child){
    return this;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-inferenceType inference, issues or improvements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions