From 668c8af2ecbbf6b4b85f3d8389b20552cce79aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Bia=C5=82y?= Date: Thu, 18 Apr 2024 09:05:20 +0200 Subject: [PATCH] fix issue 462 - pulumi string interpolation inference macro threw MatchError on dynamic Output[String] --- .../scala/besom/util/NonEmptyString.scala | 4 +++ .../scala/besom/util/CompileAssertions.scala | 4 +-- .../scala/besom/util/NonEmptyStringTest.scala | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/besom/util/NonEmptyString.scala b/core/src/main/scala/besom/util/NonEmptyString.scala index 11940e6a..dd906ded 100644 --- a/core/src/main/scala/besom/util/NonEmptyString.scala +++ b/core/src/main/scala/besom/util/NonEmptyString.scala @@ -51,6 +51,10 @@ object NonEmptyString: handleParts(parts) case '{ scala.StringContext.apply(${ Varargs(parts) }: _*).pulumi(${ Varargs(_) }: _*)(using ${ xd }: Context) } => handleParts(parts) + case _ => + report.errorAndAbort( + "This is an Output of a dynamic String which can't be inferred to be a NonEmptyString! Use `.flatMap(_.toNonEmptyOutput)` instead." + ) end fromStringOutputImpl diff --git a/core/src/test/scala/besom/util/CompileAssertions.scala b/core/src/test/scala/besom/util/CompileAssertions.scala index 093a03d0..7ce1f9b7 100644 --- a/core/src/test/scala/besom/util/CompileAssertions.scala +++ b/core/src/test/scala/besom/util/CompileAssertions.scala @@ -10,13 +10,13 @@ trait CompileAssertions: private val NL = System.lineSeparator() - inline def failsToCompile(inline code: String): Unit = + transparent inline def failsToCompile(inline code: String): Unit = assert( !scala.compiletime.testing.typeChecks(code), s"Code compiled correctly when expecting type errors:$NL$code" ) - inline def compiles(inline code: String): Unit = + transparent inline def compiles(inline code: String): Unit = val errors = scala.compiletime.testing.typeCheckErrors(code) if errors.nonEmpty then val errorMessages = errors.map(_.message).mkString(NL) diff --git a/core/src/test/scala/besom/util/NonEmptyStringTest.scala b/core/src/test/scala/besom/util/NonEmptyStringTest.scala index 966bfa76..3c35e260 100644 --- a/core/src/test/scala/besom/util/NonEmptyStringTest.scala +++ b/core/src/test/scala/besom/util/NonEmptyStringTest.scala @@ -360,4 +360,31 @@ class NonEmptyStringTest extends munit.FunSuite with CompileAssertions: case None => () case Some(_) => fail("apply doesn't work") } + + test("issue 462") { + val errors = scala.compiletime.testing.typeCheckErrors(""" + import besom.* + given Context = ??? + + def requireString(key: NonEmptyString)(using Context): Output[String] = ??? + + val s: Output[NonEmptyString] = requireString("stuff") + """) + + assert(errors.size == 1) + assert( + errors.head.message.contains( + "This is an Output of a dynamic String which can't be inferred to be a NonEmptyString! Use `.flatMap(_.toNonEmptyOutput)` instead." + ) + ) + + compiles(""" + import besom.* + given Context = ??? + + def requireString(key: NonEmptyString)(using Context): Output[String] = ??? + + val s: Output[NonEmptyString] = requireString("stuff").flatMap(_.toNonEmptyOutput) + """) + } end NonEmptyStringTest