diff --git a/codeOptimizers/src/prog8/optimizer/Inliner.kt b/codeOptimizers/src/prog8/optimizer/Inliner.kt index 122444345..d8d90b44e 100644 --- a/codeOptimizers/src/prog8/optimizer/Inliner.kt +++ b/codeOptimizers/src/prog8/optimizer/Inliner.kt @@ -118,9 +118,11 @@ class Inliner(val program: Program): AstWalker() { } private fun makeFullyScoped(identifier: IdentifierReference) { - val scoped = (identifier.targetStatement(program)!! as INamedStatement).scopedName - val scopedIdent = IdentifierReference(scoped, identifier.position) - modifications += IAstModification.ReplaceNode(identifier, scopedIdent, identifier.parent) + identifier.targetStatement(program)?.let { target -> + val scoped = (target as INamedStatement).scopedName + val scopedIdent = IdentifierReference(scoped, identifier.position) + modifications += IAstModification.ReplaceNode(identifier, scopedIdent, identifier.parent) + } } private fun makeFullyScoped(call: BuiltinFunctionCallStatement) { @@ -130,27 +132,30 @@ class Inliner(val program: Program): AstWalker() { } private fun makeFullyScoped(call: FunctionCallStatement) { - val sub = call.target.targetSubroutine(program)!! - val scopedName = IdentifierReference(sub.scopedName, call.target.position) - val scopedArgs = makeScopedArgs(call.args) - val scopedCall = FunctionCallStatement(scopedName, scopedArgs.toMutableList(), call.void, call.position) - modifications += IAstModification.ReplaceNode(call, scopedCall, call.parent) + call.target.targetSubroutine(program)?.let { sub -> + val scopedName = IdentifierReference(sub.scopedName, call.target.position) + val scopedArgs = makeScopedArgs(call.args) + val scopedCall = FunctionCallStatement(scopedName, scopedArgs.toMutableList(), call.void, call.position) + modifications += IAstModification.ReplaceNode(call, scopedCall, call.parent) + } } private fun makeFullyScoped(call: BuiltinFunctionCall) { - val sub = call.target.targetSubroutine(program)!! - val scopedName = IdentifierReference(sub.scopedName, call.target.position) - val scopedArgs = makeScopedArgs(call.args) - val scopedCall = BuiltinFunctionCall(scopedName, scopedArgs.toMutableList(), call.position) - modifications += IAstModification.ReplaceNode(call, scopedCall, call.parent) + call.target.targetSubroutine(program)?.let { sub -> + val scopedName = IdentifierReference(sub.scopedName, call.target.position) + val scopedArgs = makeScopedArgs(call.args) + val scopedCall = BuiltinFunctionCall(scopedName, scopedArgs.toMutableList(), call.position) + modifications += IAstModification.ReplaceNode(call, scopedCall, call.parent) + } } private fun makeFullyScoped(call: FunctionCallExpression) { - val sub = call.target.targetSubroutine(program)!! - val scopedName = IdentifierReference(sub.scopedName, call.target.position) - val scopedArgs = makeScopedArgs(call.args) - val scopedCall = FunctionCallExpression(scopedName, scopedArgs.toMutableList(), call.position) - modifications += IAstModification.ReplaceNode(call, scopedCall, call.parent) + call.target.targetSubroutine(program)?.let { sub -> + val scopedName = IdentifierReference(sub.scopedName, call.target.position) + val scopedArgs = makeScopedArgs(call.args) + val scopedCall = FunctionCallExpression(scopedName, scopedArgs.toMutableList(), call.position) + modifications += IAstModification.ReplaceNode(call, scopedCall, call.parent) + } } private fun makeScopedArgs(args: List): List { diff --git a/compiler/test/TestOptimization.kt b/compiler/test/TestOptimization.kt index 56501d232..81303b036 100644 --- a/compiler/test/TestOptimization.kt +++ b/compiler/test/TestOptimization.kt @@ -793,4 +793,16 @@ main { (statements[6] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 53281.0 (statements[7] as Assignment).target.memoryAddress!!.addressExpression.constValue(result.program)!!.number shouldBe 53281.0 } + + test("no crash on sorting unused array") { + val text=""" +main { + ubyte[5] cards = [ 14, 6, 29, 16, 3 ] + + sub start() { + sort(cards) + } +}""" + compileText(C64Target(), true, text, writeAssembly = false) shouldNotBe null + } })