Hi all, I was wondering why the following snippet ...
# compiler
k
Hi all, I was wondering why the following snippet doesn't compile:
Copy code
val x: Any? = get()
if (x is B || x is C) {
    println(x.b()) // <-- "Unresolved reference: b"
}
where
class C
implements
intrface B
and
b()
is a method defined in
B
I calculated the following smart types (see image) and don't see why it's not finding working. Is this a compiler bug? Playground: https://pl.kotl.in/mbuF47NGk
d
Such smartcasts are supported in K2 compiler
k
ah
I do have a question for which I can't seem to find an answer in the spec. Is there an appropriate channel for me to ask it in?
d
This channel is fine There are also #language-evolution and #language-proposals
k
ok, so imagine that after the
println
there is also an else branch. Now I know that the
smartCastTypeOf(x)
is gonna be
Any
but, I don't see how to get the actual
SmartCastType
.
It should be
LUB(C, ⊥) × GLB(B, LUB(B, C))
and I assume that
GLB(X, LUB(Y, Z)) == LUB(GLB(X, Y), GLB(X, Z))
but I can't seem to find that anywhere in the specs.
d
Actually, I didn't understand where smartcast to
Any
appeared in your calculations All we know for
else
branch is that
x !is B && x !is C
. It does not prevent
x
to be any other type, including
Nothing?
As for spec, then I should say that smartcasts are not specified
k
sorry I meant
Any?
, I have just been using java and forgot nullability was a thing 😃
d
Ok, then I don't fully understand your question
k
mmh, looking at the spec again, I realized that LUB and GLB aren't defined in terms of their normalizations rules, but on the subtyping relation directly.
So it makes sense that the normalization rules doesn't cover all cases
d
Since there is no specification for DFA, I can send a link to sources of data flow analyzer in K2 compiler It's written pretty well, so it may be readable even by not prepared reader
k
That would nice yes
d
Here is a root directory of DFA model ◦
model.kt
,
statements.kt
,
DfaVariables
.kt and
Flow.kt
describes main entities used in DFA ◦
Flow
is a information about smartcasts which we pass by edges of control flow graph ◦
LogicSystem
is a class with implementation of different operations over Flow • FirDataFlowAnalyzer is a main class which analyses all possible nodes in FIR tree and changes flow according to semantic
k
Thanks