{"record":{"id":"9f8a116cfe434e4f","repo":"dotnet/efcore","slug":"non-void-label-target","errorCode":null,"errorMessage":"Non-void label target","messagePattern":"Non-void label target","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1298,"sourceCode":"\n        Result = TranslateLabelTarget(labelTarget);\n        return labelTarget;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual IdentifierNameSyntax TranslateLabelTarget(LabelTarget labelTarget)\n    {\n        // In LINQ expression trees, label targets can have a return type (they're expressions), which means they return the last evaluated\n        // thing if e.g. they're the last expression in a block. This would require lifting out the last evaluation before the goto/break,\n        // assigning it to a temporary variable, and adding a variable evaluation after the label.\n        if (labelTarget.Type != typeof(void))\n        {\n            throw new NotImplementedException(\"Non-void label target\");\n        }\n\n        // We did a processing pass on the block's labels, so any labels should already be found in our label stack frame\n        return IdentifierName(_stack.Peek().Labels[labelTarget]);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual TypeSyntax Generate(Type type)\n        => TryGenerate(type, out var result)\n            ? result\n            : throw new NotSupportedException(DesignStrings.UnableToTranslateType(type.DisplayName(fullName: false)));\n\n    /// <summary>","sourceCodeStart":1280,"sourceCodeEnd":1316,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1280-L1316","documentation":"TranslateLabelTarget throws NotImplementedException for any label target whose Type is not void. LINQ label targets can carry a return type (the value a block evaluates to when control reaches the label), but C# labels cannot return values, so faithfully translating this would require hoisting the last evaluation into a temp before the goto and reading it after the label — logic the translator does not implement.","triggerScenarios":"An expression tree uses a non-void label target such as Expression.Label(typeof(int), \"name\"). Occurs in value-returning goto-based control flow built programmatically.","commonSituations":"Custom expression trees that model computed-goto with return values; rarely if ever from compiler-generated query lambdas.","solutions":["Use void label targets: Expression.Label(typeof(void)).","Restructure so the value is written to a separate ParameterExpression before the goto and read after the label.","Avoid value-returning label control flow inside precompiled query / model lambdas."],"exampleFix":"// before\nvar ret = Expression.Label(typeof(int), \"ret\");\n// after\nvar result = Expression.Parameter(typeof(int), \"result\");\nvar ret = Expression.Label(typeof(void), \"ret\");","handlingStrategy":"validation","validationCode":"public sealed class TypedLabelDetector : ExpressionVisitor {\n    public bool Found;\n    protected override Expression VisitLabel(LabelExpression l) { if (l.Target?.Type is { } t && t != typeof(void)) Found = true; return l; }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use void label targets only.","Route returned values through a separate ParameterExpression.","Avoid value-returning label control flow in compiled lambdas."],"tags":["ef-core","linq","expression-tree","control-flow"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}