Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving type guarantees when invoking #233

Merged

Conversation

GrigoriiSolnyshkin
Copy link
Collaborator

With this pull request necessary casts are added when calling and returning from lambdas and functions (inline and non-inline).

Before code like

fun <T> id(x: T) = x

inline fun <T, R> T.runWithId(block: T.() -> R) = id(this).block()

class Class {
  val member = 0
}

Class().runWithId { member }

wouldn't work: we only knew that the object returned from id had type Any?. Therefore, we make casts when entering (inline calls) and returning (from any calls) to keep TypeEmbedding consistent with kotlin types (this is also needed to correctly represent == calls etc.)

Comment on lines 144 to 147
if (argWithInvariants is VariableEmbedding) null to argWithInvariants
else declareAnonVar(callType, argWithInvariants).let {
it to it.variable
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My impression was that we already make copies of values passed to inlined functions; otherwise the following code is not translated correctly:

class X(var a: Int)
fun f(x: X) : Int = x.a.run { x.a += 1; it }

This isn't an idiomatic example, of course, but do we miscompile it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I added a couple of tests now that check we don't evaluate receiver twice. I'm not able to find any other copies when passing arguments. Anyway this part is just slightly changed logic of what was in this file before.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll try to experiment without this piece of code and report what happens.

Comment on lines 181 to 183
fun TypeEmbedding.subTypeInvariant(): SubTypeInvariantEmbedding? = equals(buildType { nullableAny() }).ifFalse {
SubTypeInvariantEmbedding(this)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this special case really worth it? I guess it reduces the number of inhales we do, but it may be hard to find why nullable any behaves differently. Perhaps with the type decomposition redesign we can do something nicer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I think it has some sense to introduce this optimization eventually. Particularly because it can enable further optimizations (like .simplified). But I don't think it should be done like this anyway.

@GrigoriiSolnyshkin GrigoriiSolnyshkin force-pushed the improving-type-guarantees-when-invoking branch from 09f5168 to 870e059 Compare August 16, 2024 10:01
Comment on lines 378 to 380
if (returnTarget.variable.type.equalsToType { unit() }) {
returnTarget.variable.withInvariants { proven = true }.toViperUnusedResult(linearizer)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is a bit hard to read because there's no verb: I'm guessing withInvariants produces an ExpEmbedding that inhales the invariants?

@@ -39,6 +39,8 @@ fun TypeBuilder.nullableAny(): AnyPretypeBuilder {

fun buildType(init: TypeBuilder.() -> PretypeBuilder): TypeEmbedding = TypeBuilder().complete(init)

fun TypeEmbedding.equalsToType(init: TypeBuilder.() -> PretypeBuilder) = equals(buildType { init() })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: equalToType or equalsType

@GrigoriiSolnyshkin GrigoriiSolnyshkin merged commit 2da4599 into formal-verification Aug 25, 2024
1 check passed
@GrigoriiSolnyshkin GrigoriiSolnyshkin deleted the improving-type-guarantees-when-invoking branch August 25, 2024 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants