Consider the following code:
```
fun test(a: List<String>) {
a.first()
}
```
The dispatch receiver type of `first` in this case is `List<T>` before
this change. After this change, it's `List<String>`.
In addition, this change also replace the dispatch receiver type with
the more specific type if available. For example, consider the following
```
class MyList: ArrayList<String>()
fun test(a: MyList) {
a.get(0)
}
```
The dispatch receiver type of `get` is `MyList`, instead of
`ArrayList<String>`. That is, a fake override is created in this case.
Various HL API needs to create fake override for symbols in libraries.
Without module data it may fail. Following exception is an example:
java.lang.IllegalStateException: Module data is not registered in Libraries session
at org.jetbrains.kotlin.fir.FirModuleDataKt.getModuleData(FirModuleData.kt:80)
at org.jetbrains.kotlin.fir.scopes.impl.FirFakeOverrideGenerator.createCopyForFirProperty(FirFakeOverrideGenerator.kt:297)
at org.jetbrains.kotlin.fir.scopes.impl.FirFakeOverrideGenerator.createCopyForFirProperty$default(FirFakeOverrideGenerator.kt:281)
at org.jetbrains.kotlin.fir.scopes.impl.FirFakeOverrideGenerator.createSubstitutionOverrideProperty(FirFakeOverrideGenerator.kt:271)
at org.jetbrains.kotlin.fir.scopes.impl.FirFakeOverrideGenerator.createSubstitutionOverrideProperty$default(FirFakeOverrideGenerator.kt:254)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassSubstitutionScope.createSubstitutionOverrideProperty(FirClassSubstitutionScope.kt:211)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassSubstitutionScope.access$createSubstitutionOverrideProperty(FirClassSubstitutionScope.kt:30)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassSubstitutionScope$processPropertiesByName$1.invoke(FirClassSubstitutionScope.kt:84)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassSubstitutionScope$processPropertiesByName$1.invoke(FirClassSubstitutionScope.kt:81)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope$processPropertiesByName$1.invoke(FirClassUseSiteMemberScope.kt:32)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope$processPropertiesByName$1.invoke(FirClassUseSiteMemberScope.kt:25)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScope.processPropertiesByName(FirClassDeclaredMemberScope.kt:115)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope.processPropertiesByName(FirClassUseSiteMemberScope.kt:25)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassSubstitutionScope.processPropertiesByName(FirClassSubstitutionScope.kt:81)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope.computeDirectOverridden(FirClassUseSiteMemberScope.kt:45)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope.access$computeDirectOverridden(FirClassUseSiteMemberScope.kt:17)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope$processPropertiesByName$1.invoke(FirClassUseSiteMemberScope.kt:28)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope$processPropertiesByName$1.invoke(FirClassUseSiteMemberScope.kt:25)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScope.processPropertiesByName(FirClassDeclaredMemberScope.kt:115)
at org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope.processPropertiesByName(FirClassUseSiteMemberScope.kt:25)
at org.jetbrains.kotlin.idea.frontend.api.fir.components.KtFirSymbolDeclarationOverridesProvider.processCallableByName(KtFirSymbolDeclarationOverridesProvider.kt:58)
at org.jetbrains.kotlin.idea.frontend.api.fir.components.KtFirSymbolDeclarationOverridesProvider.access$processCallableByName(KtFirSymbolDeclarationOverridesProvider.kt:29)
at org.jetbrains.kotlin.idea.frontend.api.fir.components.KtFirSymbolDeclarationOverridesProvider$getDirectlyOverriddenSymbols$$inlined$processOverrides$1.invoke(FirRefWithValidityCheck.kt:105)
at org.jetbrains.kotlin.idea.frontend.api.fir.components.KtFirSymbolDeclarationOverridesProvider$getDirectlyOverriddenSymbols$$inlined$processOverrides$1.invoke(FirRefWithValidityCheck.kt:33)
at org.jetbrains.kotlin.idea.fir.low.level.api.api.LowLevelFirApiFacadeKt.withFirDeclaration(LowLevelFirApiFacade.kt:129)
at org.jetbrains.kotlin.idea.frontend.api.fir.components.KtFirSymbolDeclarationOverridesProvider.getDirectlyOverriddenSymbols(KtFirSymbolDeclarationOverridesProvider.kt:253)
at org.jetbrains.kotlin.idea.frontend.api.components.KtSymbolDeclarationOverridesProviderMixIn$DefaultImpls.getDirectlyOverriddenSymbols(KtSymbolDeclarationOverridesProvider.kt:45)
at org.jetbrains.kotlin.idea.frontend.api.KtAnalysisSession.getDirectlyOverriddenSymbols(KtAnalysisSession.kt:29)
at org.jetbrains.kotlin.idea.completion.weighers.CallableWeigher.addWeight(CallableWeigher.kt:28)
at org.jetbrains.kotlin.idea.completion.weighers.Weighers.applyWeighsToLookupElement(Weighers.kt:61)
at org.jetbrains.kotlin.idea.completion.contributors.FirCompletionContributorBase.applyWeighers(FirCompletionContributorBase.kt:115)
at org.jetbrains.kotlin.idea.completion.contributors.FirCompletionContributorBase.addCallableSymbolToCompletion(FirCompletionContributorBase.kt:101)
at org.jetbrains.kotlin.idea.completion.contributors.FirCallableCompletionContributor.collectDotCompletionForCallableReceiver(FirCallableCompletionContributor.kt:189)
at org.jetbrains.kotlin.idea.completion.contributors.FirCallableCompletionContributor.collectDotCompletion(FirCallableCompletionContributor.kt:145)
at org.jetbrains.kotlin.idea.completion.contributors.FirCallableCompletionContributor.complete(FirCallableCompletionContributor.kt:79)
at org.jetbrains.kotlin.idea.completion.contributors.FirCallableCompletionContributor.complete(FirCallableCompletionContributor.kt:31)
at org.jetbrains.kotlin.idea.completion.contributors.FirCompletionContributorBaseKt.complete(FirCompletionContributorBase.kt:124)
at org.jetbrains.kotlin.idea.completion.Completions.complete(Completions.kt:18)
at org.jetbrains.kotlin.idea.completion.KotlinFirCompletionProvider.complete(KotlinFirCompletionContributor.kt:81)
at org.jetbrains.kotlin.idea.completion.KotlinFirCompletionProvider.addCompletions(KotlinFirCompletionContributor.kt:70)
at com.intellij.codeInsight.completion.CompletionProvider.addCompletionVariants(CompletionProvider.java:34)
at com.intellij.codeInsight.completion.CompletionContributor.fillCompletionVariants(CompletionContributor.java:156)
at com.intellij.codeInsight.completion.CompletionService.getVariantsFromContributors(CompletionService.java:77)
at com.intellij.codeInsight.completion.CompletionService.getVariantsFromContributors(CompletionService.java:60)
at com.intellij.codeInsight.completion.CompletionService.performCompletion(CompletionService.java:133)
at com.intellij.codeInsight.completion.BaseCompletionService.performCompletion(BaseCompletionService.java:41)
at com.intellij.codeInsight.completion.CompletionProgressIndicator.lambda$calculateItems$12(CompletionProgressIndicator.java:863)
at com.intellij.util.indexing.FileBasedIndex.lambda$ignoreDumbMode$0(FileBasedIndex.java:163)
at com.intellij.openapi.util.RecursionManager$1.computePreventingRecursion(RecursionManager.java:111)
at com.intellij.util.indexing.FileBasedIndexEx.ignoreDumbMode(FileBasedIndexEx.java:588)
at com.intellij.util.indexing.FileBasedIndex.ignoreDumbMode(FileBasedIndex.java:162)
at com.intellij.util.indexing.DumbModeAccessType.ignoreDumbMode(DumbModeAccessType.java:43)
at com.intellij.codeInsight.completion.CompletionProgressIndicator.calculateItems(CompletionProgressIndicator.java:859)
at com.intellij.codeInsight.completion.CompletionProgressIndicator.runContributors(CompletionProgressIndicator.java:847)
at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.lambda$startContributorThread$6(CodeCompletionHandlerBase.java:353)
at com.intellij.codeInsight.completion.AsyncCompletion.lambda$tryReadOrCancel$5(CompletionThreading.java:172)
at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1078)
at com.intellij.codeInsight.completion.AsyncCompletion.tryReadOrCancel(CompletionThreading.java:170)
at com.intellij.codeInsight.completion.CodeCompletionHandlerBase.lambda$startContributorThread$7(CodeCompletionHandlerBase.java:345)
at com.intellij.codeInsight.completion.AsyncCompletion.lambda$startThread$0(CompletionThreading.java:95)
at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:183)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:705)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:647)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:63)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:170)
at com.intellij.codeInsight.completion.AsyncCompletion.lambda$startThread$1(CompletionThreading.java:91)
at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:265)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
This is useful for quickfixes offering casts. We don't want to offer
user to cast incompatible types.
Also, explicitly allow compare to `Nothing` and handle `Nothing` from intersection
Also change
org.jetbrains.kotlin.fir.types.TypeUtilsKt#hideLocalTypeIfNeeded to skip
approximating anonymous objects if there are multiple super type refs so
that resolution behaves the same (for uncompilable code).
Note that this change does not implement check for
ApproximateAnonymousReturnTypesInPrivateInlineFunctions as the check is
already turned on in 1.5 and will likely not needed when FIR becomes
stable.
ProhibitAssigningSingleElementsToVarargsInNamedForm is enabled from 1.3
so there is no need to make FIR accepting single element passed through
named arguments.
In addition, we may want to report only
ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION and not arg
type mismatch. But FE1.0 is reporting both so I am following it. If we
want to remove the redundant report, we will probably want to remove
FirNamedVarargChecker and report during resolution as a cone diagnostic.
Difference from FE1.0
* KT-4285: calls to virtual method with default argument should be
reported as not tailrec. FE1.0 is missing such cases.
* KT-48600: calls inside lambda should be reported as not tailrec. FE1.0
also misses such cases.
Changes from FE1.0:
1. As discussed previously, no expression evaluation happens during this
check.
2. FE1.0 doesn't check redundant object comparisons.
FIR simply ignores anything after the first comma if the when expression
doesn't have a subject. Hence, the checker has to rely on PSI structure
instead.
This change touches the following diagnostics to make them behave closer
to FE1.0
* SUPER_NOT_AVAILABLE
* SUPER_IS_NOT_AN_EXPRESSION
* INSTANCE_ACCESS_BEFORE_SUPER_CALL
* NOT_A_SUPERTYPE
Other than tweaking the diagnostics, this change also alters resolution
by consider marking `super` with mismatched type parameter as
errorenous. As a result, the following code no longer resolves.
```
class A: B() {
fun test() {
super<String>.length
// ^^^^^^ FIR currently resolves this to `String.length`.
// With this change, `length` becomes unresolved
// instead
}
}
```
Also, now we report `UNRESOLVED_LABEL` on unresolved label on `super`
reference, though FE1.0 reports `UNRESOLVED_REFERENCE`.
All the errors above are reported as ConeDiagnostics and hence some
checkers are deleted.
In addition, it also suppresses more downstream (mostly unresolved)
errors if the receiver has errors. FE1.0 doesn't do it for all the cases
we have here. But it seems nicer to reduce these "redundant" unresolved
errors.
Consider an atomic reference `i`, it's FIR representation is
- FirQualifiedAccessExpression
- calleeReference : FirNamedReference
Currently, both of the above FIR elements uses the same PSI element as
the source. Such problems are not present with `this` or `super` because
we manually mark them as `ExplicitThisOrSuperReference`.
This change generalizes the previous `ExplicitThisOrSuperReference` as
`ReferenceInAtomicQualifiedAccess` and fixes it for more cases.
First, the order of arguments is swapped.
Second, projection erasure for argType was too aggressive. We should
instead retain the projections that are under an invariant position.
FE1.0 only reports SENSELESS_COMPARISON if one of the operand is `null`.
This change makes FIR reports also in case one of the operand has type
`Nothing?`.
In addition, fix handling of type alias in ConeTypeContext#isNullableType
In order to make resolution still work for members not available from
`Nothing`, we track the type without `Nothing?` and use that for
resolution instead.
The logic should clear back aliases as well. To ensure all back aliases
don't lose any information, statements on the original variable are
copied over to its aliases.
Previously, if user compares an `it<String, Int>` with `String`, the
checker reports it since the flattened types `[String, Int, Int]` are
incompatible. But technically, before flattening, the intersection type
actually contain the other side, so they should really be compatible.
The current SELECTOR_BY_QUALIFIED positioning strategy is closely
related what should be done here. But it only works on qualified access
expressions. This change also makes it work for type references.
These two diagnostics are similar: both are reported on type references
to enum entries. But `IS_ENUM_TYPE` is reported if the type ref is an
operand of `is` operator. To pass along this contextual information, a
boolean is added to FirSpecificTypeResolverTransformer.
The fix is a bit hacky, but it's very simple. In addition, it still does
not handle the case where the receiver is a lambda function. But such
case seems to be fairly rare in practice.
For a qualified name like `foo.bar.Outer.Inner`, FIR represents it as
one atomic FIR element. Hence, to properly resolve these names to the
corresponding package and class, we need some additional work.
The fix reuses logic that is already available from
FirWhenExhaustivenessTransformer to collect missing when branches. The
current logic unfortunately uses hackyAllowRunningOnEdt to shorten the
generated code.
As part of this change, we also extend the usage of RealVariable in more
places during DFA. Now mutable properties, property with custom getters,
delegated properties, etc are also treatd as a `RealVariable`. In
general this is needed in order to carry out smartcast computation in
order to report `SMARTCAST_IMPOSSIBLE`. It seems to also have side
effects that improves behavior of some test files.
As part of this change, we also extend the usage of RealVariable in more
places during DFA. Now mutable properties, property with custom getters,
delegated properties, etc are also treatd as a `RealVariable`. In
general this is needed in order to carry out smartcast computation in
order to report `SMARTCAST_IMPOSSIBLE`. It seems to also have side
effects that improves behavior of some test files.