-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #6150: Emit error when calling Scala 2 macro #6179
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
Changes from 6 commits
d5840fc
70de4ec
48b66ef
89070ae
c9c53b8
c5c5f69
4b8f227
89412a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
--- | ||
layout: doc-page | ||
title: Dropped: Macros | ||
title: Dropped: Scala 2 Macros | ||
--- | ||
|
||
The previous, experimental macro system has been dropped. Instead, | ||
there is a cleaner, more restricted system based on two complementary | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't talk about "two complementary concepts" when the list below has three entries :). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified text. Added more detail on how the parts interact without going in depth. |
||
concepts: `inline` and `meta`. | ||
|
||
`inline` has been [implemented](../other-new-features/inline.md) in Dotty. | ||
|
||
`meta` is worked on in the separate [Scalameta](http://scalameta.org) project | ||
concepts: `inline` and `'{ ... }`/`${ ... }` code generation. | ||
|
||
* `inline` has been [implemented](../other-new-features/inline.md) in Dotty. | ||
* Quotes `'{ ... }` and splices `${ ... }` has been [implemented](../other-new-features/principled-meta-programming.md) in Dotty. | ||
* [TASTy reflect](../other-new-features/tasty-reflect.md) provides more complex tree based APIs to inspect or create quoted code. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package scala.internal | ||
nicolasstucki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import scala.quoted._ | ||
|
||
object StringContext { | ||
|
||
inline def f(sc: => scala.StringContext)(args: Any*): String = ${ fImpl('sc, 'args) } | ||
|
||
private def fImpl(sc: Expr[StringContext], args: Expr[Seq[Any]]): Expr[String] = { | ||
// TODO implement f interpolation checks and generate optimal code | ||
// See https://github.com/alemannosara/f-interpolators-in-Dotty-macros | ||
'{ | ||
// Purely runtime implementation of the f interpolation without any checks | ||
val parts = $sc.parts.toList | ||
assert(parts.nonEmpty, "StringContext should have non empty parts") | ||
val parts2 = parts.head :: parts.tail.map(x => if (x.startsWith("%s")) x else "%s" + x) | ||
parts2.mkString.format($args: _*) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package scala.internal | ||
|
||
object StringContext { | ||
|
||
@forceInline def f(sc: => scala.StringContext)(args: Any*): String = | ||
throw new Exception("non-boostrapped library") | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<56..56> in override-scala2-macro.scala | ||
error overriding method f in class StringContext of type [A >: Any](args: Seq[A]): String; | ||
method f of type [A >: Any](args: Seq[A]): String cannot be used here - only Scala-2 macros can override Scala-2 macros |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Foo extends StringContext { | ||
override inline def f[A >: Any](args: A*): String = ??? // error | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
abc | ||
Hello world! | ||
Hello world! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
println(f"abc") | ||
println(f"Hello ${"world"}%s!") | ||
println(f"Hello ${"world"}!") | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.