Extract duplicated code, remove unused methods and unneeded OptIn.
Also, check original for descriptors only in case the symbol is not
computed yet. Checking it on every access is not needed.
Collecting all outer classes into a hash set and looking up classifiers
in that set for each type of each fake override is noticeably slow.
Instead, check that function types reference any type parameter whose
container is a class; if the function is not local, it means such type
parameter belongs to one of the outer classes.
#KT-42020
It's not that simple because we still need inline functions bodies
and classes fields which aren't present in Lazy IR. To overcome this,
save additional binary info for a cached library and then use it when needed
parameters.
This was causing exceptions with analysis API trying to resolve the
synthetic FirValueParameter because it could not find a KtDeclaration
for it. Using BODY_RESOLVE should be safe; the SAM constructor function
is also on BODY_RESOLVE and the type of the value parameter is known.
Partially fix KT-46525
Now the code like
```
fun foo(a: () -> Unit) {}
interface A {
fun run()
}
fun main() {
foo {
println()
}
val a = object : A {
override fun run() {
TODO()
}
}
}
```
is being compiled into
```
function foo(a) {
}
function A() {
}
A.$metadata$ = {
simpleName: 'A',
kind: 'interface',
interfaces: []
};
function main() {
foo(main$lambda());
var a = new _no_name_provided_();
}
function main$lambda() {
return function () {
println();
};
}
// TODO
function _no_name_provided_() {
}
_no_name_provided_.$metadata$ = {
kind: 'class',
interfaces: [A]
};
```
Consider the code below
```
fun test() {
a@ b@ {
{}
}
}
```
Currently when the code is converted to FIR, label `b` is bound to the
outer lambda and `a` gets bound to the inner lambda because it's not
consumed. This is wrong and also leads transfromation to fail with
exceptions because of the unexpected consumption of `a`.
This change fixes the above issue by designating a specific node in the
AST as the allowed user of a label when the label is added.
tailrec f(x: () -> T = { y }, y: T = ...) = f()
-- at the call we know that in `x` the observed value of `y` is `null`,
but the constructor should still have a single parameter.
This is needed so that SharedVariablesLowering doesn't get confused, and
SharedVariablesLowering should run after TailrecLowering to properly
optimize tailrec calls in inline lambdas.