Skip to content

Incorrect type inference when swapping type parameters in a constructor call #26990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stereotype441 opened this issue Jul 30, 2016 · 6 comments
Assignees
Labels
legacy-area-analyzer Use area-devexp instead. P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@stereotype441
Copy link
Member

stereotype441 commented Jul 30, 2016

The following code (inspired by HashBiMap.inverse from https://github.com/google/quiver-dart/blob/master/lib/src/collection/bimap.dart) produces strong mode errors:

    class Pair<T, U> {
      T t;
      U u;
      Pair(this.t, this.u);
      Pair<U, T> get reversed => new Pair(u, t);
    }

The errors are:

  • At new Pair(u, t): The return type 'Pair<U, U>' is not a 'Pair<U, T>', as defined by the method 'reversed'.
  • At the t in new Pair(u, t): The argument type 'T' cannot be assigned to the parameter type 'U'.

It seems like incorrect type parameters are being inferred for the call to the Pair constructor.

This behavior started happening in 8d2c6d0.

@bwilkerson bwilkerson added P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jul 30, 2016
@bwilkerson
Copy link
Member

@jmesserly

@jmesserly
Copy link

jmesserly commented Aug 1, 2016

good catch!

it looks like an incorrect capture bug.

I wonder if we have a similar bug with generic functions. We certainly tried very hard to avoid this class of bug in them :)

If not, there's a few possibilities on what could be going on. Constructors have some notable differences:

  • their downward inference uses matchType instead of the type variable constraint solver used for inference on generic functions/methods. (This is currently a TODO in the code)
  • they go through some contortions to get back to the "uninstantiated" constructor function type, which generic functions/methods don't need to worry about (this one might be a TODO as well :) )
  • we never fully refactored interface type generics to avoid capture like we did with function types

Anyway I'll dig in, but that's kind of a scary bug. Hopefully it's something little and not "omg this implementation is fundamentally broken" :)

@jmesserly jmesserly self-assigned this Aug 1, 2016
@jmesserly
Copy link

Couldn't reproduce this with generic functions, so it seems specific to constructors (which makes sense)

@jmesserly
Copy link

got it. Our substitution for the "extends" clause w/ recursive bounds was incorrectly affecting inference for this. Fix on the way

@jmesserly
Copy link

@scheglov
Copy link
Contributor

Possibly related: https://codereview.chromium.org/2376213003

scheglov added a commit that referenced this issue Oct 3, 2016
…e arguments.

There is internal code like the added test, which caused us to use
out of scope type parameter during inferred type computation.

Possibly related issues.
#26990
#27072

[email protected], [email protected], [email protected]
BUG=

Review URL: https://codereview.chromium.org/2376213003 .
whesse pushed a commit that referenced this issue Oct 5, 2016
…e arguments.

There is internal code like the added test, which caused us to use
out of scope type parameter during inferred type computation.

Possibly related issues.
#26990
#27072

[email protected], [email protected], [email protected]
BUG=

Review URL: https://codereview.chromium.org/2376213003 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants