kotlin icon indicating copy to clipboard operation
kotlin copied to clipboard

Experimental support for assign operator overload

Open asodja opened this issue 2 years ago • 3 comments

This PR adds experimental support for assign operator overload. Operator can be enabled with -Xassign-operator. Operator behaves similar to += operator.

It's implemented for F1.0 frontend and FIR frontend.

There are two issues that I know about but I didn't resolve for F1.0 frontend yet:

  1. rhs of assign can be resolved twice (performance issue)
  2. Case with smartcast/nullable extension operator with safe call, e.g.:
data class Foo(val x: Container)
data class Container(var value: String)
operator fun Container.assign(value: String) {
}
operator fun Container.plusAssign(value: String) {
}

fun test() {
    val nullCheck: Foo? = null
    // Old frontend
    // `=` diagnostics should be the same as `+=`
    <!VAL_REASSIGNMENT!>nullCheck?.x<!> = <!TYPE_MISMATCH!>"Fail"<!>
    <!DEBUG_INFO_SMARTCAST!>nullCheck?.x<!> <!NULLABLE_EXTENSION_OPERATOR_WITH_SAFE_CALL_RECEIVER!>+=<!> "Fail"
}

Let me know if you prefer that I squash commits, or if I can do something to make tests more readable.

asodja avatar Jun 30 '22 16:06 asodja

Thanks for your contribution! It would be great to rebase branch on master (currently we have some conflicts). Also it would be great to group commits somehow (squash all-in-one as the simplest idea, or, for example, leave FE10/FIR/backend changes in separate commits). At least commits like "small code cleanup", "remove unintentionally added file" should be squashed.

mglukhikh avatar Jul 01 '22 12:07 mglukhikh

Ok, I will try. I think I can group commits in some way, at least to separate fe10 and fir.

asodja avatar Jul 01 '22 12:07 asodja

I think this should be a bit easier to review:

  • first commit has main changes for FIR
  • second one has main changes for FE10
  • third commit is some polishing: puts changes for FIR behind a feature flag, removes delegate assign operator constraint for FE10 and adds additional tests/adjusts some tests

asodja avatar Jul 04 '22 17:07 asodja

This is not relevant anymore.

asodja avatar Sep 01 '22 08:09 asodja