@SuppressWarnings("ConstantConditions")
  private JetScope addImports(JetScope scope) {
    WritableScopeImpl writableScope =
        new WritableScopeImpl(
            scope,
            scope.getContainingDeclaration(),
            RedeclarationHandler.DO_NOTHING,
            "JetTypeCheckerTest.addImports");
    List<JetScope> scopeChain = new ArrayList<JetScope>();
    scopeChain.add(writableScope);

    ModuleDescriptor module = LazyResolveTestUtil.resolveProject(getProject());
    for (ImportPath defaultImport : module.getDefaultImports()) {
      FqName fqName = defaultImport.fqnPart();
      if (defaultImport.isAllUnder()) {
        scopeChain.add(module.getPackage(fqName).getMemberScope());
      } else {
        Name shortName = fqName.shortName();
        assert shortName.equals(defaultImport.getImportedName());
        writableScope.addClassifierDescriptor(
            module.getPackage(fqName.parent()).getMemberScope().getClassifier(shortName));
      }
    }
    scopeChain.add(module.getPackage(FqName.ROOT).getMemberScope());
    writableScope.changeLockLevel(WritableScope.LockLevel.BOTH);
    return new ChainedScope(
        scope.getContainingDeclaration(),
        "JetTypeCheckerTest.addImports scope with imports",
        scopeChain.toArray(new JetScope[scopeChain.size()]));
  }
  @NotNull
  public JetScope getScopeForClassHeaderResolution() {
    if (scopeForClassHeaderResolution == null) {
      WritableScopeImpl scope =
          new WritableScopeImpl(
              JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Class Header Resolution");
      for (TypeParameterDescriptor typeParameterDescriptor : getTypeConstructor().getParameters()) {
        scope.addClassifierDescriptor(typeParameterDescriptor);
      }
      scope.changeLockLevel(WritableScope.LockLevel.READING);

      scopeForClassHeaderResolution =
          new ChainedScope(
              this,
              scope,
              resolveSession.getResolutionScope(
                  declarationProvider.getOwnerInfo().getScopeAnchor()));
    }
    return scopeForClassHeaderResolution;
  }
  public JetScope getScopeForMemberDeclarationResolution() {
    if (scopeForMemberDeclarationResolution == null) {
      WritableScopeImpl scope =
          new WritableScopeImpl(
              JetScope.EMPTY,
              this,
              RedeclarationHandler.DO_NOTHING,
              "Member Declaration Resolution");
      scope.addLabeledDeclaration(this);
      scope.changeLockLevel(WritableScope.LockLevel.READING);

      scopeForMemberDeclarationResolution =
          new ChainedScope(
              this,
              getScopeForMemberLookup().getImplicitReceiver(),
              scope,
              getScopeForMemberLookup(),
              getScopeForClassHeaderResolution());
    }
    return scopeForMemberDeclarationResolution;
  }
  @NotNull
  private JetScope computeScopeForClassHeaderResolution() {
    WritableScopeImpl scope =
        new WritableScopeImpl(
            JetScope.EMPTY,
            this,
            RedeclarationHandler.DO_NOTHING,
            "Scope with type parameters for " + getName());
    for (TypeParameterDescriptor typeParameterDescriptor : getTypeConstructor().getParameters()) {
      scope.addClassifierDescriptor(typeParameterDescriptor);
    }
    scope.changeLockLevel(WritableScope.LockLevel.READING);

    PsiElement scopeAnchor = declarationProvider.getOwnerInfo().getScopeAnchor();

    return new ChainedScope(
        this,
        "ScopeForClassHeaderResolution: " + getName(),
        scope,
        getScopeProvider().getResolutionScopeForDeclaration(scopeAnchor));
  }
 @SuppressWarnings("ConstantConditions")
 private WritableScopeImpl addImports(JetScope scope) {
   WritableScopeImpl writableScope =
       new WritableScopeImpl(
           scope,
           scope.getContainingDeclaration(),
           RedeclarationHandler.DO_NOTHING,
           "JetTypeCheckerTest.addImports");
   InjectorForJavaDescriptorResolver injector =
       new InjectorForJavaDescriptorResolver(getProject(), new BindingTraceContext());
   JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver();
   writableScope.importScope(
       javaDescriptorResolver
           .resolveNamespace(FqName.ROOT, INCLUDE_KOTLIN_SOURCES)
           .getMemberScope());
   writableScope.importScope(
       javaDescriptorResolver
           .resolveNamespace(new FqName("java.lang"), IGNORE_KOTLIN_SOURCES)
           .getMemberScope());
   writableScope.changeLockLevel(WritableScope.LockLevel.BOTH);
   writableScope.importScope(builtIns.getBuiltInsScope());
   return writableScope;
 }
  @NotNull
  private JetScope computeScopeForPropertyInitializerResolution() {
    ConstructorDescriptor primaryConstructor = getUnsubstitutedPrimaryConstructor();
    if (primaryConstructor == null) return getScopeForMemberDeclarationResolution();

    WritableScopeImpl scope =
        new WritableScopeImpl(
            JetScope.EMPTY,
            this,
            RedeclarationHandler.DO_NOTHING,
            "Scope with constructor parameters in " + getName());
    for (ValueParameterDescriptor valueParameterDescriptor :
        primaryConstructor.getValueParameters()) {
      scope.addVariableDescriptor(valueParameterDescriptor);
    }
    scope.changeLockLevel(WritableScope.LockLevel.READING);

    return new ChainedScope(
        this,
        "ScopeForPropertyInitializerResolution: " + getName(),
        scope,
        getScopeForMemberDeclarationResolution());
  }
  @NotNull
  private JetScope computeScopeForMemberDeclarationResolution() {
    WritableScopeImpl thisScope =
        new WritableScopeImpl(
            JetScope.EMPTY,
            this,
            RedeclarationHandler.DO_NOTHING,
            "Scope with 'this' for " + getName());
    thisScope.addLabeledDeclaration(this);
    thisScope.changeLockLevel(WritableScope.LockLevel.READING);

    ClassDescriptor classObject = getClassObjectDescriptor();
    JetScope classObjectAdapterScope =
        (classObject != null) ? new ClassObjectMixinScope(classObject) : JetScope.EMPTY;

    return new ChainedScope(
        this,
        "ScopeForMemberDeclarationResolution: " + getName(),
        thisScope,
        getScopeForMemberLookup(),
        getScopeForClassHeaderResolution(),
        classObjectAdapterScope);
  }
  public JetScope getScopeForPropertyInitializerResolution() {
    ConstructorDescriptor primaryConstructor = getUnsubstitutedPrimaryConstructor();
    if (primaryConstructor == null) return getScopeForMemberDeclarationResolution();

    if (scopeForPropertyInitializerResolution == null) {
      WritableScopeImpl scope =
          new WritableScopeImpl(
              JetScope.EMPTY,
              this,
              RedeclarationHandler.DO_NOTHING,
              "Property Initializer Resolution");

      List<ValueParameterDescriptor> parameters = primaryConstructor.getValueParameters();
      for (ValueParameterDescriptor valueParameterDescriptor : parameters) {
        scope.addVariableDescriptor(valueParameterDescriptor);
      }

      scope.changeLockLevel(WritableScope.LockLevel.READING);

      scopeForPropertyInitializerResolution =
          new ChainedScope(this, scope, getScopeForMemberDeclarationResolution());
    }
    return scopeForPropertyInitializerResolution;
  }