{"record":{"id":"f199a94b99652c34","repo":"dotnet/efcore","slug":"null-argument-in-visitlabeltarget","errorCode":null,"errorMessage":"Null argument in VisitLabelTarget","messagePattern":"Null argument in VisitLabelTarget","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1278,"sourceCode":"    }\n\n    /// <inheritdoc />\n    protected override Expression VisitLabel(LabelExpression label)\n    {\n        // C# labels apply on a statement, but in LINQ they can appear anywhere (i.e. last thing in a block).\n        // So we apply the label to a dummy null literal statement, which we'll filter out of the block in statement context anyway.\n        Result = LabeledStatement(\n            TranslateLabelTarget(label.Target).Identifier.Text,\n            ExpressionStatement(LiteralExpression(SyntaxKind.NullLiteralExpression)));\n        return label;\n    }\n\n    /// <inheritdoc />\n    protected override LabelTarget VisitLabelTarget(LabelTarget? labelTarget)\n    {\n        if (labelTarget is null)\n        {\n            throw new NotImplementedException(\"Null argument in VisitLabelTarget\");\n        }\n\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))","sourceCodeStart":1260,"sourceCodeEnd":1296,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1260-L1296","documentation":"VisitLabelTarget throws NotImplementedException when called with a null LabelTarget. The translator must emit a concrete C# label identifier, and with no target there is nothing to name, so it bails. The C# compiler never produces null label targets, so this indicates a malformed or hand-built tree.","triggerScenarios":"An expression tree contains a LabelExpression, GotoExpression, LoopExpression, or SwitchExpression whose Target/ContinueLabel/BreakLabel reference is null when visited; or VisitLabelTarget is invoked directly on null. This comes from programmatic Expression construction, e.g. Expression.Label(null) or building gotos with null targets.","commonSituations":"Hand-built control-flow trees passed to the precompiled-query generator; expression rewriters that replace label targets with null; integration code that assembles loops/gotos dynamically.","solutions":["Always construct labels with a real target: Expression.Label(typeof(void), \"name\").","Audit any programmatic expression-tree assembly for null LabelTarget references before precompiling.","Avoid hand-built goto/label control flow inside EF Core precompiled query lambdas; restructure as conditionals or method calls."],"exampleFix":"// before\nvar badLabel = Expression.Label(null); // Target is null -> throws\n// after\nvar label = Expression.Label(typeof(void), \"done\");","handlingStrategy":"validation","validationCode":"// Verify every Label/Goto/Loop target is non-null\npublic sealed class NullLabelDetector : ExpressionVisitor {\n    public bool Found;\n    protected override Expression VisitLabel(LabelExpression l) { if (l.Target is null) Found = true; return l; }\n    protected override Expression VisitGoto(GotoExpression g) { if (g.Target is null) Found = true; return g; }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always construct labels with Expression.Label(typeof(void), \"name\").","Never pass null as a LabelTarget.","Validate hand-built control-flow trees before precompiling."],"tags":["ef-core","linq","expression-tree","control-flow"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}