Skip to content
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

Merged
merged 9 commits into from
May 12, 2023

Conversation

jdisanti
Copy link
Collaborator

@jdisanti jdisanti commented May 12, 2023

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

  • 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.

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).
@jdisanti jdisanti force-pushed the jdisanti-fix-prelude-collisions branch from 6936f69 to 17d4b71 Compare May 12, 2023 00:25
@jdisanti jdisanti marked this pull request as ready for review May 12, 2023 00:27
@jdisanti jdisanti requested review from a team as code owners May 12, 2023 00:27
@jdisanti jdisanti requested a review from a team as a code owner May 12, 2023 01:18
@jdisanti jdisanti requested a review from unexge May 12, 2023 01:18
@github-actions
Copy link

Copy link
Contributor

@ysaito1001 ysaito1001 left a 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?

Comment on lines 95 to 110
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"),
),
Copy link
Contributor

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 */
Copy link
Contributor

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?

Copy link
Collaborator Author

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,
Copy link
Contributor

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.

Copy link
Collaborator Author

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)]
Copy link
Contributor

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.

Copy link
Collaborator Author

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,
Copy link
Contributor

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.

Copy link
Collaborator Author

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 {
Copy link
Contributor

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.

Copy link
Collaborator Author

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 {
Copy link
Contributor

@david-perez david-perez May 12, 2023

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}IntegrationTests. 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.

Copy link
Collaborator Author

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this.

@Velfi Velfi enabled auto-merge May 12, 2023 18:48
@github-actions
Copy link

@jdisanti jdisanti disabled auto-merge May 12, 2023 20:16
@jdisanti jdisanti force-pushed the jdisanti-fix-prelude-collisions branch from f7926ff to 62d0106 Compare May 12, 2023 21:10
Copy link
Contributor

@ysaito1001 ysaito1001 left a 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.

@github-actions
Copy link

@jdisanti jdisanti enabled auto-merge May 12, 2023 22:25
@jdisanti jdisanti added this pull request to the merge queue May 12, 2023
Merged via the queue into main with commit 040d0e4 May 12, 2023
@jdisanti jdisanti deleted the jdisanti-fix-prelude-collisions branch May 12, 2023 23:15
@github-actions
Copy link

david-perez pushed a commit that referenced this pull request May 18, 2023
#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._
david-perez pushed a commit that referenced this pull request May 22, 2023
#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._
david-perez pushed a commit that referenced this pull request May 22, 2023
#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._
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants