-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix client operation name collisions with the standard library prelude #2696
Conversation
Operations named `Send` or `Sync` (and probably others) were colliding with the types in the standard library prelude and causing compiler errors. This PR adds tests that include all the type names from the Rust prelude, and fixes the compiler errors they cause. In the future, the `no_implicit_prelude` attribute can be added to certain code generated modules to better enforce that there can't be name collisions, but for now, the `tokio::test` macro doesn't compile with that attribute enabled (and likely other macros from other libraries).
6936f69
to
17d4b71
Compare
A new generated diff is ready to view.
A new doc preview is ready to view. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this effort! To contribute, if we find more instances of unqualified prelude in the codebase, we can fix them on-the-fly as we go in respective PRs?
codegen-client-test/build.gradle.kts
Outdated
CodegenTest( | ||
"crate#Config", | ||
"naming_test_prelude_ops", | ||
""" | ||
, "codegen": { "renameErrors": false } | ||
""".trimIndent(), | ||
imports = listOf("$commonModels/naming-obstacle-course-prelude-ops.smithy"), | ||
), | ||
CodegenTest( | ||
"crate#Config", | ||
"naming_test_prelude_structs", | ||
""" | ||
, "codegen": { "renameErrors": false } | ||
""".trimIndent(), | ||
imports = listOf("$commonModels/naming-obstacle-course-prelude-structs.smithy"), | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great tests to add!
@@ -193,14 +193,57 @@ data class RuntimeType(val path: String, val dependency: RustDependency? = null) | |||
* The companion object contains commonly used RuntimeTypes | |||
*/ | |||
companion object { | |||
/** Scope that contains all Rust prelude types, but not macros or functions */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Maybe it's useful to include a link to prelude contents like this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 49d10ae
@@ -223,6 +223,7 @@ private fun renderCustomizableOperationSendMethod( | |||
val combinedGenerics = operationGenerics + handleGenerics | |||
|
|||
val codegenScope = arrayOf( | |||
*RuntimeType.preludeScope, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should just merge this scope directly in rustTemplate
and friends.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely thought about it. That and deleting rust()
and renaming rustTemplate()
to rust()
. I'd like to get rid of the old style #T
formatters entirely to tidy things up. All of this is quite a bit of work, and I wanted to get this initial fix out first.
@@ -112,12 +113,12 @@ class FluentClientGenerator( | |||
} | |||
|
|||
#{client_docs:W} | |||
##[derive(std::fmt::Debug)] | |||
##[derive(::std::fmt::Debug)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think prepending all types with ::
is a very good idea and something we should somehow enforce everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can use no_implicit_prelude
, then it will be enforced by the compiler. But there are many dependency macros that will need to get fixed before we can do that.
Err, | ||
String, | ||
ToString, | ||
Vec, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TryFrom
, TryInto
, and FromIterator
are missing? https://doc.rust-lang.org/std/prelude/index.html
Would be nice to put the prelude link in these files.
We generate Rust 2021 edition crates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! Fixed in 49d10ae
/// Confounds model generation machinery by using structs named after every item in the Rust prelude | ||
@awsJson1_1 | ||
@service(sdkId: "Config") | ||
service Config { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like there's not a need for having these be two separate integration tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had split them apart due to name collisions within the Smithy model, but it just occurred to me that I can probably use namespaces to fix that.
/// Confounds model generation machinery by using operations named after every item in the Rust prelude | ||
@awsJson1_1 | ||
@service(sdkId: "Config") | ||
service Config { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the kinds of tests that I think are better done in {client,server}IntegrationTest
s. In Kotlin we can easily code generate Smithy models that more exhaustively test this e.g. union / enum shapes with names of types from the prelude are not covered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great idea. I refactored them into Kotlin unit tests in 49d10ae and also added them to the server codegen test suite.
@@ -142,6 +143,21 @@ fun <T : AbstractCodeWriter<T>> T.conditionalBlock( | |||
return this | |||
} | |||
|
|||
fun RustWriter.conditionalBlockTemplate( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this.
A new generated diff is ready to view.
A new doc preview is ready to view. |
f7926ff
to
62d0106
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-approved after new commits. Looks fantastic! I really like how test models are now automatically derived from preludeScope
and used by both client & server tests as per David's comment.
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
#2696) ## Motivation and Context Operations named `Send` or `Sync` (and probably others) were colliding with the types in the standard library prelude and causing compiler errors. This PR adds tests that include all the type names from the Rust prelude, and fixes the compiler errors they cause. In the future, the `no_implicit_prelude` attribute can be added to certain code generated modules to better enforce that there can't be name collisions, but for now, the `tokio::test` macro doesn't compile with that attribute enabled (and likely other macros from other libraries). ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
#2696) ## Motivation and Context Operations named `Send` or `Sync` (and probably others) were colliding with the types in the standard library prelude and causing compiler errors. This PR adds tests that include all the type names from the Rust prelude, and fixes the compiler errors they cause. In the future, the `no_implicit_prelude` attribute can be added to certain code generated modules to better enforce that there can't be name collisions, but for now, the `tokio::test` macro doesn't compile with that attribute enabled (and likely other macros from other libraries). ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
#2696) ## Motivation and Context Operations named `Send` or `Sync` (and probably others) were colliding with the types in the standard library prelude and causing compiler errors. This PR adds tests that include all the type names from the Rust prelude, and fixes the compiler errors they cause. In the future, the `no_implicit_prelude` attribute can be added to certain code generated modules to better enforce that there can't be name collisions, but for now, the `tokio::test` macro doesn't compile with that attribute enabled (and likely other macros from other libraries). ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
Motivation and Context
Operations named
Send
orSync
(and probably others) were colliding with the types in the standard library prelude and causing compiler errors. This PR adds tests that include all the type names from the Rust prelude, and fixes the compiler errors they cause.In the future, the
no_implicit_prelude
attribute can be added to certain code generated modules to better enforce that there can't be name collisions, but for now, thetokio::test
macro doesn't compile with that attribute enabled (and likely other macros from other libraries).Checklist
CHANGELOG.next.toml
if I made changes to the smithy-rs codegen or runtime cratesBy submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.