-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d5840fc
Fix #6150: Emit error when calling Scala 2 macro
nicolasstucki 70de4ec
Intrinsify f interpolator
nicolasstucki 48b66ef
Avoid failing with macros in the community build
nicolasstucki 89070ae
Fix implementation of f interpolation macro
nicolasstucki c9c53b8
Fix #5971: Update Scala 2 macros doc
nicolasstucki c5c5f69
Fix Scala 2 Macro check
nicolasstucki 4b8f227
Add a complete description of macros
nicolasstucki 89412a0
Mode internal StringContext.f implementation to dotty package
nicolasstucki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
--- | ||
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 | ||
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 | ||
The previous, experimental macro system has been dropped. Instead, there is a cleaner, more restricted system based on two complementary concepts: `inline` and `'{ ... }`/`${ ... }` code generation. | ||
`'{ ... }` delays the compilation of the code and produces an object containing the code, dually `${ ... }` evaluates an expression which produces code and inserts it in the surrounding `${ ... }`. | ||
In this setting, a definition marked as inlined containing a `${ ... }` is a macro, the code inside the `${ ... }` is executed at compile-time and produces code in the form of `'{ ... }`. | ||
Additionally, the contents of code can be inspected and created with a more complex reflection API (TASTy Reflect) as an extension of `'{ ... }`/`${ ... }` framework. | ||
|
||
* `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. |
22 changes: 22 additions & 0 deletions
22
library/src-bootstrapped/dotty/internal/StringContext.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dotty.internal | ||
|
||
import scala.quoted._ | ||
|
||
object StringContext { | ||
|
||
/** Implemetation of scala.StringContext.f used in Dotty while the standard library is still not bootstrapped */ | ||
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: _*) | ||
} | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
library/src-non-bootstrapped/dotty/internal/StringContext.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dotty.internal | ||
|
||
object StringContext { | ||
|
||
@forceInline def f(sc: => scala.StringContext)(args: Any*): String = | ||
throw new Exception("non-boostrapped library") | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
abc | ||
Hello world! | ||
Hello world! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}!") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.