Repository: apple/swift
File: lib/ClangImporter/SwiftDeclSynthesizer.cpp (C++)

Code before:
      LookUpConformanceInModule());
  ConcreteDeclRef concreteDeclRef(reinterpretCast, subMap);
  auto reinterpretCastRef =
      new (ctx) DeclRefExpr(concreteDeclRef, DeclNameLoc(), /*implicit*/ true);
  // FIXME: Verify ExtInfo state is correct, not working by accident.
  FunctionType::ExtInfo info;
  reinterpretCastRef->setType(
      FunctionType::get({FunctionType::Param(rawTy)}, enumTy, info));

  auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {paramRef});
  auto reinterpreted =
      CallExpr::createImplicit(ctx, reinterpretCastRef, argList);
  reinterpreted->setType(enumTy);
  reinterpreted->setThrows(nullptr);

  auto assign = new (ctx) AssignExpr(selfRef, SourceLoc(), reinterpreted,
                                     /*implicit*/ true);
  assign->setType(TupleType::getEmpty(ctx));

  // Check if the enum is frozen (closed). If so, we need to validate
  // that the raw value is one of the declared cases.
  bool isFrozen = enumDecl->getAttrs().hasAttribute<FrozenAttr>();

  if (isFrozen) {
    // For frozen enums, generate a switch statement to validate the raw value
    SmallVector<CaseStmt *, 8> cases;

    // Collect all case labels for valid enum values
    SmallVector<CaseLabelItem, 8> validCaseLabels;
    for (auto *elt : enumDecl->getAllElements()) {
      // Get the raw value literal for this element
      auto rawValueExpr = elt->getStructuralRawValueExpr();
      if (!rawValueExpr)
        continue;

      // Clone the raw value expression for pattern matching
      auto *litExpr = cloneRawLiteralExpr(ctx, cast<LiteralExpr>(rawValueExpr));
      auto *litPat = ExprPattern::createImplicit(ctx, litExpr, ctorDecl);

      // Add to the list of valid case labels
      validCaseLabels.push_back(CaseLabelItem(litPat));
    }

    // Create a single case statement with all valid raw values
    // All valid values perform the same action: reinterpret cast
    auto *caseBody = BraceStmt::create(ctx, SourceLoc(), {assign},
                                       SourceLoc(), /*implicit*/ true);
    auto *validCase = CaseStmt::createImplicit(ctx, CaseParentKind::Switch,
                                               validCaseLabels, caseBody);
    cases.push_back(validCase);


Review comment (suggestion):
You now only ever push a constant number of cases, so this can be shrunk:

```suggestion
    SmallVector<CaseStmt *, 2> cases;
```

Using a `SmallVector` here is actually unnecessary because you don't even need to allocate, so you could consider just using a fixed-size array, but I'm not sure what `SwitchStmt::create()` expects/is able to take. IMO the cleanest would be to eliminate `cases` altogether and pass `{validCase, defaultCase}` directly, but I'm not sure whether that will or if you'll need to stack-allocate some array first.

Code after:
      new (ctx) DeclRefExpr(concreteDeclRef, DeclNameLoc(), /*implicit*/ true);
  // FIXME: Verify ExtInfo state is correct, not working by accident.
  FunctionType::ExtInfo info;
  reinterpretCastRef->setType(
      FunctionType::get({FunctionType::Param(rawTy)}, enumTy, info));

  auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {paramRef});
  auto reinterpreted =
      CallExpr::createImplicit(ctx, reinterpretCastRef, argList);
  reinterpreted->setType(enumTy);
  reinterpreted->setThrows(nullptr);

  auto assign = new (ctx) AssignExpr(selfRef, SourceLoc(), reinterpreted,
                                     /*implicit*/ true);
  assign->setType(TupleType::getEmpty(ctx));

  // Check if the enum is frozen (closed). If so, we need to validate
  // that the raw value is one of the declared cases.
  bool isFrozen = enumDecl->getAttrs().hasAttribute<FrozenAttr>();

  if (isFrozen) {
    // For frozen enums, generate a switch statement to validate the raw value.

    // Collect all case labels for valid enum values
    SmallVector<CaseLabelItem, 8> validCaseLabels;
    for (auto *elt : enumDecl->getAllElements()) {
      // Get the raw value literal for this element
      auto rawValueExpr = elt->getStructuralRawValueExpr();
      if (!rawValueExpr)
        continue;

      // Clone the raw value expression for pattern matching
      auto *litExpr = cloneRawLiteralExpr(ctx, cast<LiteralExpr>(rawValueExpr));
      auto *litPat = ExprPattern::createImplicit(ctx, litExpr, ctorDecl);

      // Add to the list of valid case labels
      validCaseLabels.push_back(CaseLabelItem(litPat));
    }

    // Create a single case statement with all valid raw values
    // All valid values perform the same action: reinterpret cast
    auto *caseBody = BraceStmt::create(ctx, SourceLoc(), {assign},
                                       SourceLoc(), /*implicit*/ true);
    auto *validCase = CaseStmt::createImplicit(ctx, CaseParentKind::Switch,
                                               validCaseLabels, caseBody);

    // Create default case that returns nil
    auto *defaultPattern = AnyPattern::createImplicit(ctx);
    auto defaultLabelItem = CaseLabelItem(defaultPattern);

    auto *failStmt = new (ctx) FailStmt(SourceLoc(), SourceLoc(), /*implicit*/ true);