Skip to content

Use tree checker in macros #16570

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 4 commits into from
Jan 16, 2023
Merged

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Dec 21, 2022

Trees are only checked if -Xcheck-macros is enabled.

Fixes:

@nicolasstucki nicolasstucki force-pushed the use-tree-checker-in-macros branch from bca7cc4 to b3b7889 Compare December 22, 2022 07:56
@nicolasstucki nicolasstucki force-pushed the use-tree-checker-in-macros branch 3 times, most recently from 2ef4a2d to d9a1b24 Compare December 22, 2022 10:07
@nicolasstucki nicolasstucki self-assigned this Dec 22, 2022
@nicolasstucki nicolasstucki force-pushed the use-tree-checker-in-macros branch 3 times, most recently from 3c7011b to c50ef24 Compare December 22, 2022 16:29
@nicolasstucki nicolasstucki marked this pull request as ready for review December 23, 2022 09:12
@nicolasstucki
Copy link
Contributor Author

We should test this on the extended community build before merging.

@nicolasstucki nicolasstucki removed their assignment Dec 23, 2022
@nicolasstucki
Copy link
Contributor Author

if ctx.settings.XcheckMacros.value then
val checkingCtx = ctx
.fresh
.addMode(Mode.ImplicitsEnabled)
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to generate implicits when checking trees?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

val implicitTree = ctx.typer.inferImplicitArg(tpe, Position.ofMacroExpansion.span)
// Make sure that we do not have any uninstantiated type variables.
// See tests/pos-macros/exprSummonWithTypeVar with -Xcheck-macros.
implicitTree.foreachSubTree(tree => dotc.typer.Inferencing.fullyDefinedType(tree.tpe, "", tree))
Copy link
Member

Choose a reason for hiding this comment

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

foreachSubTree is expensive. You could use typer.interpolateTypeVars instead (probably with pt = WildcardType and locked = ctx.typerState.ownedVars).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

interpolateTypeVars did not work.

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 there a way to make inferImplicitArg resolve all type variables that it inserts?

Copy link
Member

Choose a reason for hiding this comment

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

Try defining val locked = ctx.typerState.ownedVars before the call to inferImplicitArg, so that variables created by it can be instantiated when you pass locked to interpolateTypeVars

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It did not work either. Note that the ownedVars of this context are empty before and after the call to inferImplicitArg. The type variable in introduced within inferImplicitArg.

@smarter smarter assigned nicolasstucki and unassigned smarter Jan 3, 2023
@nicolasstucki nicolasstucki requested a review from smarter January 9, 2023 12:38
@nicolasstucki nicolasstucki force-pushed the use-tree-checker-in-macros branch from 4fe1657 to 9cea198 Compare January 9, 2023 14:48
The issue was that an uninstantiated type variable escaped the implicit
search and was then affected by the quote pattern match.

Fixes scala#15779
Fixes scala#16636
Trees are only checked if -Xcheck-macros is enabled.

Fixes:
 - Add missing positions to {ValDef,Bind}.apply
 - Inline by-name ascribed param
 - Buggy test (tests/neg-macros/annot-mod-top-method-add-top-method)
@nicolasstucki nicolasstucki force-pushed the use-tree-checker-in-macros branch from be3ce46 to aaf86b3 Compare January 16, 2023 09:09
@@ -9,5 +9,5 @@ class addTopLevelMethodOutsidePackageObject extends MacroAnnotation:
import quotes.reflect._
val methType = MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Int])
val methSym = Symbol.newMethod(Symbol.spliceOwner.owner, Symbol.freshName("toLevelMethod"), methType, Flags.EmptyFlags, Symbol.noSymbol)
val methDef = ValDef(methSym, Some(Literal(IntConstant(1))))
val methDef = DefDef(methSym, _ => Some(Literal(IntConstant(1))))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

After rebasing, the check caught this unintended bug in this neg test.

@smarter smarter merged commit 1f7b03c into scala:main Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants