-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Enter missing symbols generated by the MacroAnnotation expansion #18826
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
error overriding method toString in class Foo of type (): String; | ||
method toString of type (): String cannot override final member method toString in class Foo |
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,19 @@ | ||
import scala.annotation.experimental | ||
import scala.annotation.MacroAnnotation | ||
import scala.quoted.* | ||
|
||
@experimental | ||
class toString extends MacroAnnotation : | ||
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = | ||
import quotes.reflect.* | ||
tree match | ||
case ClassDef(name, ctr, parents, self, body) => | ||
val cls = tree.symbol | ||
val toStringSym = Symbol.requiredMethod("java.lang.Object.toString") | ||
val toStringOverrideSym = Symbol.newMethod(cls, "toString", toStringSym.info, Flags.Override, Symbol.noSymbol) | ||
val toStringDef = DefDef(toStringOverrideSym, _ => Some(Literal(StringConstant("Hello from macro")))) | ||
val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, toStringDef :: body) | ||
List(newClassDef) | ||
case _ => | ||
report.error("@toString can only be annotated on class definitions") | ||
tree :: Nil |
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,15 @@ | ||
// nopos-error | ||
|
||
import annotation.experimental | ||
|
||
class Foo : | ||
final override def toString(): String = "Hello" | ||
|
||
@experimental | ||
@toString | ||
class AFoo extends Foo //: | ||
//override def toString(): String = "Hello from macro" | ||
|
||
@experimental | ||
@main def run = | ||
println(new AFoo().toString) |
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,23 @@ | ||
|
||
-- Error: tests/neg-macros/wrong-owner/Test_2.scala:5:6 ---------------------------------------------------------------- | ||
3 |@experimental | ||
4 |@wrongOwner | ||
5 |class Foo // error | ||
|^ | ||
|Malformed tree was found while expanding macro with -Xcheck-macros. | ||
| |The tree does not conform to the compiler's tree invariants. | ||
| | | ||
| |Macro was: | ||
| |@scala.annotation.internal.SourceFile("tests/neg-macros/wrong-owner/Test_2.scala") @wrongOwner @scala.annotation.experimental class Foo() | ||
| | | ||
| |The macro returned: | ||
| |@scala.annotation.internal.SourceFile("tests/neg-macros/wrong-owner/Test_2.scala") @wrongOwner @scala.annotation.experimental class Foo() { | ||
| override def toString(): java.lang.String = "Hello from macro" | ||
|} | ||
| | | ||
| |Error: | ||
| |assertion failed: bad owner; method toString has owner class String, expected was class Foo | ||
|owner chain = method toString, class String, package java.lang, package java, package <root>, ctxOwners = class Foo, class Foo, package <empty>, package <empty>, package <empty>, package <root>, package <root>, package <root>, package <root>, package <root>, package <root>, <none>, <none>, <none>, <none>, <none> | ||
| | | ||
|stacktrace available when compiling with `-Ydebug` | ||
| | |
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,19 @@ | ||
import scala.annotation.experimental | ||
import scala.annotation.MacroAnnotation | ||
import scala.quoted.* | ||
|
||
@experimental | ||
class wrongOwner extends MacroAnnotation : | ||
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = | ||
import quotes.reflect.* | ||
tree match | ||
case ClassDef(name, ctr, parents, self, body) => | ||
val cls = tree.symbol | ||
val toStringSym = Symbol.requiredMethod("java.lang.Object.toString") | ||
val toStringOverrideSym = Symbol.newMethod(Symbol.classSymbol("java.lang.String"), "toString", toStringSym.info, Flags.Override, Symbol.noSymbol) | ||
val toStringDef = DefDef(toStringOverrideSym, _ => Some(Literal(StringConstant("Hello from macro")))) | ||
val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, toStringDef :: body) | ||
List(newClassDef) | ||
case _ => | ||
report.error("@toString can only be annotated on class definitions") | ||
tree :: Nil |
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,5 @@ | ||
import scala.annotation.experimental | ||
|
||
@experimental | ||
@wrongOwner | ||
class Foo // 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 @@ | ||
hi |
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,24 @@ | ||
import scala.annotation.{experimental, MacroAnnotation} | ||
import scala.quoted._ | ||
|
||
@experimental | ||
class gen1 extends MacroAnnotation: | ||
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = | ||
import quotes.reflect._ | ||
tree match | ||
case ClassDef(name, ctr, parents, self, body) => | ||
val cls = tree.symbol | ||
// val meth = cls.methodMember("foo").head | ||
// val fooTpe = cls.typeRef.memberType(meth) | ||
|
||
val overrideTpe = MethodType(Nil)(_ => Nil, _ => defn.StringClass.typeRef) | ||
|
||
val fooOverrideSym = Symbol.newMethod(cls, "foo", overrideTpe, Flags.Override, Symbol.noSymbol) | ||
|
||
val fooDef = DefDef(fooOverrideSym, _ => Some(Literal(StringConstant("hi")))) | ||
|
||
val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, fooDef :: body) | ||
List(newClassDef) | ||
case _ => | ||
report.error("Annotation only supports `class`") | ||
List(tree) |
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,14 @@ | ||
import scala.annotation.experimental | ||
|
||
class Base: | ||
def foo(): Object = ??? | ||
|
||
@experimental | ||
@gen1 | ||
class Sub extends Base | ||
// > override def foo(): String = "hi" | ||
|
||
@experimental | ||
@main def Test(): Unit = | ||
val sub = new Sub | ||
println(sub.foo()) |
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
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.