Repository: JuliaLang/julia
File: src/subtype.c (C)

Code before:
                           e->invdepth, NULL, e->vars };
    JL_GC_PUSH4(&res, &vb.lb, &vb.ub, &vb.innervars);
    save_env(e, &se, 1);
    int noinv = !var_occurs_invariant(u->body, u->var);
    if (is_leaf_typevar(u->var) && noinv && always_occurs_cov(u->body, u->var, param))
        vb.constraintkind = 1;
    res = intersect_unionall_(t, u, e, R, param, &vb);
    vb.intersected = 1;
    if (vb.limited) {
        // if the environment got too big, avoid tree recursion and propagate the flag
        if (e->vars)
            e->vars->limited = 1;
    }
    else if (res != jl_bottom_type) {
        int constraint1 = vb.constraintkind;
        if (vb.concrete || vb.occurs_inv>1 || (vb.occurs_inv && vb.occurs_cov))
            vb.constraintkind = vb.concrete ? 1 : 2;
        else if (u->var->lb != jl_bottom_type)
            vb.constraintkind = 2;
        else if (vb.occurs_cov && noinv)
            vb.constraintkind = 1;
        int reintersection = constraint1 != vb.constraintkind || vb.concrete;
        if (reintersection) {
            if (constraint1 == 1) {
                vb.lb = vb.var->lb;
                vb.ub = vb.var->ub;
            }
            restore_env(e, &se, vb.constraintkind == 1 ? 1 : 0);
            vb.occurs_cov = vb.occurs_inv = 0;
            res = intersect_unionall_(t, u, e, R, param, &vb);
        }
    }
    if (res != jl_bottom_type && vb.constraintkind == 1 && vb.widened_to_kind) {
        // a `Type` was widened to a non-concrete kind union during intersection
        if (vb.occurs_cov > 1) {
            // diagonal: reintersect with the widened leaf bound to recover precision
            // or return Union{} if the widened bound is not a leaf
            if (is_leaf_bound(vb.ub)) {
                restore_env(e, &se, 1);
                vb.lb = vb.var->lb;
                vb.occurs_cov = vb.occurs_inv = 0;
                res = intersect_unionall_(t, u, e, R, param, &vb);
            }
            else {
                res = jl_bottom_type;
            }
        }
        else {
            // actually non-diagonal: reintersect without widening or constraint
            restore_env(e, &se, 1);
            vb.lb = vb.var->lb;

Review comment (bug):
In fact, non-leaf `lb` (rather than `ub`) violates the diagonal rule. Looks like the remaining `@test_broken` stems from this.

Code after:
    }
    else if (res != jl_bottom_type) {
        int constraint1 = vb.constraintkind;
        if (vb.concrete || vb.occurs_inv>1 || (vb.occurs_inv && vb.occurs_cov))
            vb.constraintkind = vb.concrete ? 1 : 2;
        else if (u->var->lb != jl_bottom_type)
            vb.constraintkind = 2;
        else if (vb.occurs_cov && noinv)
            vb.constraintkind = 1;
        int reintersection = constraint1 != vb.constraintkind || vb.concrete;
        if (reintersection) {
            if (constraint1 == 1) {
                vb.lb = vb.var->lb;
                vb.ub = vb.var->ub;
            }
            restore_env(e, &se, vb.constraintkind == 1 ? 1 : 0);
            vb.occurs_cov = vb.occurs_inv = 0;
            res = intersect_unionall_(t, u, e, R, param, &vb);
        }
    }
    if (res != jl_bottom_type && vb.constraintkind == 1 && vb.widened_to_kind == 1) {
        // a `Type` was widened to a non-concrete kind union during intersection
        if (vb.occurs_cov > 1) {
            // diagonal: reintersect if able to narrow across positions to a leaf bound
            // otherwise use original (possibly non-precise) bound
            if (is_leaf_bound(vb.ub)) {
                restore_env(e, &se, 1);
                vb.lb = vb.var->lb;
                vb.occurs_cov = vb.occurs_inv = 0;
                res = intersect_unionall_(t, u, e, R, param, &vb);
            }
        }
        else {
            // actually non-diagonal: reintersect without widening or constraint
            restore_env(e, &se, 1);
            vb.lb = vb.var->lb;
            vb.ub = vb.var->ub;
            vb.constraintkind = 0;
            vb.widened_to_kind = 0;
            vb.occurs_cov = vb.occurs_inv = 0;
            res = intersect_unionall_(t, u, e, R, param, &vb);
        }
    }
    free_env(&se);
    JL_GC_POP();
    return res;
}

static jl_value_t *intersect_invariant(jl_value_t *x, jl_value_t *y, jl_stenv_t *e);

// check n = (length of vararg type v)