PSI to FIR association issue: incorrect types on selects of dot qualified type references. #524
Description
PSI to FIR associations are not guaranteed since the FIR elements may have a null or fake source element.
Each part of a fully qualified type reference like foo.bar.TypeName
will be visited through KtSimpleNameExpression
. The PsiElementAssociation
class will attempt to associate a FIR element to every part of the FQN. PsiElementAssociation#primary
will traverse the PSI parent fields until a match is found.
The issue is variances between how to associate the PSI to the FIR due to differences in name references and aliased types. I've checked the PSI utilities and extension functions and found no utilities to identify when a KtSimpleNameExpression
is resolved to a type. There are many parts to generating the FIR (posterity: relevant FIR resolution QualifiedNameResolution
and FirCallResolver
), and resolving types. So, associating types to the PSI correctly without using the existing compiler code will require much reverse engineering.
To link the PSI to the IR, the Kotlin compiler team recommends using Fir2IrConverter.createIrModuleFragment
. The conversion to the IR returns an actualized result that contains caches that link the FIR to the IR, and enable the conversion of constants to IR constant expressions. Unfortunately, the caches do not store locally scoped variables, which must be done manually or retrieved from the cache while the variable still exists. Declarations relevant to the local scope MUST exist in the declaration caches to retrieve the cache or create a local variable. So, parsing the PSI elements in order is required, which has yet to happen.
In this case, the existence of foo.bar.TypeName
in each target of a J.FieldAccess
should not affect recipes unless an author has updated types without using ChangeType
.
Based on my analysis, we have two options if we find a case significantly affecting type attribution.
- Revise the parser visitor to parse in order and use existing visitors and utilities to link the PSI to the IR to attribute types.
- Ideal; the most reliable and correct type of attribution.
- Relevant sources:
psi2ir
package in Kotlin compiler andPsiSourceManager
.
- Do not traverse the PSI parents.