Skip to content

Commit

Permalink
Rename unqualified method calls (#11556)
Browse files Browse the repository at this point in the history
close #11281

Changelog:
- update: `refactoring/renameSymbol` can rename unqualified method calls

# Important Notes
https://github.com/user-attachments/assets/e03c4ec7-7620-4ce4-8eab-86b95f308be2
  • Loading branch information
4e6 authored Nov 15, 2024
1 parent 58512e7 commit 6b810ee
Show file tree
Hide file tree
Showing 21 changed files with 394 additions and 123 deletions.
8 changes: 0 additions & 8 deletions docs/language-server/protocol-language-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -3191,14 +3191,6 @@ Current limitations of the method renaming are:
```rust
Main.function1 x = x
```
- Method calls where the self type is not specified will not be renamed, i.e.

```rust
function1 x = x

main =
operator1 = function1 42
```

#### Parameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public Expression transformExpression(Expression ir) {
var loc = sectionLeft.location().isDefined() ? sectionLeft.location().get() : null;
var passData = sectionLeft.passData();
var rightArgName = freshNameSupply.newName(false, Option.empty());
var rightCallArg = new CallArgument.Specified(Option.empty(), rightArgName, null, meta());
var rightCallArg =
new CallArgument.Specified(Option.empty(), rightArgName, true, null, meta());
var rightDefArg =
new DefinitionArgument.Specified(
rightArgName.duplicate(true, true, true, false),
Expand All @@ -89,7 +90,8 @@ public Expression transformExpression(Expression ir) {

if (arg.value() instanceof Name.Blank) {
var leftArgName = freshNameSupply.newName(false, Option.empty());
var leftCallArg = new CallArgument.Specified(Option.empty(), leftArgName, null, meta());
var leftCallArg =
new CallArgument.Specified(Option.empty(), leftArgName, true, null, meta());
var leftDefArg =
new DefinitionArgument.Specified(
leftArgName.duplicate(true, true, true, false),
Expand Down Expand Up @@ -122,7 +124,8 @@ public Expression transformExpression(Expression ir) {
var loc = sectionSides.location().isDefined() ? sectionSides.location().get() : null;
var passData = sectionSides.passData();
var leftArgName = freshNameSupply.newName(false, Option.empty());
var leftCallArg = new CallArgument.Specified(Option.empty(), leftArgName, null, meta());
var leftCallArg =
new CallArgument.Specified(Option.empty(), leftArgName, true, null, meta());
var leftDefArg =
new DefinitionArgument.Specified(
leftArgName.duplicate(true, true, true, false),
Expand All @@ -133,7 +136,8 @@ public Expression transformExpression(Expression ir) {
meta());

var rightArgName = freshNameSupply.newName(false, Option.empty());
var rightCallArg = new CallArgument.Specified(Option.empty(), rightArgName, null, meta());
var rightCallArg =
new CallArgument.Specified(Option.empty(), rightArgName, true, null, meta());
var rightDefArg =
new DefinitionArgument.Specified(
rightArgName.duplicate(true, true, true, false),
Expand Down Expand Up @@ -183,7 +187,8 @@ public Expression transformExpression(Expression ir) {
var loc = sectionRight.location().isDefined() ? sectionRight.location().get() : null;
var passData = sectionRight.passData();
var leftArgName = freshNameSupply.newName(false, Option.empty());
var leftCallArg = new CallArgument.Specified(Option.empty(), leftArgName, null, meta());
var leftCallArg =
new CallArgument.Specified(Option.empty(), leftArgName, true, null, meta());
var leftDefArg =
new DefinitionArgument.Specified(
leftArgName.duplicate(true, true, true, false),
Expand All @@ -197,7 +202,7 @@ public Expression transformExpression(Expression ir) {
// Note [Blanks in Sections]
var rightArgName = freshNameSupply.newName(false, Option.empty());
var rightCallArg =
new CallArgument.Specified(Option.empty(), rightArgName, null, meta());
new CallArgument.Specified(Option.empty(), rightArgName, true, null, meta());
var rightDefArg =
new DefinitionArgument.Specified(
rightArgName.duplicate(true, true, true, false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ case object AliasAnalysis extends IRPass {
args: List[CallArgument],
builder: GraphBuilder
): List[CallArgument] = {
args.map { case arg @ CallArgument.Specified(_, expr, _, _) =>
args.map { case arg @ CallArgument.Specified(_, expr, _, _, _) =>
val currentScope = expr match {
case _: Literal => builder
case _ => builder.addChild()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ object AutomaticParallelism extends IRPass {
Name.Special(Name.Special.WriteRef, null),
List(
CallArgument
.Specified(None, refVars(bind.name).duplicate(), null),
CallArgument.Specified(None, bind.name.duplicate(), null)
.Specified(None, refVars(bind.name).duplicate(), true, null),
CallArgument.Specified(None, bind.name.duplicate(), true, null)
),
false,
null
Expand All @@ -338,6 +338,7 @@ object AutomaticParallelism extends IRPass {
CallArgument.Specified(
None,
Expression.Block(blockBody.init, blockBody.last, null),
true,
null
)
),
Expand All @@ -354,7 +355,7 @@ object AutomaticParallelism extends IRPass {
val threadJoins = threadSpawns.map { bind =>
Application.Prefix(
Name.Special(Name.Special.JoinThread, null),
List(CallArgument.Specified(None, bind.name.duplicate(), null)),
List(CallArgument.Specified(None, bind.name.duplicate(), true, null)),
false,
null
)
Expand All @@ -366,7 +367,7 @@ object AutomaticParallelism extends IRPass {
name.duplicate(),
Application.Prefix(
Name.Special(Name.Special.ReadRef, null),
List(CallArgument.Specified(None, ref.duplicate(), null)),
List(CallArgument.Specified(None, ref.duplicate(), true, null)),
false,
null
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ case object DataflowAnalysis extends IRPass {
info: DependencyInfo
): CallArgument = {
argument match {
case spec @ CallArgument.Specified(name, value, _, _) =>
case spec @ CallArgument.Specified(name, value, _, _, _) =>
val specDep = asStatic(spec)
val valueDep = asStatic(value)
info.dependents.updateAt(valueDep, Set(specDep))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ case object DemandAnalysis extends IRPass {
*/
def analyseCallArgument(arg: CallArgument): CallArgument = {
arg match {
case spec @ CallArgument.Specified(_, expr, _, _) =>
spec.copy(
case arg: CallArgument.Specified =>
arg.copy(
value = analyseExpression(
expr,
arg.value,
isInsideCallArgument = true
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,13 @@ case object FramePointerAnalysis extends IRPass {
arguments: List[CallArgument],
graph: Graph
): Unit = {
arguments.foreach { case arg @ CallArgument.Specified(name, value, _, _) =>
maybeAttachFramePointer(arg, graph)
name.foreach(maybeAttachFramePointer(_, graph))
processExpression(value, graph, false)
maybAttachFrameVariableNames(value)
maybAttachFrameVariableNames(arg)
arguments.foreach {
case arg @ CallArgument.Specified(name, value, _, _, _) =>
maybeAttachFramePointer(arg, graph)
name.foreach(maybeAttachFramePointer(_, graph))
processExpression(value, graph, false)
maybAttachFrameVariableNames(value)
maybAttachFrameVariableNames(arg)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class LambdaShorthandToLambdaMini(
parent match {
case Application.Prefix(fn, args, _, _, _) =>
val hasBlankArg = args.exists {
case CallArgument.Specified(_, _: Name.Blank, _, _) => true
case _ => false
case CallArgument.Specified(_, _: Name.Blank, _, _, _) => true
case _ => false
}
val hasBlankFn = fn.isInstanceOf[Name.Blank]
hasBlankArg || hasBlankFn
Expand Down Expand Up @@ -222,8 +222,8 @@ class LambdaShorthandToLambdaMini(
private def determineLambdaShorthand(
args: List[CallArgument]
): List[Boolean] = {
args.map { case CallArgument.Specified(_, value, _, _) =>
value match {
args.map { arg =>
arg.value match {
case _: Name.Blank => true
case _ => false
}
Expand All @@ -245,14 +245,14 @@ class LambdaShorthandToLambdaMini(
val isShorthand = argAndIsShorthand._2

arg match {
case s @ CallArgument.Specified(_, value, _, _) =>
case s: CallArgument.Specified =>
if (isShorthand) {
val newName = freshNameSupply
.newName()
.copy(
location = value.location,
passData = value.passData,
diagnostics = value.diagnostics
location = s.value.location,
passData = s.value.passData,
diagnostics = s.value.diagnostics
)

s.copy(value = newName)
Expand All @@ -274,11 +274,11 @@ class LambdaShorthandToLambdaMini(
): Option[DefinitionArgument] = {
if (isShorthand) {
arg match {
case specified @ CallArgument.Specified(_, value, _, passData) =>
case specified: CallArgument.Specified =>
// Note [Safe Casting to Name.Literal]
val defArgName =
Name.Literal(
value.asInstanceOf[Name.Literal].name,
specified.value.asInstanceOf[Name.Literal].name,
isMethod = false,
null
)
Expand All @@ -290,7 +290,7 @@ class LambdaShorthandToLambdaMini(
None,
suspended = false,
null,
passData.duplicate,
specified.passData.duplicate,
specified.diagnosticsCopy
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@ case object GlobalNames extends IRPass {
val app = Application.Prefix(
fun,
List(
CallArgument
.Specified(None, self, identifiedLocation = null)
CallArgument.Specified(
None,
self,
true,
identifiedLocation = null
)
),
hasDefaultsSuspended = false,
lit.identifiedLocation
Expand Down Expand Up @@ -345,7 +349,7 @@ case object GlobalNames extends IRPass {
)
)
val selfArg =
CallArgument.Specified(None, self, identifiedLocation = null)
CallArgument.Specified(None, self, true, identifiedLocation = null)
processedFun.passData.remove(this) // Necessary for IrToTruffle
app.copy(function = processedFun, arguments = selfArg :: processedArgs)
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ case object TypeFunctions extends IRPass {
*/
private def resolveCallArgument(arg: CallArgument): CallArgument = {
arg match {
case spec @ CallArgument.Specified(_, value, _, _) =>
case spec: CallArgument.Specified =>
spec.copy(
value = resolveExpression(value)
value = resolveExpression(spec.value)
)
}
}
Expand All @@ -241,8 +241,8 @@ case object TypeFunctions extends IRPass {
*/
private def isValidCallArg(arg: CallArgument): Boolean = {
arg match {
case CallArgument.Specified(name, _, _, _) =>
name.isEmpty
case specified: CallArgument.Specified =>
specified.name.isEmpty
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.enso.compiler.refactoring
import org.enso.compiler.core.Implicits.AsMetadata
import org.enso.compiler.core.{ExternalID, IR, Identifier}
import org.enso.compiler.core.ir.Name
import org.enso.compiler.core.ir.expression.Application
import org.enso.compiler.data.BindingsMap
import org.enso.compiler.pass.analyse.DataflowAnalysis
import org.enso.compiler.pass.resolve.MethodCalls
Expand Down Expand Up @@ -64,23 +65,27 @@ trait IRUtils {
for {
usages <- findDynamicUsages(ir, node)
} yield {
usages
.collect {
case usage: Name.Literal
if usage.isMethod && usage.name == node.name =>
usage
}
.flatMap { symbol =>
symbol.getMetadata(MethodCalls).flatMap { resolution =>
resolution.target match {
case BindingsMap.ResolvedModuleMethod(module, _)
if module.getName == moduleName =>
Some(symbol)
case _ =>
None
}
usages.collect {
case Application.Prefix(function: Name.Literal, args, _, _, _)
if function.name == node.name =>
function.getMetadata(MethodCalls) match {
case Some(resolution) =>
resolution.target match {
case BindingsMap.ResolvedModuleMethod(module, _)
if module.getName == moduleName =>
Some(function)
case _ =>
None
}
case None =>
args.headOption match {
case Some(arg) if arg.isSynthetic =>
Some(function)
case _ =>
None
}
}
}
}.flatten
}

/** Find usages of a static dependency in the [[DataflowAnalysis]] metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GatherDiagnosticsTest extends CompilerTest {
val plusApp = Application.Prefix(
plusOp,
List(
CallArgument.Specified(None, error1, identifiedLocation = null)
CallArgument.Specified(None, error1, false, identifiedLocation = null)
),
hasDefaultsSuspended = false,
identifiedLocation = null
Expand Down Expand Up @@ -121,11 +121,11 @@ class GatherDiagnosticsTest extends CompilerTest {
)

val result = GatherDiagnostics.runModule(module, buildModuleContext())
val gatheredErrros = result
val gatheredErros = result
.unsafeGetMetadata(GatherDiagnostics, "Impossible")
.diagnostics

gatheredErrros.toSet shouldEqual Set(error1, error2, error3)
gatheredErros.toSet shouldEqual Set(error1, error2, error3)
}

"work with annotations" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ case object TailCallMegaPass extends IRPass {
*/
private def analyseCallArg(argument: CallArgument): CallArgument = {
argument match {
case arg @ CallArgument.Specified(_, expr, _, _) =>
case arg: CallArgument.Specified =>
arg
.copy(
// Note [Call Argument Tail Position]
value = analyseExpression(expr, isInTailPosition = true)
value = analyseExpression(arg.value, isInTailPosition = true)
)
.updateMetadata(TAIL_META)
}
Expand Down
Loading

0 comments on commit 6b810ee

Please sign in to comment.