@@ -361,13 +361,20 @@ optimizePotentialTailCall cycle name args expr =
361361optimizeTail :: Cycle -> N.Name -> [N.Name] -> Can.Expr -> Names.Tracker Opt.Expr
362362optimizeTail cycle rootName argNames locExpr@(A.At _ expression) =
363363 case expression of
364− Can.Call func@(A.At _ (Can.VarTopLevel _ name)) args ->
364+ Can.Call func args ->
365365 do oargs <- traverse (optimize cycle) args
366− if name == rootName
366+
367+ let isMatchingName =
368+ case A.toValue func of
369+ Can.VarLocal name -> rootName == name
370+ Can.VarTopLevel _ name -> rootName == name
371+ _ -> False
372+
373+ if isMatchingName
367374 then
368375 case Index.indexedZipWith (\_ a b -> (a,b)) argNames oargs of
369376 Index.LengthMatch pairs ->
370− pure $ Opt.TailCall name pairs
377+ pure $ Opt.TailCall rootName pairs
371378
372379 Index.LengthMismatch _ _ ->
373380 do ofunc <- optimize cycle func