Skip to content

Commit

Permalink
Prevent method invocations from being created from FirSafeCallExpress…
Browse files Browse the repository at this point in the history
…ion that are not functions.

Reverted variableType call in visitSimpleNameExpression. In most cases names are not variables.
Reverted changes to visitArrayAccessExpression.
  • Loading branch information
traceyyoshima committed Oct 29, 2023
1 parent 9ea816b commit 41b0551
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,53 +179,34 @@ public J visitAnonymousInitializer(KtAnonymousInitializer initializer, Execution

@Override
public J visitArrayAccessExpression(KtArrayAccessExpression expression, ExecutionContext data) {
Expression left = convertToExpression(expression.getArrayExpression().accept(this, data));
Markers markers = Markers.EMPTY;
boolean hasExplicitReceiver = false;
boolean implicitExtensionFunction = false;
Expression selectExpr = convertToExpression(expression.getArrayExpression().accept(this, data));
JRightPadded<Expression> select = padRight(selectExpr, suffix(expression.getArrayExpression()));
JContainer<Expression> typeParams = null;
J.Identifier name = createIdentifier("get", Space.EMPTY, methodInvocationType(expression));

if (expression.getIndexExpressions().size() != 1) {
throw new UnsupportedOperationException("TODO");
markers = markers.addIfAbsent(new IndexedAccess(randomId()));

List<KtExpression> indexExpressions = expression.getIndexExpressions();
List<JRightPadded<Expression>> expressions = new ArrayList<>();

for (KtExpression indexExp : indexExpressions) {
expressions.add(padRight(convertToExpression(indexExp.accept(this, data)), suffix(indexExp)));
}

return new J.ArrayAccess(randomId(),
prefix(expression),
Markers.EMPTY,
left,
new J.ArrayDimension(randomId(),
merge(prefix(expression.getIndicesNode()), prefix(expression.getLeftBracket())),
Markers.EMPTY,
padRight(convertToExpression(expression.getIndexExpressions().get(0).accept(this, data)),
prefix(expression.getRightBracket()))
),
null // type(expression)
JContainer<Expression> args = JContainer.build(Space.EMPTY, expressions, markers);
return new J.MethodInvocation(
randomId(),
Space.EMPTY,
markers,
select,
typeParams,
name,
args,
methodInvocationType(expression)
);
//
// Markers markers = Markers.EMPTY;
// boolean hasExplicitReceiver = false;
// boolean implicitExtensionFunction = false;
// Expression selectExpr = convertToExpression(expression.getArrayExpression().accept(this, data));
// JRightPadded<Expression> select = padRight(selectExpr, suffix(expression.getArrayExpression()));
// JContainer<Expression> typeParams = null;
// J.Identifier name = createIdentifier("get", Space.EMPTY, methodInvocationType(expression));
//
// markers = markers.addIfAbsent(new IndexedAccess(randomId()));
//
// List<KtExpression> indexExpressions = expression.getIndexExpressions();
// List<JRightPadded<Expression>> expressions = new ArrayList<>();
//
// for (KtExpression indexExp : indexExpressions) {
// expressions.add(padRight(convertToExpression(indexExp.accept(this, data)), suffix(indexExp)));
// }
//
// JContainer<Expression> args = JContainer.build(Space.EMPTY, expressions, markers);
// return new J.MethodInvocation(
// randomId(),
// Space.EMPTY,
// markers,
// select,
// typeParams,
// name,
// args,
// methodInvocationType(expression)
// );
}

@Override
Expand Down Expand Up @@ -2932,10 +2913,8 @@ public J visitPropertyDelegate(KtPropertyDelegate delegate, ExecutionContext dat

@Override
public J visitSimpleNameExpression(KtSimpleNameExpression expression, ExecutionContext data) {
// TODO: The FIR does not have a PSI element associated to names, similarly name references from the PSI are not linked to the IR.
// The type must be added in context to the parent element.
// I.E. IrConstructor assocaited to the KtPrimaryConstructor.
return createIdentifier(expression, type(expression), variableType(expression));
// The correct type cannot consistently be associated to the expression due to the relationship between the PSI and FIR.
return createIdentifier(expression, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,17 @@ class PsiElementAssociations(val typeMapping: KotlinTypeMapping, val file: FirFi
else -> throw UnsupportedOperationException("Unsupported resolved symbol: ${fir.calleeReference.resolved?.resolvedSymbol?.javaClass}")
}
}
is FirSafeCallExpression -> ExpressionType.METHOD_INVOCATION
is FirSafeCallExpression -> {
return when (fir.selector) {
is FirFunctionCall -> when (fir.selector.calleeReference?.resolved?.resolvedSymbol) {
is FirConstructorSymbol -> ExpressionType.CONSTRUCTOR
is FirNamedFunctionSymbol -> ExpressionType.METHOD_INVOCATION
else -> null
}

else -> null
}
}
else -> throw UnsupportedOperationException("Unsupported call type: ${fir.javaClass}")
}
}
Expand Down

0 comments on commit 41b0551

Please sign in to comment.