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

Varargs compilation error as of v3.9.5 #354

Open
kittsville opened this issue May 23, 2022 · 3 comments · May be fixed by #433
Open

Varargs compilation error as of v3.9.5 #354

kittsville opened this issue May 23, 2022 · 3 comments · May be fixed by #433
Assignees
Labels

Comments

@kittsville
Copy link

The new version v3.9.5 no longer accepts the varargs logging methods being wrapped, so a class like such will fail to compile:

class LogWrapper (val underlying: Logger) {
  // Imagine some DataDog/Bugsnag/analytics/etc. magic here that'd justify wrapping the logger

  def info(message: String, args: AnyRef*): Unit = underlying.info(message, args: _*)
}

I've create a minimal replication repo, based on the SBT hello world template, to demonstrate how the code no longer compiles in v3.9.5.

@kittsville kittsville changed the title Varargs compilation as of v3.9.5 Varargs compilation error as of v3.9.5 May 23, 2022
@jxnu-liguobin
Copy link
Collaborator

Seems similar to #191. Starting from 3.6.0.

When I remove the format, the compilation can pass, and the log display is normal.

  def infoMessageArgs(c: LoggerContext)(message: c.Expr[String], args: c.Expr[Any]*): c.universe.Tree = {
    import c.universe._
    val underlying = q"${c.prefix}.underlying"
    //    val anyRefArgs = formatArgs(c)(args: _*)
    if (args.length == 2)
      q"if ($underlying.isInfoEnabled) $underlying.info($message, _root_.scala.Array[AnyRef](${args.head}, ${args(1)}): _*)"
    else
      q"if ($underlying.isInfoEnabled) $underlying.info($message, ..$args)"
  }

Output

10:15:31.981 [main] INFO example.Hello$ - Hello World

@SakulK
Copy link

SakulK commented Feb 28, 2024

FYI in case someone else also runs into this, when using Scala 3 and varargs with :_* (or just * using Scala 3 syntax) the code compiles with scala-logging 3.9.4 and 3.9.5, but doesn't actually pass the values to the underlying logger. You can check following script with scala-cli:

//> using dep "com.typesafe.scala-logging::scala-logging:3.9.4"
//> using dep "ch.qos.logback:logback-classic:1.5.1"

import com.typesafe.scalalogging.LazyLogging

object Test extends LazyLogging {
  def log(): Unit = {
    val args = Seq("1", "2")
    logger.info("test {} {}", args:_*)
  }
}
Test.log()

when running with --scala-version 2.13.12 it will print "test 1 2" and with --scala-version 3.3.1 just "test {} {}". With scala-logging 3.9.5 it won't compile on 2.13 at all

I think the issue for Scala 3 is in this part of the macro:
https://github.com/lightbend-labs/scala-logging/blob/main/src/main/scala-3/com/typesafe/scalalogging/LoggerMacro.scala#L279

@jxnu-liguobin
Copy link
Collaborator

jxnu-liguobin commented Feb 29, 2024

I think the issue for Scala 3 is in this part of the macro:

Yes, this is the same as my survey, there is an issue with the formatArgs method andScala 2 has similar issues..

jxnu-liguobin pushed a commit that referenced this issue Feb 29, 2024
After wrapping varargs, the user code fails to compile.

In Scala 2, there were no inline parameters, and the subtype of args was obtained during compilation. However, this approach may not always be accurate.

Regarding #191

There is an issue with obtaining inline parameters in Scala 3.

To address this, we recursively obtain the actual value of inline parameters. This necessitates the ongoing use of inlining in the parameters of the wrapper function. For instance:

```scala
class LogWrapper(val underlying: Logger) {
    inline def info(message: String, inline args: AnyRef*): Unit = underlying.info(message, args: _*)
}
```
This ensures compatibility and accurate handling of inline parameters in both Scala 2 and Scala 3.
jxnu-liguobin added a commit that referenced this issue Feb 29, 2024
After wrapping varargs, the user code fails to compile.

In Scala 2, there were no inline parameters, and the subtype of args was obtained during compilation. However, this approach may not always be accurate.

Regarding #191

There is an issue with obtaining inline parameters in Scala 3.

To address this, we recursively obtain the actual value of inline parameters. This necessitates the ongoing use of inlining in the parameters of the wrapper function. For instance:

```scala
class LogWrapper(val underlying: Logger) {
    inline def info(message: String, inline args: AnyRef*): Unit = underlying.info(message, args: _*)
}
```
This ensures compatibility and accurate handling of inline parameters in both Scala 2 and Scala 3.
@jxnu-liguobin jxnu-liguobin linked a pull request Feb 29, 2024 that will close this issue
@jxnu-liguobin jxnu-liguobin linked a pull request Feb 29, 2024 that will close this issue
@jxnu-liguobin jxnu-liguobin self-assigned this Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants