private static void intersectFrom(Scope scope1, Scope scope2,
                                      Set<String> matchedNames,
                                      Collection<Variable> vars) {
        Set<Variable> privates1 = scope1.mPrivateVars;

        Variable[] vars1 = scope1.getLocallyDeclaredVariables();
        for (int i=0; i<vars1.length; i++) {
            Variable var1 = vars1[i];
            if (privates1 != null && privates1.contains(var1)) {
                continue;
            }

            String varName = var1.getName();

            if (matchedNames.contains(varName)) {
                // This variable has already been moved into the intersection.
                continue;
            }
            else {
                matchedNames.add(varName);
            }

            Variable var2 = scope2.getDeclaredVariable(varName, true);

            if (var2 == null) {
                // No matching public variable in scope2, so continue.
                continue;
            }

            Type type1 = var1.getType();
            Type type2 = var2.getType();

            // Find common type.
            Type type = type1.getCompatibleType(type2);
            if (type == null) {
                continue;
            }

            // Find a variable to hold common type.
            Variable var = null;
            if (type.equals(type1)) {
                var = var1;
            }
            else if (type.equals(type2)) {
                var = var2;
            }
            else {
                // Create a new variable with the common type.
                var = new Variable(var1.getSourceInfo(), varName, type, var != null ? var.isStaticallyTyped() : false);
            }

            vars.add(var);
        }
    }