Skip to content

Fix for Rust compiler warning about derive helpers #2581

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

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,17 @@ data class RustMetadata(
this.copy(derives = derives - withoutDerives.toSet())

fun renderAttributes(writer: RustWriter): RustMetadata {
additionalAttributes.forEach {
val (deriveHelperAttrs, otherAttrs) = additionalAttributes.partition { it.isDeriveHelper }
otherAttrs.forEach {
it.render(writer)
}

Attribute(derive(derives)).render(writer)

// Derive helper attributes must come after derive, see https://github.com/rust-lang/rust/issues/79202
deriveHelperAttrs.forEach {
it.render(writer)
}
return this
}

Expand Down Expand Up @@ -450,17 +456,22 @@ enum class AttributeKind {
* [Attributes](https://doc.rust-lang.org/reference/attributes.html) are general free form metadata
* that are interpreted by the compiler.
*
* If the attribute is a "derive helper", such as `#[serde]`, set `isDeriveHelper` to `true` so it is sorted correctly after
* the derive attribute is rendered. (See https://github.com/rust-lang/rust/issues/79202 for why sorting matters.)
*
* For example:
* ```rust
* #[allow(missing_docs)] // <-- this is an attribute, and it is not a derive helper
* #[derive(Clone, PartialEq, Serialize)] // <-- this is an attribute
* #[serde(serialize_with = "abc")] // <-- this is an attribute
* #[serde(serialize_with = "abc")] // <-- this attribute is a derive helper because the `Serialize` derive uses it
* struct Abc {
* a: i64
* }
* ```
*/
class Attribute(val inner: Writable) {
class Attribute(val inner: Writable, val isDeriveHelper: Boolean = false) {
constructor(str: String) : this(writable(str))
constructor(str: String, isDeriveHelper: Boolean) : this(writable(str), isDeriveHelper)
constructor(runtimeType: RuntimeType) : this(runtimeType.writable)

fun render(writer: RustWriter, attributeKind: AttributeKind = AttributeKind.Outer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ class RustWriterTest {
sut.toString().shouldContain("#[foo]\n/// here's an attribute")
}

@Test
fun `attributes with derive helpers must come after derives`() {
val attr = Attribute("foo", isDeriveHelper = true)
val metadata = RustMetadata(
derives = setOf(RuntimeType.Debug),
additionalAttributes = listOf(Attribute.AllowDeprecated, attr),
visibility = Visibility.PUBLIC,
)
val sut = RustWriter.root()
metadata.render(sut)
sut.toString().shouldContain("#[allow(deprecated)]\n#[derive(std::fmt::Debug)]\n#[foo]")
}

@Test
fun `deprecated attribute without any field`() {
val sut = RustWriter.root()
Expand Down