Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

KT-26983 - Kotlin compiler smart casting confused by var with same name in class member #1115

Open
SimonLammer opened this issue Sep 15, 2018 · 3 comments

Comments

@SimonLammer
Copy link

SimonLammer commented Sep 15, 2018

Buildscript:

open class CustomTask : DefaultTask() {
  var dir = File(".")

  @TaskAction
  fun action() {
    println("CustomTask.dir: ${dir.absolutePath}")
  }
}
tasks.create<CustomTask>("debug") {
  //dir = File(".gradle") // Script compilation fails if I uncomment this line
}

tasks.create("traverseDir") {
  doLast {
    traverseDir(File("gradle/wrapper/gradle-wrapper.jar"))
  }
}
fun traverseDir(file: File) {
  var dir: File? = file.parentFile
  while (dir != null) {
    println(dir)

    dir = dir.parentFile
  }
}

Expected Behavior

Build should continue to succeed when I configure the custom task.

Current Behavior

Buildscript fails to compile:

> Configure project :
e: /home/.../gradle-kotlin_dsl-smart_cast_error/build.gradle.kts:32:11: Smart cast to 'File' is impossible, because 'dir' is a local variable that is captured by a changing closure

FAILURE: Build failed with an exception.

* Where:
Build file '/home/.../gradle-kotlin_dsl-smart_cast_error/build.gradle.kts' line: 32

* What went wrong:
Script compilation error:

  Line 32:     dir = dir.parentFile
                     ^ Smart cast to 'File' is impossible, because 'dir' is a local variable that is captured by a changing closure

1 error

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s

Your Environment

Output of ./gradlew --version:

------------------------------------------------------------
Gradle 4.10
------------------------------------------------------------

Build time:   2018-08-27 18:35:06 UTC
Revision:     ee3751ed9f2034effc1f0072c2b2ee74b5dce67d

Kotlin DSL:   1.0-rc-3
Kotlin:       1.2.60
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_181 (Oracle Corporation 25.181-b13)
OS:           Linux 4.15.0-33-generic amd64

Additional

I have asked this on stackoverflow a few days ago: https://stackoverflow.com/questions/52302399/why-does-this-gradle-buildscript-kotlin-dsl-fail-to-compile-when-i-introduce
On the jetbrains kotlin issue tracker too: https://youtrack.jetbrains.com/issue/KT-26983

@eskatos
Copy link
Member

eskatos commented Sep 18, 2018

It looks like the Kotlin compiler is confused by dir being ambiguously resolved.
@SimonLammer could you please open an issue on JetBrains Youtrack https://youtrack.jetbrains.com/issues/KT and link it from here?

@SimonLammer
Copy link
Author

@eskatos eskatos changed the title Gradle buildscript fails to compile upon introduction of unrelated custom task KT-26983 - Kotlin compiler smart casting confused by var with same name in class member Sep 20, 2018
@eskatos
Copy link
Member

eskatos commented Sep 20, 2018

Thank you @SimonLammer

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants