Skip to content

Commit

Permalink
Lifetimes of inner references (#3141)
Browse files Browse the repository at this point in the history
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: Daniele Ahmed <[email protected]>
  • Loading branch information
82marbag authored Nov 2, 2023
1 parent cfdec94 commit 315d88b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 315d88b

Please sign in to comment.