Skip to content

Commit

Permalink
Fix false-positive compiler warnings in macro-generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
cb372 committed Mar 12, 2020
1 parent 5342473 commit e7d785d
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,34 @@ object serviceImpl {
..$nonRpcDefs
}""".supressWarts("DefaultArguments")

/*
* When you write an anonymous parameter in an anonymous function that
* ignores the parameter, e.g. `List(1, 2, 3).map(_ => "hello")` the
* -Wunused:params scalac flag does not warn you about it. That's
* because the compiler attaches a `NoWarnAttachment` to the tree for
* the parameter.
*
* But if you write the same thing in a quasiquote inside a macro, the
* attachment does not get added, so you get false-positive compiler
* warnings at the macro use site like: "parameter value x$2 in anonymous
* function is never used".
*
* (The parameter needs a name, even though the function doesn't
* reference it, so `_` gets turned into a fresh name e.g. `x$2`. The
* same thing happens even if you're not in a macro.)
*
* I'd say this is a bug in Scala. We work around it by manually adding
* the attachment.
*/
private def anonymousParam: ValDef = {
val tree: ValDef = q"{(_) => 1}".vparams.head
c.universe.internal.updateAttachment(
tree,
c.universe.asInstanceOf[scala.reflect.internal.StdAttachments].NoWarnAttachment
)
tree
}

val client: DefDef =
q"""
def client[$F_](
Expand All @@ -296,7 +324,7 @@ object serviceImpl {
_root_.cats.effect.Resource.make {
new _root_.higherkindness.mu.rpc.channel.ManagedChannelInterpreter[$F](channelFor, channelConfigList).build
}(channel => CE.void(CE.delay(channel.shutdown()))).flatMap(ch =>
_root_.cats.effect.Resource.make[F, $serviceName[$F]](CE.delay(new $Client[$F](ch, options)))(_ => CE.unit))
_root_.cats.effect.Resource.make[F, $serviceName[$F]](CE.delay(new $Client[$F](ch, options)))($anonymousParam=> CE.unit))
""".supressWarts("DefaultArguments")

val clientFromChannel: DefDef =
Expand All @@ -306,7 +334,7 @@ object serviceImpl {
options: _root_.io.grpc.CallOptions = _root_.io.grpc.CallOptions.DEFAULT
)(implicit ..$classImplicits): _root_.cats.effect.Resource[$F, $serviceName[$F]] = _root_.cats.effect.Resource.make(channel)(channel =>
CE.void(CE.delay(channel.shutdown()))).flatMap(ch =>
_root_.cats.effect.Resource.make[$F, $serviceName[$F]](CE.delay(new $Client[$F](ch, options)))(_ => CE.unit))
_root_.cats.effect.Resource.make[$F, $serviceName[$F]](CE.delay(new $Client[$F](ch, options)))($anonymousParam => CE.unit))
""".supressWarts("DefaultArguments")

val unsafeClient: DefDef =
Expand Down

0 comments on commit e7d785d

Please sign in to comment.