private VariableDescriptor createLoopParameterDescriptor(
      KtParameter loopParameter,
      KotlinType expectedParameterType,
      ExpressionTypingContext context) {
    components
        .modifiersChecker
        .withTrace(context.trace)
        .checkParameterHasNoValOrVar(loopParameter, VAL_OR_VAR_ON_LOOP_PARAMETER);

    KtTypeReference typeReference = loopParameter.getTypeReference();
    VariableDescriptor variableDescriptor;
    if (typeReference != null) {
      variableDescriptor =
          components.descriptorResolver.resolveLocalVariableDescriptor(
              context.scope, loopParameter, context.trace);
      KotlinType actualParameterType = variableDescriptor.getType();
      if (expectedParameterType != null
          && !KotlinTypeChecker.DEFAULT.isSubtypeOf(expectedParameterType, actualParameterType)) {
        context.trace.report(
            TYPE_MISMATCH_IN_FOR_LOOP.on(
                typeReference, expectedParameterType, actualParameterType));
      }
    } else {
      if (expectedParameterType == null) {
        expectedParameterType = ErrorUtils.createErrorType("Error");
      }
      variableDescriptor =
          components.descriptorResolver.resolveLocalVariableDescriptor(
              loopParameter, expectedParameterType, context.trace, context.scope);
    }

    checkVariableShadowing(context.scope, context.trace, variableDescriptor);

    return variableDescriptor;
  }
Beispiel #2
0
 public static boolean isVarCapturedInClosure(
     BindingContext bindingContext, DeclarationDescriptor descriptor) {
   if (!(descriptor instanceof VariableDescriptor) || descriptor instanceof PropertyDescriptor)
     return false;
   VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor;
   return bindingContext.get(CAPTURED_IN_CLOSURE, variableDescriptor) != null
       && variableDescriptor.isVar();
 }
  @Override
  public KotlinTypeInfo visitTryExpression(
      @NotNull KtTryExpression expression, ExpressionTypingContext typingContext) {
    ExpressionTypingContext context = typingContext.replaceContextDependency(INDEPENDENT);
    KtExpression tryBlock = expression.getTryBlock();
    List<KtCatchClause> catchClauses = expression.getCatchClauses();
    KtFinallySection finallyBlock = expression.getFinallyBlock();
    List<KotlinType> types = new ArrayList<KotlinType>();
    for (KtCatchClause catchClause : catchClauses) {
      KtParameter catchParameter = catchClause.getCatchParameter();
      KtExpression catchBody = catchClause.getCatchBody();
      if (catchParameter != null) {
        components.identifierChecker.checkDeclaration(catchParameter, context.trace);
        ModifiersChecker.ModifiersCheckingProcedure modifiersChecking =
            components.modifiersChecker.withTrace(context.trace);
        modifiersChecking.checkParameterHasNoValOrVar(
            catchParameter, VAL_OR_VAR_ON_CATCH_PARAMETER);
        ModifierCheckerCore.INSTANCE$.check(catchParameter, context.trace, null);

        VariableDescriptor variableDescriptor =
            components.descriptorResolver.resolveLocalVariableDescriptor(
                context.scope, catchParameter, context.trace);
        KotlinType throwableType = components.builtIns.getThrowable().getDefaultType();
        components.dataFlowAnalyzer.checkType(
            variableDescriptor.getType(),
            catchParameter,
            context.replaceExpectedType(throwableType));
        if (catchBody != null) {
          LexicalWritableScope catchScope = newWritableScopeImpl(context, "Catch scope");
          catchScope.addVariableDescriptor(variableDescriptor);
          KotlinType type =
              facade.getTypeInfo(catchBody, context.replaceScope(catchScope)).getType();
          if (type != null) {
            types.add(type);
          }
        }
      }
    }

    KotlinTypeInfo result = TypeInfoFactoryKt.noTypeInfo(context);
    if (finallyBlock != null) {
      result =
          facade.getTypeInfo(
              finallyBlock.getFinalExpression(), context.replaceExpectedType(NO_EXPECTED_TYPE));
    }

    KotlinType type = facade.getTypeInfo(tryBlock, context).getType();
    if (type != null) {
      types.add(type);
    }
    if (types.isEmpty()) {
      return result.clearType();
    } else {
      return result.replaceType(CommonSupertypes.commonSupertype(types));
    }
  }