Unverified Commit f9fb9e69 authored by david-perez's avatar david-perez Committed by GitHub
Browse files

Fix recursive constraint violations with paths over list and map shapes (#2371)

* Fix recursive constraint violations with paths over list and map shapes

There is a widespread assumption throughout the generation of constraint
violations that does not hold true all the time, namely, that a
recursive constraint violation graph has the same requirements with
regards to boxing as the regular shape graph.

Some types corresponding to recursive shapes are boxed to introduce
indirection and thus not generate an infinitely recursive type. The
algorithm however does not superfluously introduce boxes when the cycle
goes through a list shape or a map shape. Why list shapes and map
shapes? List shapes and map shapes get rendered in Rust as `Vec<T>` and
`HashMap<K, V>`, respectively, they're the only Smithy shapes that
"organically" introduce indirection (via a pointer to the heap) in the
recursive path. For other recursive paths, we thus have to introduce the
indirection artificially ourselves using `Box`. This is done in the
`RecursiveShapeBoxer` model transform.

However, the constraint violation graph needs to box types in recursive
paths more often. Since we don't collect constraint violations
(yet, see #2040), the constraint violation graph never holds
`Vec<T>`s or `HashMap<K, V>`s, only simple types. Indeed, the following simple
recursive model:

```smithy
union Recursive {
    list: List
}

@length(min: 69)
list List {
    member: Recursive
}
```

has a cycle that goes through a list shape, so no shapes in it need
boxing in the regular shape graph. However, the constraint violation
graph is infinitely recursive if we don't introduce boxing somewhere:

```rust
pub mod model {
    pub mod list {
        pub enum ConstraintViolation {
            Length(usize),
            Member(
                usize,
                crate::model::recursive::ConstraintViolation,
            ),
        }
    }

    pub mod recursive {
        pub enum ConstraintViolation {
            List(crate::model::list::ConstraintViolation),
        }
    }
}
```

This commit fixes things by making the `RecursiveShapeBoxer` model
transform configurable so that the "cycles through lists and maps
introduce indirection" assumption can be lifted. This allows a server
model transform, `RecursiveConstraintViolationBoxer`, to tag member
shapes along recursive paths with a new trait,
`ConstraintViolationRustBoxTrait`, that the constraint violation type
generation then utilizes to ensure that no infinitely recursive
constraint violation types get generated.

For example, for the above model, the generated Rust code would now look
like:

```rust
pub mod model {
    pub mod list {
        pub enum ConstraintViolation {
            Length(usize),
            Member(
                usize,
                std::boxed::Box(crate::model::recursive::ConstraintViolation),
            ),
        }
    }

    pub mod recursive {
        pub enum ConstraintViolation {
            List(crate::model::list::ConstraintViolation),
        }
    }
}
```

Likewise, places where constraint violations are handled (like where
unconstrained types are converted to constrained types) have been
updated to account for the scenario where they now are or need to be
boxed.

Parametrized tests have been added to exhaustively test combinations of
models exercising recursive paths going through (sparse and non-sparse)
list and map shapes, as well as union and structure shapes
(`RecursiveConstraintViolationsTest`). These tests even assert that the
specific member shapes along the cycles are tagged as expected
(`RecursiveConstraintViolationBoxerTest`).

* Address comments
parent 6cade905
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -105,14 +105,14 @@ class ClientCodegenVisitor(
            // Add errors attached at the service level to the models
            .let { ModelTransformer.create().copyServiceErrorsToOperations(it, settings.getService(it)) }
            // Add `Box<T>` to recursive shapes as necessary
            .let(RecursiveShapeBoxer::transform)
            .let(RecursiveShapeBoxer()::transform)
            // Normalize the `message` field on errors when enabled in settings (default: true)
            .letIf(settings.codegenConfig.addMessageToErrors, AddErrorMessage::transform)
            // NormalizeOperations by ensuring every operation has an input & output shape
            .let(OperationNormalizer::transform)
            // Drop unsupported event stream operations from the model
            .let { RemoveEventStreamOperations.transform(it, settings) }
            // - Normalize event stream operations
            // Normalize event stream operations
            .let(EventStreamNormalizer::transform)

    /**
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ internal class ResiliencyConfigCustomizationTest {

    @Test
    fun `generates a valid config`() {
        val model = RecursiveShapeBoxer.transform(OperationNormalizer.transform(baseModel))
        val model = RecursiveShapeBoxer().transform(OperationNormalizer.transform(baseModel))
        val project = TestWorkspace.testProject()
        val codegenContext = testCodegenContext(model, settings = project.rustSettings())

+2 −1
Original line number Diff line number Diff line
@@ -12,7 +12,8 @@ import software.amazon.smithy.model.traits.Trait
/**
 * Trait indicating that this shape should be represented with `Box<T>` when converted into Rust
 *
 * This is used to handle recursive shapes. See RecursiveShapeBoxer.
 * This is used to handle recursive shapes.
 * See [software.amazon.smithy.rust.codegen.core.smithy.transformers.RecursiveShapeBoxer].
 *
 * This trait is synthetic, applied during code generation, and never used in actual models.
 */
+62 −32
Original line number Diff line number Diff line
@@ -7,25 +7,50 @@ package software.amazon.smithy.rust.codegen.core.smithy.transformers

import software.amazon.smithy.codegen.core.TopologicalIndex
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.SetShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.transform.ModelTransformer
import software.amazon.smithy.rust.codegen.core.smithy.traits.RustBoxTrait
import software.amazon.smithy.rust.codegen.core.util.hasTrait

object RecursiveShapeBoxer {
class RecursiveShapeBoxer(
    /**
     * Transform a model which may contain recursive shapes into a model annotated with [RustBoxTrait]
     * A predicate that determines when a cycle in the shape graph contains "indirection". If a cycle contains
     * indirection, no shape needs to be tagged. What constitutes indirection is up to the caller to decide.
     */
    private val containsIndirectionPredicate: (Collection<Shape>) -> Boolean = ::containsIndirection,
    /**
     * A closure that gets called on one member shape of a cycle that does not contain indirection for "fixing". For
     * example, the [RustBoxTrait] trait can be used to tag the member shape.
     */
    private val boxShapeFn: (MemberShape) -> MemberShape = ::addRustBoxTrait,
) {
    /**
     * Transform a model which may contain recursive shapes.
     *
     * When recursive shapes do NOT go through a List, Map, or Set, they must be boxed in Rust. This function will
     * iteratively find loops & add the `RustBox` trait in a deterministic way until it reaches a fixed point.
     * For example, when recursive shapes do NOT go through a `CollectionShape` or a `MapShape` shape, they must be
     * boxed in Rust. This function will iteratively find cycles and call [boxShapeFn] on a member shape in the
     * cycle to act on it. This is done in a deterministic way until it reaches a fixed point.
     *
     * This function MUST be deterministic (always choose the same shapes to `Box`). If it is not, that is a bug. Even so
     * This function MUST be deterministic (always choose the same shapes to fix). If it is not, that is a bug. Even so
     * this function may cause backward compatibility issues in certain pathological cases where a changes to recursive
     * structures cause different members to be boxed. We may need to address these via customizations.
     *
     * For example, given the following model,
     *
     * ```smithy
     * namespace com.example
     *
     * structure Recursive {
     *     recursiveStruct: Recursive
     *     anotherField: Boolean
     * }
     * ```
     *
     * The `com.example#Recursive$recursiveStruct` member shape is part of a cycle, but the
     * `com.example#Recursive$anotherField` member shape is not.
     */
    fun transform(model: Model): Model {
        val next = transformInner(model)
@@ -37,16 +62,17 @@ object RecursiveShapeBoxer {
    }

    /**
     * If [model] contains a recursive loop that must be boxed, apply one instance of [RustBoxTrait] return the new model.
     * If [model] contains no loops, return null.
     * If [model] contains a recursive loop that must be boxed, return the transformed model resulting form a call to
     * [boxShapeFn].
     * If [model] contains no loops, return `null`.
     */
    private fun transformInner(model: Model): Model? {
        // Execute 1-step of the boxing algorithm in the path to reaching a fixed point
        // 1. Find all the shapes that are part of a cycle
        // 2. Find all the loops that those shapes are part of
        // 3. Filter out the loops that go through a layer of indirection
        // 3. Pick _just one_ of the remaining loops to fix
        // 4. Select the member shape in that loop with the earliest shape id
        // Execute 1 step of the boxing algorithm in the path to reaching a fixed point:
        // 1. Find all the shapes that are part of a cycle.
        // 2. Find all the loops that those shapes are part of.
        // 3. Filter out the loops that go through a layer of indirection.
        // 3. Pick _just one_ of the remaining loops to fix.
        // 4. Select the member shape in that loop with the earliest shape id.
        // 5. Box it.
        // (External to this function) Go back to 1.
        val index = TopologicalIndex.of(model)
@@ -58,34 +84,38 @@ object RecursiveShapeBoxer {
            // Flatten the connections into shapes.
            loops.map { it.shapes }
        }
        val loopToFix = loops.firstOrNull { !containsIndirection(it) }
        val loopToFix = loops.firstOrNull { !containsIndirectionPredicate(it) }

        return loopToFix?.let { loop: List<Shape> ->
            check(loop.isNotEmpty())
            // pick the shape to box in a deterministic way
            // Pick the shape to box in a deterministic way.
            val shapeToBox = loop.filterIsInstance<MemberShape>().minByOrNull { it.id }!!
            ModelTransformer.create().mapShapes(model) { shape ->
                if (shape == shapeToBox) {
                    shape.asMemberShape().get().toBuilder().addTrait(RustBoxTrait()).build()
                    boxShapeFn(shape.asMemberShape().get())
                } else {
                    shape
                }
            }
        }
    }
}

/**
     * Check if a List<Shape> contains a shape which will use a pointer when represented in Rust, avoiding the
     * need to add more Boxes
 * Check if a `List<Shape>` contains a shape which will use a pointer when represented in Rust, avoiding the
 * need to add more `Box`es.
 *
 * Why `CollectionShape`s and `MapShape`s? Note that `CollectionShape`s get rendered in Rust as `Vec<T>`, and
 * `MapShape`s as `HashMap<String, T>`; they're the only Smithy shapes that "organically" introduce indirection
 * (via a pointer to the heap) in the recursive path. For other recursive paths, we thus have to introduce the
 * indirection artificially ourselves using `Box`.
 *
 */
    private fun containsIndirection(loop: List<Shape>): Boolean {
        return loop.find {
private fun containsIndirection(loop: Collection<Shape>): Boolean = loop.find {
    when (it) {
                is ListShape,
                is MapShape,
                is SetShape, -> true
        is CollectionShape, is MapShape -> true
        else -> it.hasTrait<RustBoxTrait>()
    }
} != null
    }
}

private fun addRustBoxTrait(shape: MemberShape): MemberShape = shape.toBuilder().addTrait(RustBoxTrait()).build()
+1 −1
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ class InstantiatorTest {
            @required
            num: Integer
        }
    """.asSmithyModel().let { RecursiveShapeBoxer.transform(it) }
    """.asSmithyModel().let { RecursiveShapeBoxer().transform(it) }

    private val codegenContext = testCodegenContext(model)
    private val symbolProvider = codegenContext.symbolProvider
Loading