Unverified Commit bbdde4bb authored by Harry Barber's avatar Harry Barber Committed by GitHub
Browse files

Fix RustWriter debug mode (#1523)

Debug mode is useful for tracing the origin of generated code. Currently `./gradlew codegen-server-test:check` will fail with it enabled.

This is caused by the following `rustfmt` bug https://github.com/rust-lang/rustfmt/issues/5425 which causes valid rust code to break after `cargo fmt`. One instance of this pattern is generated by `withBlock("$memberName: self.$memberName", ",")` found [here](https://github.com/awslabs/smithy-rs/blob/46984ab91d027e6a4d1e312c125a850f5cb23cfd/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/BuilderGenerator.kt#L252).

To prevent such code being generated we disallow debug comment being introduced when the line is just a single `,`.
parent 1338063c
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -359,7 +359,9 @@ class RustWriter private constructor(
    }

    override fun write(content: Any?, vararg args: Any?): RustWriter {
        if (debugMode) {
        // TODO(https://github.com/rust-lang/rustfmt/issues/5425): The second condition introduced here is to prevent
        // this rustfmt bug
        if (debugMode && (content as? String?)?.let { it.trim() != "," } ?: false) {
            val location = Thread.currentThread().stackTrace
            location.first { it.isRelevant() }?.let { "/* ${it.fileName}:${it.lineNumber} */" }
                ?.also { super.writeInline(it) }