MCPcopy Index your code
hub / github.com/MadAppGang/dingo / generateArmBodyWithAssignment

Method generateArmBodyWithAssignment

pkg/codegen/match.go:965–1011  ·  view source on GitHub ↗

generateArmBodyWithAssignment generates the body of a match arm with assignment to varName. Handles variable bindings, guards, and assignment generation.

(arm *ast.MatchArm, varName string, bindings []Binding)

Source from the content-addressed store, hash-verified

963// generateArmBodyWithAssignment generates the body of a match arm with assignment to varName.
964// Handles variable bindings, guards, and assignment generation.
965func (g *MatchCodeGen) generateArmBodyWithAssignment(arm *ast.MatchArm, varName string, bindings []Binding) {
966 // Generate variable bindings
967 for _, binding := range bindings {
968 bindingCode := binding.Name + " := "
969
970 // If binding has a field path, use type assertion variable
971 if binding.FieldPath != "" {
972 // Use scrutineeTempVar (e.g., "v") set in generateMatchSwitchWithAssignment
973 tempVar := g.scrutineeTempVar
974 if tempVar == "" {
975 tempVar = "v" // Fallback if not set
976 }
977 bindingCode += tempVar + "." + binding.FieldPath
978 } else {
979 // Bind to scrutinee temp var (for variable patterns in default case)
980 // Use scrutinee temp var to avoid double evaluation
981 tempVar := g.scrutineeTempVar
982 if tempVar == "" {
983 // Fallback: generate expression (shouldn't happen in normal flow)
984 scrutineeResult := GenerateExpr(g.Match.Scrutinee)
985 bindingCode += string(scrutineeResult.Output)
986 } else {
987 bindingCode += tempVar
988 }
989 }
990
991 bindingCode += "\n"
992 g.Write(bindingCode)
993 }
994
995 // Generate guard check if present
996 if arm.Guard != nil {
997 guardResult := GenerateExpr(arm.Guard)
998 g.Write("if " + string(guardResult.Output) + " {\n")
999 }
1000
1001 // Generate body with assignment
1002 bodyResult := GenerateExpr(arm.Body)
1003 g.Write(fmt.Sprintf("\t%s = %s\n", varName, string(bodyResult.Output)))
1004
1005 // Close guard if present
1006 if arm.Guard != nil {
1007 g.Write("}\n")
1008 // NO PANIC: Guarded patterns are handled by exhaustiveness checking
1009 // The guard failure simply means this case doesn't match, so try next case
1010 }
1011}
1012
1013// checkExhaustiveness validates exhaustiveness for expression matches.
1014// Returns error result if non-exhaustive; otherwise returns empty result.

Calls 2

GenerateExprFunction · 0.85
WriteMethod · 0.45

Tested by

no test coverage detected