InferenceVariable (List<InferenceVariable> vars, Map<InferenceVariable, Set<InferenceVariable>> depMap) { // Fast-path to find the first resolve group if it consists of single var InferenceVariable var = vars.get(0); if (var.getInstantiation() != PsiTypes.nullType() || depMap.get(var).isEmpty()) { return var; } Set<InferenceVariable> visited = new HashSet<>(); while (visited.add(var)) { if (var.getInstantiation() != PsiTypes.nullType()) { return var; } Set<InferenceVariable> deps = depMap.get(var); if (deps.isEmpty()) { return var; } InferenceVariable nextVar = ContainerUtil.find(deps, v -> vars.contains(v)); if (nextVar == null) { return var; } var = nextVar; } return null; }