From 315d88b6d43832284f48700f6d71d8d402578c23 Mon Sep 17 00:00:00 2001 From: 82marbag <69267416+82marbag@users.noreply.github.com> Date: Thu, 2 Nov 2023 20:32:26 +0000 Subject: [PATCH] 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: Daniele Ahmed --- .../amazon/smithy/rust/codegen/core/rustlang/RustType.kt | 7 +++++++ .../codegen/core/smithy/generators/StructureGenerator.kt | 3 ++- .../smithy/rust/codegen/core/rustlang/RustTypeTest.kt | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt index 873ead1bc8..f20572bbd3 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt @@ -284,6 +284,13 @@ inline fun 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 diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt index 5c9635c5a8..d30365bba4 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt @@ -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 diff --git a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustTypeTest.kt b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustTypeTest.kt index c0443166f9..fcd9583d49 100644 --- a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustTypeTest.kt +++ b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustTypeTest.kt @@ -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 + } }