Better diagnostics for macro errors?

Here’s a very simple code example:

I think we can all agree that the error message is very confusing and unhelpful.
What’s happening is that the implicit search finds a macro inherited from SchemaCompanionMacros, calls it, and it fails with a completely bogus error message. Needless to say, the codebase I encountered this error in wasn’t quite as nice and tidy as my example above, and I spent a considerable amount of time pulling my own hair trying to find the cause.

I think we can do better. At the very least, I would like to know what macro was being called that led to this error. And ideally I would like to see the complete expression that the compiler generates during implicit search.

2 Likes

I don’t always click thru to the Scastie to see the error message, but when I do, I hope it says:

java.lang.String is not a subtype of scala.Predef.String

Looking through this a bit. Seems to be a faulty library. Nothing to do with the compiler. Printing trees after the typer produces this.

package sttp.tapir {
  import sttp.tapir.macros.SchemaCompanionMacros
  import sttp.tapir.Schema
  final lazy module val A: sttp.tapir.A = new sttp.tapir.A()
  final module class A() extends Object(), sttp.tapir.macros.
    SchemaCompanionMacros { this: sttp.tapir.A.type =>
    {
      val x$proxy1: sttp.tapir.Schema[Set[String]] =
        sttp.tapir.Schema.schemaForSet[String, Set](
          sttp.tapir.A.derivedStringBasedUnionEnumeration[String](
            sttp.tapir.macros.IsUnionOf.inline$IsUnionOf.inline$singleton.
              asInstanceOf[sttp.tapir.macros.IsUnionOf[String, String]]
          )
        )
      x$proxy1
    }
    val a: "foo" = "bar"
  }
}

My guess is that the error happens in derivedStringBasedUnionEnumeration, which eventually calls this function.

This code simply fails compilation if it is not given a union type.

It very much is a compiler issue that the compiler doesn’t tell me which macro produced the error and why it’s even being called.

And actually I have spelled out quite clearly that it’s a macro that generated this error. Please read more carefully next time before responding, thanks.

Please read more than the error message next time before trying to make a joke, thanks.

3 Likes