Skip to content

Consolidate expression-form context restriction diagnostics - #55614

Open
BillWagner wants to merge 3 commits into
dotnet:mainfrom
BillWagner:expression-form
Open

Consolidate expression-form context restriction diagnostics#55614
BillWagner wants to merge 3 commits into
dotnet:mainfrom
BillWagner:expression-form

Conversation

@BillWagner

@BillWagner BillWagner commented Aug 20, 2026

Copy link
Copy Markdown
Member

Closes #55339

Summary

Consolidates five compiler diagnostics related to invalid expression contexts into a single reference article: expression-form-restrictions.md.

Included Diagnostics

Code Message
CS8115 A throw expression is not allowed in this context.
CS8185 A declaration is not allowed in this context.
CS8209 A value of type 'void' may not be assigned.
CS8310 Operator '{0}' cannot be applied to operand '{1}'
CS8312 Use of default literal is not valid in this context

Changes

  • Created docs/csharp/language-reference/compiler-messages/expression-form-restrictions.md with content organized by remediation strategy:

    1. Throw expressions in expression contexts
    2. Declaration expressions in restricted contexts
    3. Type binding for default literals and typeless expressions
    4. Void-returning expression restrictions
  • Updated docs/csharp/language-reference/toc.yml to add TOC entry after "Lambda expressions"

  • Removed all five codes from docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md

Related Diagnostics

CS8188 (An expression tree may not contain a throw-expression) remains in expression-tree-restrictions.md per issue #55339 scope guidance.

Exhaustive Follow-up Search

A comprehensive search of all 194 catch-all diagnostics and Roslyn source identified four additional candidates fitting a related but distinct theme (varargs __arglist restrictions: CS1636, CS1669, CS8362, CS8378). Per issue triage, these have been deferred as a separate future varargs-theme consolidation. Candidates CS8081, CS8082, and CS8092 were classified as orthogonal (nameof syntax, parse-level errors) and excluded.

Standalone Diagnostic Article Consolidation Evaluation

Scope: Enumerating existing standalone CS????.md articles for potential merge into this theme.

Findings:

  • 514 total standalone diagnostic articles evaluated across docs/csharp/language-reference/compiler-messages/ and docs/csharp/misc/
  • 14 semantic candidates identified via keyword matching ("not allowed in context", "not valid in context", "cannot be applied", etc.)
  • 0 articles qualify for consolidation into expression-form-restrictions.md

Candidate Categories & Rationale for Exclusion:

  • Operator type incompatibility (CS0019, CS0023) — belong in operator overloading docs
  • Property/indexer accessors (CS0154, CS0271, CS0272) — belong in property/access-modifier docs
  • Keyword context restrictions (CS0175, CS0186, CS0119, CS1547) — belong in feature-specific docs (inheritance, iteration, type semantics)
  • By-reference returns (CS8156 + 20 more in 814x–817x range) — separate feature family; belong in ref-return docs
  • Parse/build errors (CS0726, CS1001, CS1583, CS1639) — metadata/build validation; not compiler expression diagnostics

Conclusion: The five-code article captures a unique, non-overlapping thematic scope: expressions whose syntactic form is prohibited in specific semantic contexts. Keeping the narrowly scoped five-code article aligns with the goal of shrinking article footprint through coherent thematic consolidation rather than forced merges.

Verification

  • ✅ Independent Fact Checker approval: all five Roslyn messages verified; code-set equality across front matter, master list, and TOC confirmed; YAML valid; no trailing whitespace
  • ✅ Thematic organization by remediation path
  • ✅ All anchors resolve; no anchor-tbd placeholders
  • ✅ Merged-content requirements met (no useful content discarded)
  • ✅ Standalone article consolidation search complete: 0 candidates identified

Notes for Reviewers

This is the first consolidation under the expression-form theme. The article is complete and narrowly scoped to expression-form context restrictions triggered directly during binding and compilation, not ref-safety or escape-scope analysis.


Internal previews

File Preview link
.openpublishing.redirection.csharp.json Preview published page
docs/csharp/language-reference/compiler-messages/expression-form-restrictions.md Preview published page
docs/csharp/language-reference/toc.yml Preview published page
docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md Preview published page

…8115, CS8185, CS8209, CS8310, CS8312)

This article consolidates five compiler diagnostics related to invalid expression contexts:
- CS8115: Throw expressions in restricted contexts
- CS8185: Declaration expressions in restricted contexts
- CS8209: Void-returning expression restrictions
- CS8310: Operator binding for null/default/new
- CS8312: Default literal target type requirements

Content organized by remediation strategy. Codes removed from catch-all.
CS8188 preserved in expression-tree-restrictions.md per issue guidance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 20, 2026 14:59
@BillWagner
BillWagner requested a review from a team as a code owner August 20, 2026 14:59
@dotnetrepoman dotnetrepoman Bot added this to the August 2026 milestone Aug 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request consolidates several C# compiler diagnostics about invalid expression forms/contexts into a single reference article, and wires the new article into the C# language reference navigation while removing the consolidated codes from the catch-all diagnostics page.

Changes:

  • Added a new consolidated compiler-messages article: expression-form-restrictions.md.
  • Added a TOC entry for the new article under C# language reference compiler messages.
  • Removed the consolidated diagnostic codes from the catch-all “sorry we don’t have specifics” page.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md Removes the consolidated diagnostic codes from the catch-all diagnostics list.
docs/csharp/language-reference/toc.yml Adds a TOC entry for the consolidated “Expression-form restrictions” article.
docs/csharp/language-reference/compiler-messages/expression-form-restrictions.md Introduces the consolidated guidance article for CS8115, CS8185, CS8209, CS8310, and CS8312.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +38 to +44
The compiler permits throw expressions only in specific contexts where an expression can appear and the exception is immediately propagated. Move the `throw` expression to a valid context, such as:

- A conditional arm of a ternary or switch expression (where the throw is one of the arms).
- An assignment to evaluate the throw in place of the assigned value.
- An argument to a method call where throwing is appropriate.
- A statement in a lambda or local function body (not within a method that must return a value).

BillWagner and others added 2 commits August 20, 2026 11:54
…context restrictions (CS0175, CS0186, CS1547)

Consolidate three standalone diagnostic articles (CS0175, CS0186, CS1547)
into expression-form-restrictions.md, broadening the article scope from 5
to 8 codes while maintaining thematic coherence:

MERGED CODES:
- CS0175: Use of keyword 'base' is not valid in this context
- CS0186: Use of null is not valid in this context
- CS1547: Keyword 'void' cannot be used in this context

CHANGES:
- Updated expression-form-restrictions.md with new 'Keyword and literal
  context restrictions' section containing substantive guidance from the
  three deleted standalone articles
- Merged all unique remediation information and examples
- Updated front matter f1_keywords and helpviewer_keywords (8 codes)
- Updated master error list with all 8 codes and exact Roslyn messages
- Updated TOC displayName with all 8 codes
- Removed old TOC entries for CS0175, CS0186, CS1547
- Created three redirects from old paths to new destination anchors
- Deleted three now-retired standalone files

FOOTPRINT REDUCTION: 3 standalone files → 1 consolidated article

VERIFICATION:
- YAML front matter valid
- All 8 codes present in f1_keywords/helpviewer_keywords
- No trailing whitespace
- Redirect JSON valid (1415 total entries)
- Exact Roslyn messages preserved per Bill's requirements

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…OC entry

The displayName folded scalar contained a duplicated five-code tail
(CS8115, CS8185, CS8209, CS8310, CS8312 with associated phrases) left
over from the original commit after the eight-code expansion appended
new content without removing the old suffix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C# diagnostics] - Expression-form context restrictions

2 participants