Unverified Commit 315d88b6 authored by 82marbag's avatar 82marbag Committed by GitHub
Browse files

Lifetimes of inner references (#3141)



We weren't computing inner lifetimes, e.g. `Option(&'a ...)`

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>
parent cfdec948
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -284,6 +284,13 @@ inline fun <reified T : RustType.Container> RustType.stripOuter(): RustType = wh
    else -> this
}

/** Extracts the inner Reference type */
fun RustType.innerReference(): RustType? = when (this) {
    is RustType.Reference -> this
    is RustType.Container -> this.member.innerReference()
    else -> null
}

/** Wraps a type in Option if it isn't already */
fun RustType.asOptional(): RustType = when (this) {
    is RustType.Option -> this
+2 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.asRef
import software.amazon.smithy.rust.codegen.core.rustlang.deprecatedShape
import software.amazon.smithy.rust.codegen.core.rustlang.docs
import software.amazon.smithy.rust.codegen.core.rustlang.documentShape
import software.amazon.smithy.rust.codegen.core.rustlang.innerReference
import software.amazon.smithy.rust.codegen.core.rustlang.isCopy
import software.amazon.smithy.rust.codegen.core.rustlang.isDeref
import software.amazon.smithy.rust.codegen.core.rustlang.render
@@ -93,7 +94,7 @@ open class StructureGenerator(
     */
    private fun lifetimeDeclaration(): String {
        val lifetimes = members
            .map { symbolProvider.toSymbol(it).rustType() }
            .map { symbolProvider.toSymbol(it).rustType().innerReference() }
            .mapNotNull {
                when (it) {
                    is RustType.Reference -> it.lifetime
+9 −0
Original line number Diff line number Diff line
@@ -220,4 +220,13 @@ internal class RustTypesTest {
        val attributeMacro = Attribute(derive())
        forInputExpectOutput(writable { attributeMacro.render(this) }, "")
    }

    @Test
    fun `finds inner reference type`() {
        val innerReference = RustType.Reference("a", RustType.Bool)
        val type = RustType.Box(RustType.Option(innerReference))

        type.innerReference() shouldBe innerReference
        RustType.Bool.innerReference() shouldBe null
    }
}