pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

`int` is not assignable to `int` (caused by inconsistent types when breaking cycles)

Open connernilsen opened this issue 2 months ago • 6 comments

Type error assigning int to int in while loop. repro

Repro contents:

def decode(s: str):
    i = 0
    while i < len(s):
        j = i
        while (
            s[j] != ""
        ):
            j += 1
        i = j + 1

connernilsen avatar Nov 13 '25 02:11 connernilsen

@connernilsen Hey! I took a look at this issue and I think I found the rootcause.

With nested loops that have cyclic variable dependencies (j = i then i = j + 1), the type checker creates LoopRecursive type variables to handle the cycles. But when comparing types, it was rejecting these variables instead of using their underlying types, causing the false positive

AryanBagade avatar Nov 14 '25 03:11 AryanBagade

I tried to fix it !! Modified is_subset_eq in solver.rs ( to handle LoopRecursive variables appearing in the want position by using their prior type instead of throwing an internal error. and its working shall i create a pr??

AryanBagade avatar Nov 14 '25 03:11 AryanBagade

cc @stroxler for opinions on the approach

yangdanny97 avatar Nov 14 '25 04:11 yangdanny97

any update on this ?

AryanBagade avatar Nov 18 '25 20:11 AryanBagade

I'm not 100% sure where the problem comes from (although there are two interlocking cycles here which I don't think is well-tested) so changing solver.rs to use prior types more aggressively is likely the solution.

So yes I think a PR would be helpful!

stroxler avatar Nov 21 '25 00:11 stroxler

@stroxler @yangdanny97 could you please review it, apologies for taking two weeks!

AryanBagade avatar Dec 01 '25 12:12 AryanBagade