Repository navigation
Unified: Support implicit closure parameters #23181
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -436,8 +436,18 @@ module Unified { | |
|
|
||
| /** A function expression. */ | ||
| class FunctionExpr extends G::FunctionExpr { | ||
| /** Gets the number of parameters of this function. */ | ||
| int getNumberOfParameters() { result = count(this.getAParameter()) } | ||
| /** Gets the number of implicit parameters, if any. */ | ||
| int getNumberOfImplicitParameters() { | ||
|
Contributor
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. Having no result is surprising behavior for a predicate with this name. Could we just make this return 0 when there are no implicit parameters? The existing use-cases should work fine because the range expressions become Could you also expand the qldoc a bit to mention what "implicit parameters" are? I worry some might expect this number to include the receiver parameter as well, which it doesn't. |
||
| result = max(int n | exists(getImplicitFunctionExprParamName(this, n)) | n) + 1 | ||
| } | ||
|
|
||
| /** Gets the number of explicit or implicit parameters of this function. */ | ||
| int getNumberOfParameters() { | ||
| result = this.getNumberOfImplicitParameters() | ||
| or | ||
| not exists(this.getNumberOfImplicitParameters()) and | ||
| result = count(this.getAParameter()) | ||
| } | ||
| } | ||
|
|
||
| class ArrayLiteral extends G::ArrayLiteral { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ private newtype TParameterEx = | |
| TImplicitReceiverParameter(Callable c) { | ||
| exists(LocalVariable v | v.isImplicitReceiverParameter(c)) | ||
| } or | ||
| TImplicitFunctionExprParameter(FunctionExpr fe, int i) { | ||
| i = [0 .. fe.getNumberOfImplicitParameters() - 1] | ||
|
Contributor
Author
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. I am not too worried. But if a human echoes this we can make a change here.
Contributor
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. I'm fine with ignoring this |
||
| } or | ||
| TDefaultConstructorParameter(ConstructorDeclaration cd, int i, string name, boolean hasDefault) { | ||
| cd.hasModifier("generated") and | ||
| defaultConstructorParameter(cd, i, name, hasDefault) | ||
|
|
@@ -37,6 +40,18 @@ abstract private class ParameterExImpl extends TParameterEx { | |
| */ | ||
| predicate isImplicitReceiverParameter(Callable c) { this = TImplicitReceiverParameter(c) } | ||
|
|
||
| /** Holds if this entity represents implicit parameter `i` of `fe`. */ | ||
| predicate isImplicitFunctionExprParameter(FunctionExpr fe, int i) { | ||
| this = TImplicitFunctionExprParameter(fe, i) | ||
| } | ||
|
|
||
| /** Holds if this entity represents parameter `i` of `fe`, explicit or implicit. */ | ||
| predicate isFunctionExprParameter(FunctionExpr fe, int i) { | ||
| this.asParameter() = fe.getParameter(i) | ||
| or | ||
| this.isImplicitFunctionExprParameter(fe, i) | ||
| } | ||
|
|
||
| /** | ||
| * Holds if this entity represents the `i`th parameter of the default | ||
| * constructor `cd` with name `name`. | ||
|
|
@@ -57,6 +72,8 @@ abstract private class ParameterExImpl extends TParameterEx { | |
| or | ||
| this.isImplicitReceiverParameter(result) | ||
| or | ||
| this.isImplicitFunctionExprParameter(result, _) | ||
| or | ||
| this.isDefaultConstructorParameter(result, _, _) | ||
| } | ||
|
|
||
|
|
@@ -113,6 +130,29 @@ private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitRece | |
| override Location getLocation() { result = c.getLocation() } | ||
| } | ||
|
|
||
| /** | ||
| * An implicit parameter of a function expression inferred from usage of | ||
| * implicit parameters inside its body. | ||
| */ | ||
| private class ImplicitFunctionExprParameterEx extends ParameterExImpl, | ||
| TImplicitFunctionExprParameter | ||
| { | ||
| FunctionExpr fe; | ||
| int i; | ||
|
|
||
| ImplicitFunctionExprParameterEx() { this = TImplicitFunctionExprParameter(fe, i) } | ||
|
|
||
| override string getExternalName() { none() } | ||
|
|
||
| override predicate hasDefault() { none() } | ||
|
|
||
| override AstNode getType() { none() } | ||
|
|
||
| override string toString() { result = "implicit parameter " + i } | ||
|
|
||
| override Location getLocation() { result = fe.getLocation() } | ||
| } | ||
|
|
||
| private class DefaultConstructorParameterEx extends ParameterExImpl, TDefaultConstructorParameter { | ||
| ConstructorDeclaration cd; | ||
| int i; | ||
|
|
||
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.
Could we add a type inference test case where the same implicit parameter is used multiple times? I'm not sure exactly what kind of bug might emerge from this, but it seems worth covering given that the associated "inference node" is not unique anymore.