Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enable returning classes from MacroAnnotations (part 3) #16534

Merged
merged 13 commits into from
Jan 12, 2023

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Dec 15, 2022

Enable the addition of classes from a MacroAnnotation:

  • Can add new class/object definitions next to the annotated definition

Special cases:

  • An annotated top-level def, val, var, lazy val can return a class/object
    definition that is owned by the package or package object.

Related PRs:

@nicolasstucki nicolasstucki added this to the 3.3.0-RC1 milestone Dec 15, 2022
@nicolasstucki nicolasstucki force-pushed the macro-annotations-add-class branch from 1f0c08f to f6d8019 Compare December 15, 2022 10:57
@michelou

This comment was marked as resolved.

@nicolasstucki nicolasstucki force-pushed the macro-annotations-add-class branch from f6d8019 to fdb14f2 Compare December 15, 2022 14:33
@nicolasstucki nicolasstucki force-pushed the macro-annotations-add-class branch 5 times, most recently from e488e10 to 0576d72 Compare December 16, 2022 13:43
nicolasstucki added a commit that referenced this pull request Dec 19, 2022
Enable modification of classes with `MacroAnnotation`:
 * Can annotate `class` to transform it
 * Can annotate `object` to transform the companion class

Supported class modifications:
* Modify the implementations of `def`, `val`, `var`, `lazy val`,
`class`, `object` in the class
* Add new `def`, `val`, `var`, `lazy val`, `class`, `object` members to
the class
* Add a new override for a `def`, `val`, `var`, `lazy val` members in
the class

Restrictions:
* An annotation on a top-level class cannot return a top-level `def`,
`val`, `var`, `lazy val`.


Related PRs:
 * Includes #16513
 * Follows #16392
 * Followed by #16534
Enable the addition of classes from a `MacroAnnotation`:
 * Can add new `class` definitions next to the annotated definition

Special cases:
 * An annotated top-level `def`, `val`, `var`, `lazy val` can return a `class`
   definition that is owned by the package or package object.

Related PRs:
 * Follows scala#16454
@nicolasstucki nicolasstucki force-pushed the macro-annotations-add-class branch from 0576d72 to 5fcb869 Compare December 20, 2022 08:06
@nicolasstucki nicolasstucki force-pushed the macro-annotations-add-class branch from 59f3aa3 to 079a786 Compare December 20, 2022 08:58
@nicolasstucki nicolasstucki marked this pull request as ready for review December 20, 2022 10:33
@experimental def newClass(parent: Symbol, name: String, parents: List[TypeRepr], decls: Symbol => List[Symbol], selfType: Option[TypeRepr]): Symbol

/** Generates a new module symbol with an associated module class symbol.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Generates a new module symbol with an associated module class symbol.
/** Generates a new module symbol with an associated module class symbol,
* this is equivalent to an `object` declaration in source code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this change was not applied.

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala Outdated Show resolved Hide resolved
compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala Outdated Show resolved Hide resolved
library/src/scala/quoted/Quotes.scala Outdated Show resolved Hide resolved
library/src/scala/quoted/Quotes.scala Outdated Show resolved Hide resolved
library/src/scala/quoted/Quotes.scala Outdated Show resolved Hide resolved
library/src/scala/quoted/Quotes.scala Show resolved Hide resolved
compiler/src/dotty/tools/dotc/transform/Inlining.scala Outdated Show resolved Hide resolved
/** List of top level classes added by macro annotation in a package object.
* These are added the PackageDef that owns this particular package object.
*/
private val topClasses = new collection.mutable.ListBuffer[Tree]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I think this isn't sufficient because package objects can be nested:

package foo {
  val x = 1
  package bar {
    val y = 2
  }
}

Instead, maybe the MemberDef case of transform should return a Thicket with the top-level classes, and we should add an extra case to transform to handle the package object module class itself, where we should also return a Thicket with the top-level classes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use case was considered and works. I added tests for it in tests/run-macros/annot-add-global-class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that after after post typer the tree is

package foo {
  package bar {
    val y = 2
  }
  val x = 1
}

This implies that nested classes are processed first and the buffer never overlaps and is emptied just after transforming the nested package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implies that nested classes are processed first and the buffer never overlaps and is emptied just after transforming the nested package.

This is subtle, so this precondition should be documented in the code (and ideally checked somewhere, in case it breaks)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found cases where this precondition does not hold. I updated the implementation to handle such cases.

@smarter smarter assigned nicolasstucki and unassigned smarter Jan 3, 2023
@nicolasstucki nicolasstucki force-pushed the macro-annotations-add-class branch from dfe1c14 to 89c550b Compare January 5, 2023 09:18
@nicolasstucki nicolasstucki force-pushed the macro-annotations-add-class branch from 2d3c60f to 1acd745 Compare January 5, 2023 12:56
@nicolasstucki nicolasstucki requested a review from smarter January 9, 2023 10:35
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM.

@smarter smarter assigned nicolasstucki and unassigned smarter Jan 12, 2023
@nicolasstucki nicolasstucki requested a review from smarter January 12, 2023 14:42
@smarter smarter enabled auto-merge January 12, 2023 17:56
@smarter smarter merged commit be10bc6 into scala:main Jan 12, 2023
@smarter smarter deleted the macro-annotations-add-class branch January 12, 2023 19:48
@Kordyjan Kordyjan modified the milestones: 3.3.0-RC1, 3.3.0 Aug 1, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants