Replace resolveToDescriptor with unsafeResolveToDescriptor
Now unsafeResolveToDescriptor is equivalent to resolveToDescriptorIfAny but throws exception in case no descriptor is found. Old version of resolveToDescriptor is kept intact but made deprecated. Some clarifying comments were added
This commit is contained in:
@@ -30,15 +30,34 @@ import org.jetbrains.kotlin.resolve.BindingTraceContext
|
||||
import org.jetbrains.kotlin.resolve.ImportPath
|
||||
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.lazy.NoDescriptorForDeclarationException
|
||||
|
||||
fun KtElement.getResolutionFacade(): ResolutionFacade =
|
||||
KotlinCacheService.getInstance(project).getResolutionFacade(listOf(this))
|
||||
|
||||
/**
|
||||
* For local declarations is equivalent to unsafeResolveToDescriptor(bodyResolveMode)
|
||||
*
|
||||
* But for non-local declarations it ignores bodyResolveMode and uses LazyDeclarationResolver directly
|
||||
*/
|
||||
@Deprecated(message = "This function has unclear semantics. Please use either unsafeResolveToDescriptor or resolveToDescriptorIfAny instead",
|
||||
replaceWith = ReplaceWith("unsafeResolveToDescriptor"))
|
||||
fun KtDeclaration.resolveToDescriptor(bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL): DeclarationDescriptor =
|
||||
getResolutionFacade().resolveToDescriptor(this, bodyResolveMode)
|
||||
|
||||
//TODO: BodyResolveMode.PARTIAL is not quite safe!
|
||||
/**
|
||||
* This function throws exception when resolveToDescriptorIfAny returns null, otherwise works equivalently.
|
||||
*/
|
||||
fun KtDeclaration.unsafeResolveToDescriptor(bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL): DeclarationDescriptor =
|
||||
resolveToDescriptorIfAny(bodyResolveMode) ?: throw NoDescriptorForDeclarationException(this)
|
||||
|
||||
/**
|
||||
* This function first uses declaration resolvers to resolve this declaration and/or additional declarations (e.g. its parent),
|
||||
* and then takes the relevant descriptor from binding context.
|
||||
* The exact set of declarations to resolve depends on bodyResolveMode
|
||||
*/
|
||||
fun KtDeclaration.resolveToDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL): DeclarationDescriptor? {
|
||||
//TODO: BodyResolveMode.PARTIAL is not quite safe!
|
||||
val context = analyze(bodyResolveMode)
|
||||
if (this is KtParameter && this.hasValOrVar()) {
|
||||
return context.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, this)
|
||||
|
||||
@@ -62,7 +62,7 @@ public class QuickFixUtil {
|
||||
public static KotlinType getDeclarationReturnType(KtNamedDeclaration declaration) {
|
||||
PsiFile file = declaration.getContainingFile();
|
||||
if (!(file instanceof KtFile)) return null;
|
||||
DeclarationDescriptor descriptor = ResolutionUtils.resolveToDescriptor(declaration, BodyResolveMode.FULL);
|
||||
DeclarationDescriptor descriptor = ResolutionUtils.unsafeResolveToDescriptor(declaration, BodyResolveMode.FULL);
|
||||
if (!(descriptor instanceof CallableDescriptor)) return null;
|
||||
KotlinType type = ((CallableDescriptor) descriptor).getReturnType();
|
||||
if (type instanceof DeferredType) {
|
||||
|
||||
Reference in New Issue
Block a user