Search in sources :

Example 1 with NominalType

use of com.google.javascript.jscomp.newtypes.NominalType in project closure-compiler by google.

the class GlobalTypeInfoCollector method checkAndFreezeNominalType.

/**
 * Reconcile the properties of this nominal type with the properties it inherits from its
 * supertypes, and then freeze the type.
 *
 * NOTE(dimvar): The logic is somewhat convoluted, but I'm not sure how to make it clearer.
 * There is just a lot of case analysis involved.
 *
 * - Collect all super definitions of methods and other properties.
 * - During the collection process, warn when a property on this type is not a subtype of
 *   an inherited property on the supertype.
 * - If there are multiple inherited definitions for a property, meet them to find a single
 *   inherited type. If they meet to bottom, warn.
 * - If this type has its own definition of the property, and not just inherits it, adjust the
 *   type of the local definition based on the inherited property type.
 */
private void checkAndFreezeNominalType(RawNominalType rawType) {
    if (rawType.isFrozen()) {
        return;
    }
    checkState(inProgressFreezes.add(rawType), "Cycle in freeze order: %s (%s)", rawType, inProgressFreezes);
    NominalType superClass = rawType.getSuperClass();
    Set<String> nonInheritedPropNames = rawType.getAllNonInheritedProps();
    if (superClass != null && !superClass.isFrozen()) {
        checkAndFreezeNominalType(superClass.getRawNominalType());
    }
    for (NominalType superInterf : rawType.getInterfaces()) {
        if (!superInterf.isFrozen()) {
            checkAndFreezeNominalType(superInterf.getRawNominalType());
        }
    }
    // If this type defines a method m, collect all inherited definitions of m here in order
    // to possibly adjust the type of m.
    Multimap<String, DeclaredFunctionType> superMethodTypes = LinkedHashMultimap.create();
    // For each property p on this type, regardless of whether it is defined on the type or just
    // inherited, collect all inherited definitions of p here.
    Multimap<String, JSType> superPropTypes = LinkedHashMultimap.create();
    // Collect inherited types for extended classes
    if (superClass != null) {
        checkState(superClass.isFrozen());
        // TODO(blickly): Can we optimize this to skip unnecessary iterations?
        for (String pname : superClass.getPropertyNames()) {
            if (superClass.isAbstractClass() && superClass.hasAbstractMethod(pname) && !rawType.isAbstractClass() && !rawType.mayHaveOwnNonStrayProp(pname)) {
                warnings.add(JSError.make(rawType.getDefSite(), ABSTRACT_METHOD_NOT_IMPLEMENTED_IN_CONCRETE_CLASS, pname, superClass.getName()));
            }
            nonInheritedPropNames.remove(pname);
            checkSuperProperty(rawType, superClass, pname, superMethodTypes, superPropTypes);
        }
    }
    // Collect inherited types for extended/implemented interfaces
    for (NominalType superInterf : rawType.getInterfaces()) {
        checkState(superInterf.isFrozen());
        for (String pname : superInterf.getPropertyNames()) {
            nonInheritedPropNames.remove(pname);
            checkSuperProperty(rawType, superInterf, pname, superMethodTypes, superPropTypes);
        }
    }
    // Munge inherited types of methods
    for (String pname : superMethodTypes.keySet()) {
        Collection<DeclaredFunctionType> methodTypes = superMethodTypes.get(pname);
        checkState(!methodTypes.isEmpty());
        PropertyDef localPropDef = checkNotNull(propertyDefs.get(rawType, pname));
        // To find the declared type of a method, we must meet declared types
        // from all inherited methods.
        DeclaredFunctionType superMethodType = DeclaredFunctionType.meet(methodTypes);
        DeclaredFunctionType localMethodType = localPropDef.methodType;
        boolean getsTypeFromParent = getsTypeInfoFromParentMethod(localPropDef);
        if (superMethodType == null) {
            // If the inherited types are not compatible, pick one.
            superMethodType = methodTypes.iterator().next();
        } else if (getsTypeFromParent && localMethodType.getMaxArity() > superMethodType.getMaxArity()) {
            // When getsTypeFromParent is true, we miss the invalid override earlier, so we check here.
            warnings.add(JSError.make(localPropDef.defSite, INVALID_PROP_OVERRIDE, pname, superMethodType.toFunctionType().toString(), localMethodType.toFunctionType().toString()));
        }
        DeclaredFunctionType updatedMethodType = localMethodType.withTypeInfoFromSuper(superMethodType, getsTypeFromParent);
        localPropDef.updateMethodType(updatedMethodType);
        superPropTypes.put(pname, getCommonTypes().fromFunctionType(updatedMethodType.toFunctionType()));
    }
    // Check inherited types of all properties
    for (String pname : superPropTypes.keySet()) {
        Collection<JSType> defs = superPropTypes.get(pname);
        checkState(!defs.isEmpty());
        JSType inheritedPropType = getCommonTypes().TOP;
        for (JSType inheritedType : defs) {
            inheritedPropType = JSType.meet(inheritedPropType, inheritedType);
            if (inheritedPropType.isBottom()) {
                warnings.add(JSError.make(rawType.getDefSite(), ANCESTOR_TYPES_HAVE_INCOMPATIBLE_PROPERTIES, rawType.getName(), pname, defs.toString()));
                break;
            }
        }
        // Adjust the type of the local property definition based on the inherited type.
        PropertyDef localPropDef = propertyDefs.get(rawType, pname);
        if (localPropDef != null) {
            JSType updatedPropType;
            if (localPropDef.methodType == null) {
                JSType t = rawType.getInstancePropDeclaredType(pname);
                updatedPropType = t == null ? inheritedPropType : t.specialize(inheritedPropType);
            } else {
                // When the property is a method, the local type already includes the meet of the
                // inherited types, from the earlier loop. We don't use inheritedPropType in this branch,
                // because if the inherited method type has static properties (e.g., framework-specific
                // passes can add such properties), we don't want to inherit these.
                FunctionType ft = localPropDef.methodType.toFunctionType();
                updatedPropType = getCommonTypes().fromFunctionType(ft);
            }
            // TODO(dimvar): check if we can have @const props here
            rawType.addProtoProperty(pname, null, updatedPropType, false);
        }
    }
    // Warn when inheriting from incompatible IObject types
    if (rawType.inheritsFromIObject()) {
        JSType wrapped = rawType.getInstanceAsJSType();
        if (wrapped.getIndexType() == null) {
            warnings.add(JSError.make(rawType.getDefSite(), ANCESTOR_TYPES_HAVE_INCOMPATIBLE_PROPERTIES, rawType.getName(), "IObject<K,V>#index", "the keys K have types that can't be joined."));
        } else if (wrapped.getIndexedType() == null) {
            warnings.add(JSError.make(rawType.getDefSite(), ANCESTOR_TYPES_HAVE_INCOMPATIBLE_PROPERTIES, rawType.getName(), "IObject<K,V>#index", "the values V should have a common subtype."));
        }
    }
    // Warn for a prop declared with @override that isn't overriding anything.
    for (String pname : nonInheritedPropNames) {
        PropertyDef propDef = propertyDefs.get(rawType, pname);
        if (propDef != null) {
            Node propDefsite = propDef.defSite;
            JSDocInfo jsdoc = NodeUtil.getBestJSDocInfo(propDefsite);
            if (jsdoc != null && jsdoc.isOverride()) {
                warnings.add(JSError.make(propDefsite, UNKNOWN_OVERRIDE, pname, rawType.getName()));
            }
        }
    }
    // Freeze nominal type once all properties are added.
    rawType.freeze();
    if (rawType.isBuiltinObject()) {
        NominalType literalObj = getCommonTypes().getLiteralObjNominalType();
        if (!literalObj.isFrozen()) {
            literalObj.getRawNominalType().freeze();
        }
    }
    inProgressFreezes.remove(rawType);
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) RawNominalType(com.google.javascript.jscomp.newtypes.RawNominalType) NominalType(com.google.javascript.jscomp.newtypes.NominalType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) Node(com.google.javascript.rhino.Node) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 2 with NominalType

use of com.google.javascript.jscomp.newtypes.NominalType in project closure-compiler by google.

the class GlobalTypeInfoCollector method getPropDefsFromInterface.

private ImmutableCollection<PropertyDef> getPropDefsFromInterface(NominalType nominalType, String pname) {
    checkArgument(nominalType.isFrozen());
    checkArgument(nominalType.isInterface() || nominalType.isBuiltinObject());
    if (nominalType.getPropDeclaredType(pname) == null) {
        return ImmutableSet.of();
    } else if (propertyDefs.get(nominalType.getId(), pname) != null) {
        PropertyDef propDef = propertyDefs.get(nominalType.getId(), pname);
        return ImmutableSet.of(nominalType.isGeneric() ? propDef.substituteNominalGenerics(nominalType) : propDef);
    }
    ImmutableSet.Builder<PropertyDef> result = ImmutableSet.builder();
    for (NominalType interf : nominalType.getInstantiatedInterfaces()) {
        result.addAll(getPropDefsFromInterface(interf, pname));
    }
    return result.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) RawNominalType(com.google.javascript.jscomp.newtypes.RawNominalType) NominalType(com.google.javascript.jscomp.newtypes.NominalType)

Example 3 with NominalType

use of com.google.javascript.jscomp.newtypes.NominalType in project closure-compiler by google.

the class NewTypeInference method analyzeSuperFwd.

private EnvTypePair analyzeSuperFwd(Node expr, TypeEnv inEnv) {
    checkArgument(expr.isSuper());
    if (this.currentScope.hasThis()) {
        NominalType thisClass = checkNotNull(envGetType(inEnv, THIS_ID).getNominalTypeIfSingletonObj());
        NominalType superClass = thisClass.getInstantiatedSuperclass();
        if (superClass == null) {
            // This indicates bad code and there will probably be other errors reported.
            // In particular JSC_NTI_INHERITANCE_CYCLE for `class Foo extends Foo ...`.
            warnings.add(JSError.make(expr, UNDEFINED_SUPER_CLASS, thisClass.toString()));
            return new EnvTypePair(inEnv, UNKNOWN);
        }
        if (this.currentScope.isConstructor()) {
            JSType superCtor = commonTypes.fromFunctionType(superClass.getConstructorFunction());
            return new EnvTypePair(inEnv, superCtor);
        }
        return new EnvTypePair(inEnv, superClass.getInstanceAsJSType());
    }
    // Use of super in a static method.
    Node funName = NodeUtil.getBestLValue(this.currentScope.getRoot());
    Node classNameNode = funName.getFirstChild();
    JSType thisClassAsJstype = analyzeExprFwd(classNameNode, inEnv).type;
    FunctionType thisCtor = thisClassAsJstype.getFunTypeIfSingletonObj();
    NominalType thisClass = thisCtor.getThisType().getNominalTypeIfSingletonObj();
    NominalType superClass = thisClass.getInstantiatedSuperclass();
    if (superClass == null) {
        // This indicates bad code and there will probably be other errors reported.
        // In particular JSC_NTI_INHERITANCE_CYCLE for `class Foo extends Foo ...`.
        warnings.add(JSError.make(expr, UNDEFINED_SUPER_CLASS, funName.toString()));
        return new EnvTypePair(inEnv, UNKNOWN);
    }
    return new EnvTypePair(inEnv, superClass.getNamespaceType());
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) NominalType(com.google.javascript.jscomp.newtypes.NominalType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType)

Aggregations

NominalType (com.google.javascript.jscomp.newtypes.NominalType)3 DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)2 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)2 JSType (com.google.javascript.jscomp.newtypes.JSType)2 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)2 Node (com.google.javascript.rhino.Node)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)1 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1