-
Notifications
You must be signed in to change notification settings - Fork 27
[Issue-#89] Fix call of main methods (refer to the actual method owner not just the method) #142
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
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,8 +89,11 @@ object Macros { | |
val argSigs = Expr.ofList(argSigsExprs) | ||
|
||
val invokeRaw: Expr[(B, Seq[Any]) => T] = { | ||
def callOf(args: Expr[Seq[Any]]) = call(method, '{ Seq( ${ args }) }).asExprOf[T] | ||
'{ ((b: B, params: Seq[Any]) => ${ callOf('{ params }) }) } | ||
|
||
def callOf(methodOwner: Expr[Any], args: Expr[Seq[Any]]) = | ||
call(methodOwner, method, '{ Seq($args) }).asExprOf[T] | ||
|
||
'{ (b: B, params: Seq[Any]) => ${ callOf('b, 'params) } } | ||
} | ||
'{ MainData.create[T, B](${ Expr(method.name) }, ${ mainAnnotation.asExprOf[mainargs.main] }, ${ argSigs }, ${ invokeRaw }) } | ||
} | ||
|
@@ -115,8 +118,9 @@ object Macros { | |
* | ||
*/ | ||
private def call(using Quotes)( | ||
method: quotes.reflect.Symbol, | ||
argss: Expr[Seq[Seq[Any]]] | ||
methodOwner: Expr[Any], | ||
method: quotes.reflect.Symbol, | ||
argss: Expr[Seq[Seq[Any]]] | ||
): Expr[_] = { | ||
// Copy pasted from Cask. | ||
// https://github.com/com-lihaoyi/cask/blob/65b9c8e4fd528feb71575f6e5ef7b5e2e16abbd9/cask/src-3/cask/router/Macros.scala#L106 | ||
|
@@ -127,8 +131,6 @@ object Macros { | |
report.throwError("At least one parameter list must be declared.", method.pos.get) | ||
} | ||
|
||
val fct = Ref(method) | ||
|
||
val accesses: List[List[Term]] = for (i <- paramss.indices.toList) yield { | ||
for (j <- paramss(i).indices.toList) yield { | ||
val tpe = paramss(i)(j).tree.asInstanceOf[ValDef].tpt.tpe | ||
|
@@ -137,12 +139,9 @@ object Macros { | |
} | ||
} | ||
|
||
val base = Apply(fct, accesses.head) | ||
val application: Apply = accesses.tail.foldLeft(base)((lhs, args) => Apply(lhs, args)) | ||
val expr = application.asExpr | ||
expr | ||
methodOwner.asTerm.select(method).appliedToArgss(accesses).asExpr | ||
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. we first select the method using its |
||
} | ||
|
||
|
||
/** Lookup default values for a method's parameters. */ | ||
private def getDefaultParams(using Quotes)(method: quotes.reflect.Symbol): Map[quotes.reflect.Symbol, Expr[Any]] = { | ||
|
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,26 @@ | ||
package mainargs | ||
import utest._ | ||
|
||
trait CommandList { | ||
@main | ||
def list(@arg v: String): String = v | ||
} | ||
|
||
trait CommandCopy { | ||
@main | ||
def copy(@arg from: String, @arg to: String): (String, String) = (from, to) | ||
} | ||
|
||
object Joined extends CommandCopy with CommandList { | ||
@main | ||
def test(@arg from: String, @arg to: String): (String, String) = (from, to) | ||
} | ||
|
||
object MultiTraitTests extends TestSuite { | ||
val check = new Checker(ParserForMethods(Joined), allowPositional = true) | ||
val tests = Tests { | ||
test - check(List("copy", "fromArg", "toArg"), Result.Success(("fromArg", "toArg"))) | ||
test - check(List("test", "fromArg", "toArg"), Result.Success(("fromArg", "toArg"))) | ||
test - check(List("list", "vArg"), Result.Success("vArg")) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
methodOwner
is the actual instance of an object that owns the main methods to call