Skip to content

An expression-body callback that writes nothing renders int P => ; / get => ; (CS1525), the output Validate says it prevents #175

Closed

Description

@matt-edmondson

What's wrong

When an ExpressionBodyFactory (or the BodyFactory of an AccessorTemplate.Expression) writes no non-blank lines, TemplateRendering writes the arrow anyway and then a bare semicolon (TemplateRendering.cs#L343-L348):

codeBlocker.Write(" => ");
if (last < 0)
{
	codeBlocker.WriteLine(";");
	return;
}

AccessorTemplate.Validate says it throws for the case that "would render as get => ;" (AccessorTemplate.cs#L115-L135). It only checks for a null BodyFactory, though, so #142's fix doesn't cover a factory that runs and writes nothing.

Repro (at 21ac04c)

new PropertyTemplate { Name = "P", Type = "int", ExpressionBodyFactory = _ => { } }.WriteTo(cb);
// int P => ;

new PropertyTemplate { Name = "P", Type = "int", Getter = AccessorTemplate.Expression(_ => { }) }.WriteTo(cb);
// int P
// {
// 	get => ;
// }

A callback that writes only blank lines, for example when joining an empty list of terms, gives the same result. Neither output compiles (CS1525, invalid expression term ;), and nothing tells the generator author.

Why it matters

Block bodies handle "wrote nothing" on purpose and render { }. An expression body has no valid empty form, so the library hands back code that can never compile. The failure only shows up later, as a compile error in the consumer's generated file, far from the template call that caused it.

Suggested fix / acceptance criteria

  • Render the expression into its fragment before writing the declaration. If it has no non-blank lines, throw InvalidOperationException and write nothing, matching the null-factory case.
  • Do the same for expression accessors, and extend the AccessorTemplate.Validate and PropertyTemplate.ExpressionBodyFactory docs to cover an empty expression.
  • Tests: both repros above throw, and leave the CodeBlocker buffer unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions