Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2697,10 +2697,21 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
module Make3<InputSig3 Input3> {
private import Input3

/**
* Gets a parameter's declaration pattern or an access to its implicit
* variable.
*/
private AstNode getAParameterInferenceNode(Parameter p) {
result = p.getPattern()
or
// Implicit parameters do not have a pattern, use an access instead.
result = any(Variable v | implicitParameterDecl(p, v)).getAnAccess()

Copy link
Copy Markdown
Contributor

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.

}

pragma[nomagic]
private predicate closureStep(AstNode pattern, TypePath prefix1, Expr c, TypePath prefix2) {
exists(Closure c0, Parameter p |
pattern = p.getPattern() and
pattern = getAParameterInferenceNode(p) and
p = c0.getParameter(_) and
prefix1.isEmpty() and
prefix2 = getClosureParameterTypePath(p) and
Expand Down Expand Up @@ -3565,7 +3576,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
hasClosureParameterPseudoType(n, prefix, p) and
hasTypeAtPrefix(n, prefix, path) and
path = prefix.appendInverse(suffix) and
pattern = p.getPattern()
pattern = getAParameterInferenceNode(p)
)
}

Expand All @@ -3580,7 +3591,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
// QL doc for `ClosureParameterPseudoType`.
exists(Closure c, Parameter p | p = c.getParameter(_) |
// step 1
n = p.getPattern() and
n = getAParameterInferenceNode(p) and
path.isEmpty() and
result.(ClosureParameterPseudoType).getParameter() = p
or
Expand Down Expand Up @@ -3614,7 +3625,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
result instanceof UnknownType
or
// step 3
n = p.getPattern() and
n = getAParameterInferenceNode(p) and
result = inferType(def, getClosureParameterTypePath(p).appendInverse(path)) and
not (path.isEmpty() and result instanceof UnknownType)
)
Expand Down
30 changes: 30 additions & 0 deletions unified/ql/lib/codeql/unified/internal/AstPlugin.qll
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class AstPlugin extends Unit {
bindingset[decl]
string getVariableDeclarationKeyword(VariableDeclaration decl) { none() }

/**
* Holds if `ident` accesses an implicit function-expression parameter at
* index `n`.
*
* For instance:
* - In Kotlin `it` accesses an implicit parameter at index 0.
* - In Swift `$1` accesses an implicit parameter at index 1.
* - In Ruby `_2` accesses an implicit parameter at index 2.
*/
predicate implicitFunctionExprParamAccess(Identifier ident, int n) { none() }

/**
* Holds if the default constructor `cd` needs a parameter at index `i` with
* the given name. `hasDefault` indicates whether the parameter has a default
Expand Down Expand Up @@ -65,6 +76,25 @@ string getVariableDeclarationKeyword(VariableDeclaration decl) {
result = any(AstPlugin p).getVariableDeclarationKeyword(decl)
}

/**
* Gets the name of the `n`th implicit parameter of function expression `fe`.
*/
string getImplicitFunctionExprParamName(FunctionExpr fe, int n) {
// For now we assume that an implicit parameter access always belongs to its
// enclosing function expression. This is correct for Swift but not in the
// following Kotlin example:
// ```kotlin
// listOf(1, 2).map { listOf(3, 4).map { inner -> it + inner } }
// ```
// Here `it` refers to the outer function expression. The inner function
// expression has an explicit parameter list so it does not introduce `it`.
exists(Identifier ident |
any(AstPlugin p).implicitFunctionExprParamAccess(ident, n) and
fe = ident.getEnclosingCallable() and
result = ident.getValue()
)
}

bindingset[cd]
predicate defaultConstructorParameter(
ConstructorDeclaration cd, int i, string name, boolean hasDefault
Expand Down
7 changes: 7 additions & 0 deletions unified/ql/lib/codeql/unified/internal/AstPluginSwift.qll
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ private class AstPluginSwift extends AstPlugin {
result in ["var", "let"]
}

override predicate implicitFunctionExprParamAccess(Identifier ident, int n) {
// In Swift implicit function expression parameters are called "shorthand
// argument names" and are denoted by `$` followed by a number:
// https://docs.swift.org/latest/documentation/the-swift-programming-language/closures/#Shorthand-Argument-Names
n = ident.getValue().regexpCapture("\\$([0-9]+)", 1).toInt()
}

bindingset[cd]
override predicate defaultConstructorParameter(
ConstructorDeclaration cd, int i, string name, boolean hasDefault
Expand Down
14 changes: 12 additions & 2 deletions unified/ql/lib/codeql/unified/internal/FacadeAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 [0 ... -1].

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 {
Expand Down
14 changes: 14 additions & 0 deletions unified/ql/lib/codeql/unified/internal/LocalNameBinding.qll
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module;
private import unified
private import unified as U
private import codeql.namebinding.LocalNameBinding
private import codeql.unified.internal.AstPlugin
private import codeql.unified.internal.NameBindingPlugin
private import codeql.unified.internal.StaticNameBinding

Expand Down Expand Up @@ -325,6 +326,9 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
scope = callable
)
or
isLocalVariable = true and
name = getImplicitFunctionExprParamName(scope, _)
or
exists(ClassLikeDeclaration cls |
isLocalVariable = false and
name = any(NameBindingPlugin p).getStaticSelfName(cls) and
Expand Down Expand Up @@ -415,6 +419,9 @@ module Public {
* Holds if this local variable represents an implicit receiver parameter of the given callable.
*/
abstract predicate isImplicitReceiverParameter(Callable c);

/** Holds if this local variable represents implicit parameter `n` of `fe`. */
abstract predicate isImplicitFunctionExprParameter(FunctionExpr fe, int n);
}

private class ExplicitLocalVariable extends LocalVariableImpl {
Expand All @@ -438,6 +445,8 @@ module Public {
override Callable getDeclaringCallable() { result = this.getABinding().getEnclosingCallable() }

override predicate isImplicitReceiverParameter(Callable c) { none() }

override predicate isImplicitFunctionExprParameter(FunctionExpr fe, int n) { none() }
}

private class ImplicitLocalVariable extends LocalVariableImpl instanceof LocalNameBindingOutput::ImplicitLocal
Expand All @@ -462,6 +471,11 @@ module Public {
name = any(NameBindingPlugin p).getImplicitReceiverParameterName(scope) and
scope = c
}

override predicate isImplicitFunctionExprParameter(FunctionExpr fe, int n) {
scope = fe and
name = getImplicitFunctionExprParamName(fe, n)
}
}

private AccessorDeclaration getAnAccessor(LocalVariable v) {
Expand Down
40 changes: 40 additions & 0 deletions unified/ql/lib/codeql/unified/internal/ParameterEx.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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)
Expand All @@ -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`.
Expand All @@ -57,6 +72,8 @@ abstract private class ParameterExImpl extends TParameterEx {
or
this.isImplicitReceiverParameter(result)
or
this.isImplicitFunctionExprParameter(result, _)
or
this.isDefaultConstructorParameter(result, _, _)
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ predicate step(BuilderNode node1, Step step, BuilderNode node2) {
node2.isLocalVariableWrite(callable, getImplicitReceiverVariable(callable))
)
or
exists(FunctionExpr fe, LocalVariable var, ParameterPosition pos |
var.isImplicitFunctionExprParameter(fe, pos.asPositional()) and
node1.isImplicitParameter(toDataFlowCallable(fe), pos) and
step.value() and
node2.isLocalVariableWrite(fe, var)
)
or
exists(CallExpr call, Expr receiverExpr |
receiverExpr = call.getCallee().(MemberAccessExpr).getBase()
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ predicate performsVariableAccess(
or
exists(Callable callable |
repr = callable and
var = getImplicitReceiverVariable(callable) and
(
var = getImplicitReceiverVariable(callable)
or
var.isImplicitFunctionExprParameter(callable, _)
) and
kind.isWrite() and
cfgNode.(ControlFlow::EntryNode).getEnclosingCallable() = callable
)
Expand All @@ -118,8 +122,14 @@ newtype TDataFlowNode =
not exists(callable.asSummarizedCallable()) // represented by TSummaryNode
} or
TCallableNode(DataFlowCallable callable) or
TImplicitParameterNode(DataFlowCallable callable, ImplicitParameterPosition pos) {
not exists(callable.asSummarizedCallable()) // represented by TSummaryNode
TImplicitParameterNode(DataFlowCallable callable, ParameterPosition pos) {
not exists(callable.asSummarizedCallable()) and // represented by TSummaryNode
(
pos instanceof ImplicitParameterPosition
or
pos.asPositional() =
[0 .. callable.asSourceCallable().(FunctionExpr).getNumberOfImplicitParameters() - 1]
)
} or
TImplicitArgumentNode(DataFlowCall call, ImplicitArgumentPosition pos, Boolean isPost) {
not call.isSummaryCall(_, _) // represented by TSummaryNode
Expand Down Expand Up @@ -205,7 +215,7 @@ class BuilderNode extends TDataFlowNodeStage1 {
predicate isReturnNodeEx(DataFlowCallable callable) { this = TReturnNode(callable) }

/** Holds if this node represents an implicit parameter of `callable`. */
predicate isImplicitParameter(DataFlowCallable callable, ImplicitParameterPosition pos) {
predicate isImplicitParameter(DataFlowCallable callable, ParameterPosition pos) {
this = TImplicitParameterNode(callable, pos)
}

Expand Down
13 changes: 6 additions & 7 deletions unified/ql/lib/codeql/unified/internal/typeinference/Type.qll
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ private import unified as Unified
private import TypeMention
private import TypeInference
private import codeql.unified.internal.StaticNameBinding
private import codeql.unified.internal.ParameterEx
private import codeql.unified.internal.mad.LegacyMaD as MaD

/**
Expand Down Expand Up @@ -58,11 +59,9 @@ newtype TType =
exists(c.getNameNode())
} or
TAliasType(TypeAliasDeclaration alias) or
TClosureParameterPseudoType(Parameter p) {
exists(FunctionExpr fe |
p = fe.getAParameter() and
not exists(p.getType())
)
TClosureParameterPseudoType(ParameterEx p) {
p.isFunctionExprParameter(_, _) and
not exists(p.getType())
} or
TTypeParameterType(Unified::TypeParameter tp) or
TAssociatedTypeParameterType(
Expand Down Expand Up @@ -217,11 +216,11 @@ class UnknownType extends PseudoType, TUnknownType {
* A type representing a closure parameter.
*/
class ClosureParameterPseudoType extends PseudoType, TClosureParameterPseudoType {
private Parameter param;
private ParameterEx param;

ClosureParameterPseudoType() { this = TClosureParameterPseudoType(param) }

Parameter getParam() { result = param }
ParameterEx getParam() { result = param }

override TypeParameter getPositionalTypeParameter(int i) { none() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* - Overloading (both arity based and type based)
* - Indexing expressions
* - Operations
* - Closure short-hand parameters `$0`, `$1`, etc (extractor change?)
*/

private import codeql.util.Unit
Expand Down Expand Up @@ -341,7 +340,12 @@ module Input3 implements InputSig3 {
or
exists(Parameter param |
implicitParameterDecl(param, v) and
result = param.getType().getTypeAt(path)
(
result = param.getType().getTypeAt(path)
or
param.getParameterEx().isImplicitFunctionExprParameter(_, _) and
result = inferType(v.getAnAccess(), path)
)
)
}

Expand Down Expand Up @@ -423,6 +427,11 @@ module Input3 implements InputSig3 {
p.getParameterEx().isImplicitReceiverParameter(c) and
v.(LocalVariable).isImplicitReceiverParameter(c)
)
or
exists(FunctionExpr fe, int i |
p.getParameterEx().isImplicitFunctionExprParameter(fe, i) and
v.(LocalVariable).isImplicitFunctionExprParameter(fe, i)
)
}

class Callable extends DeclarationImpl, TCallableDeclaration {
Expand Down Expand Up @@ -489,6 +498,8 @@ module Input3 implements InputSig3 {
or
param.asParameter() = c.getParameter(i - 1)
or
param.isImplicitFunctionExprParameter(c, i - 1)
or
param.isDefaultConstructorParameter(c, i, _)
)
)
Expand Down Expand Up @@ -680,7 +691,7 @@ module Input3 implements InputSig3 {
}

class ClosureParameterPseudoType extends T::ClosureParameterPseudoType {
Parameter getParameter() { result.getParameterEx().asParameter() = this.getParam() }
Parameter getParameter() { result.getParameterEx() = this.getParam() }
}

pragma[nomagic]
Expand All @@ -689,7 +700,7 @@ module Input3 implements InputSig3 {
pragma[nomagic]
TypePath getClosureParameterTypePath(Parameter p) {
exists(FunctionExpr fe, Type t, int i |
p.getParameterEx().asParameter() = fe.getParameter(i) and
p.getParameterEx().isFunctionExprParameter(fe, i) and
t = getFunctionExprType(fe) and
result = getFunctionExprParameterTypePath(t, fe.getNumberOfParameters(), i)
)
Expand Down
Loading
Loading