Skip to content

Commit

Permalink
Update Scala target computation
Browse files Browse the repository at this point in the history
Only use release when an explicit toolchain is configured.

Fixes #23905
  • Loading branch information
ljacomet committed Feb 17, 2023
1 parent fa74f94 commit 46ba8d1
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 7 deletions.
Expand Up @@ -1402,6 +1402,9 @@ private void validate(String output, String displayName) {
} else if (line.matches(".*w: .* is deprecated\\..*")) {
// A kotlinc warning, ignore
i++;
} else if (line.matches("\\[Warn] :.* is deprecated: .*")) {
// A scalac warning, ignore
i++;
} else if (isDeprecationMessageInHelpDescription(line)) {
i++;
} else if (removeFirstExpectedDeprecationWarning(line)) {
Expand Down
Expand Up @@ -22,11 +22,13 @@ import java.util.regex.Pattern

/**
* Removes Scala Zinc compilation time logging from the build-init Scala samples output.
* Also removes the potential warning around using `-target` instead of `-release`.
* */
class ZincScalaCompilerOutputNormalizer implements OutputNormalizer {

private static final PATTERN = Pattern.compile(
"Scala Compiler interface compilation took ([0-9]+ hrs )?([0-9]+ mins )?[0-9.]+ secs\n\n",
"(Scala Compiler interface compilation took ([0-9]+ hrs )?([0-9]+ mins )?[0-9.]+ secs\n\n)" +
"|(\\[Warn] : -target is deprecated: Use -release instead to compile against the correct platform API.\none warning found\n\n)",
Pattern.MULTILINE
)

Expand Down
Expand Up @@ -67,6 +67,43 @@ Scala Compiler interface compilation took 1 hrs 20 mins 41.884 secs
> Task :app:check
> Task :app:build
BUILD SUCCESSFUL in 0s
7 actionable tasks: 7 executed
""".trim()

when:
String normalized = outputNormalizer.normalize(input, executionMetadata)

then:
normalized == expected
}

def "successfully normalizes Scala compiler warning"() {
given:
String input = """
> Task :app:compileJava NO-SOURCE
> Task :app:compileScala
[Warn] : -target is deprecated: Use -release instead to compile against the correct platform API.
one warning found
> Task :app:processResources NO-SOURCE
> Task :app:classes
> Task :app:jar
> Task :app:startScripts
> Task :app:distTar
> Task :app:distZip
> Task :app:assemble
> Task :app:compileTestJava NO-SOURCE
> Task :app:compileTestScala
[Warn] : -target is deprecated: Use -release instead to compile against the correct platform API.
one warning found
> Task :app:processTestResources NO-SOURCE
> Task :app:testClasses
> Task :app:test
> Task :app:check
> Task :app:build
BUILD SUCCESSFUL in 0s
7 actionable tasks: 7 executed
""".trim()
Expand Down
Expand Up @@ -81,6 +81,7 @@ class ScalaPluginIntegrationTest extends MultiVersionIntegrationSpec {
"""

expect:
executer.noDeprecationChecks()
succeeds(":a:classes", "--parallel")
true
}
Expand Down
Expand Up @@ -22,6 +22,8 @@ import org.gradle.integtests.fixtures.ScalaCoverage
import org.gradle.integtests.fixtures.TargetCoverage
import org.gradle.integtests.fixtures.jvm.JavaToolchainFixture
import org.gradle.internal.jvm.Jvm
import org.gradle.util.Requires
import org.gradle.util.TestPrecondition

import static org.gradle.scala.ScalaCompilationFixture.scalaDependency

Expand Down Expand Up @@ -96,4 +98,25 @@ class Person {
"immutable list" | "[].asImmutable()"
}

@Requires(TestPrecondition.JDK11_OR_LATER)
def "can compile sources using later JDK APIs"() {
file("src/main/scala/App.scala") << """
object App {
def main(args: Array[String]): Unit = {
var x : String = "Test String"
// isBlank is from JDK 11
println(x.isBlank)
}
}
"""

when:
run ":compileScala"

then:
executedAndNotSkipped(":compileScala")

JavaVersion.forClass(scalaClassFile("App.class").bytes) == JavaVersion.VERSION_1_8
}

}
Expand Up @@ -79,10 +79,30 @@ private static boolean hasTargetDefiningParameter(List<String> additionalParamet
return additionalParameters.stream().anyMatch(s -> TARGET_DEFINING_PARAMETERS.stream().anyMatch(s::startsWith));
}

/**
* Computes parameter to specify how Scala should handle Java APIs and produced bytecode version.
* <p>
* The exact result depends on the Scala version in use and if the toolchain is user specified or not.
*
* @param scalaVersion The detected scala version
* @param javaToolchain The toolchain used to run compilation
* @return a Scala compiler parameter
*/
private static String determineTargetParameter(VersionNumber scalaVersion, JavaToolchain javaToolchain) {
int effectiveTarget = javaToolchain.isFallbackToolchain() ? FALLBACK_JVM_TARGET : javaToolchain.getLanguageVersion().asInt();
if (scalaVersion.compareTo(RELEASE_REPLACES_TARGET_SINCE_VERSION) >= 0) {
return String.format("-release:%s", effectiveTarget);
boolean explicitToolchain = !javaToolchain.isFallbackToolchain();
int effectiveTarget = !explicitToolchain ? FALLBACK_JVM_TARGET : javaToolchain.getLanguageVersion().asInt();
if (scalaVersion.compareTo(VersionNumber.parse("3.0.0")) >= 0) {
if (explicitToolchain) {
return String.format("-release:%s", effectiveTarget);
} else {
return String.format("-Xtarget:%s", effectiveTarget);
}
} else if (scalaVersion.compareTo(RELEASE_REPLACES_TARGET_SINCE_VERSION) >= 0) {
if (explicitToolchain) {
return String.format("-release:%s", effectiveTarget);
} else {
return String.format("-target:%s", effectiveTarget);
}
} else if (scalaVersion.compareTo(PLAIN_TARGET_FORMAT_SINCE_VERSION) >= 0) {
return String.format("-target:%s", effectiveTarget);
} else {
Expand Down
Expand Up @@ -81,9 +81,9 @@ class ScalaCompileOptionsConfigurerTest extends Specification {
17 | false | '2.13.10' | '-release:17'
17 | false | '3.2.1' | '-release:17'

17 | true | '2.13.9' | '-release:8'
17 | true | '2.13.10' | '-release:8'
17 | true | '3.2.1' | '-release:8'
17 | true | '2.13.9' | '-target:8'
17 | true | '2.13.10' | '-target:8'
17 | true | '3.2.1' | '-Xtarget:8'

toolchain = fallbackToolchain ? "$javaToolchain (fallback)" : javaToolchain
}
Expand Down

0 comments on commit 46ba8d1

Please sign in to comment.