From 4a4f42537f2ce1ffbe9ac4ecf6eb5af72ca6365c Mon Sep 17 00:00:00 2001 From: John DiSanti Date: Wed, 15 Feb 2023 14:23:43 -0800 Subject: [PATCH] Clean up an old hack --- .../core/rustlang/RustReservedWords.kt | 29 +++++++++---------- .../core/rustlang/RustReservedWordsTest.kt | 2 +- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWords.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWords.kt index 03b59fe384..51c03b5b10 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWords.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWords.kt @@ -52,20 +52,10 @@ class RustReservedWordSymbolProvider(private val base: RustSymbolProvider) : Wra // that represent union variants that have been added since this SDK was generated. UnionGenerator.UnknownVariantName -> "${UnionGenerator.UnknownVariantName}Value" "${UnionGenerator.UnknownVariantName}Value" -> "${UnionGenerator.UnknownVariantName}Value_" - // Self cannot be used as a raw identifier, so we can't use the normal escaping strategy - // https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094/4 - "Self" -> "SelfValue" - // Real models won't end in `_` so it's safe to stop here - "SelfValue" -> "SelfValue_" else -> reservedWordReplacedName } container is EnumShape || container.hasTrait() -> when (baseName) { - // Self cannot be used as a raw identifier, so we can't use the normal escaping strategy - // https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094/4 - "Self" -> "SelfValue" - // Real models won't end in `_` so it's safe to stop here - "SelfValue" -> "SelfValue_" // Unknown is used as the name of the variant containing unexpected values "Unknown" -> "UnknownValue" // Real models won't end in `_` so it's safe to stop here @@ -168,11 +158,20 @@ object RustReservedWords : ReservedWords { "try", ) - private val cantBeRaw = setOf("self", "crate", "super") + // Some things can't be used as a raw identifier, so we can't use the normal escaping strategy + // https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094/4 + private val keywordEscapingMap = mapOf( + "crate" to "crate_", + "super" to "super_", + "self" to "self_", + "Self" to "SelfValue", + // Real models won't end in `_` so it's safe to stop here + "SelfValue" to "SelfValue_", + ) - override fun escape(word: String): String = when { - cantBeRaw.contains(word) -> "${word}_" - else -> "r##$word" + override fun escape(word: String): String = when (val mapped = keywordEscapingMap[word]) { + null -> "r##$word" + else -> mapped } fun escapeIfNeeded(word: String): String = when (isReserved(word)) { @@ -180,5 +179,5 @@ object RustReservedWords : ReservedWords { else -> word } - override fun isReserved(word: String): Boolean = RustKeywords.contains(word) + override fun isReserved(word: String): Boolean = RustKeywords.contains(word) || keywordEscapingMap.contains(word) } diff --git a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWordsTest.kt b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWordsTest.kt index 36356abf52..563dc2f454 100644 --- a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWordsTest.kt +++ b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWordsTest.kt @@ -30,7 +30,7 @@ internal class RustReservedWordSymbolProviderTest { """.asSmithyModel() val provider = RustReservedWordSymbolProvider(TestSymbolProvider(model)) val symbol = provider.toSymbol(model.lookup("test#Self")) - symbol.name shouldBe "r##Self" + symbol.name shouldBe "SelfValue" } @Test