Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 8, 2025

Description

Adds a new compiler warning (FS3879) that detects when triple-slash XML documentation comments (///) are not positioned as the first non-whitespace text on a line. This helps developers identify misplaced XML doc comments that would be ignored or misinterpreted by documentation tools.

Problem

F# developers sometimes write XML documentation comments after code on the same line, which causes the documentation to be lost or misinterpreted:

let value = 42 /// This documentation is ignored
type MyDU = | CaseA of int /// This is also ignored

Solution

The implementation adds:

  1. New informational warning FS3879: "XML documentation comments should be the first non-whitespace text on a line." (informational by default, can be enabled with --warnon:3879)

  2. Token position tracking in LexFilter: Tracks the line number of the last non-comment/non-whitespace token using the existing token stream in LexFilter.fs, excluding LBRACE and EQUALS tokens to allow legitimate record type patterns

  3. XmlDocCollector enhancement: Added lastNonCommentTokenLine field to track when non-comment tokens appear

  4. LexerStore methods: Added SetLastNonCommentTokenLine and GetLastNonCommentTokenLine to manage the tracking

  5. Warning check in lex.fsl: When a /// pattern is matched in the lexer, checks if it's on the same line as the last non-comment token and emits the informational warning

  6. F# codebase enforcement: Added FS3879 to WarnOn in FSharp.Profiles.props so the warning is enabled for the F# compiler codebase itself

Examples

Should trigger warning (when enabled):

let x = 42                  /// Bad: after code
type Foo() /// Bad: after closing paren
type MyDU = | CaseA of int /// Bad: after DU case
let x = /// Bad: after equals in expression  
  42
seq { /// Bad: after brace in expression
  yield 1
}

No warning (correct usage):

/// Good: proper documentation
let x = 42

module Test =
    /// Good: properly indented  
    let y = 43
    
type MyDU =
    /// Good: before DU case
    | CaseA of int

type MyRecord = {
    /// Good: field documentation in record type
    Field1: int
}

Implementation Details

This implementation uses the LexFilter + lex.fsl approach:

  • Leverages existing token stream tracking in runWrappedLexerInConsistentLexbufState
  • Stores the line number of the last non-comment/non-whitespace token in XmlDocCollector after each token is processed
  • Excludes LBRACE ({) and EQUALS (=) tokens to allow legitimate record type documentation patterns while still catching expression context misuse
  • Checks for /// in lex.fsl where the lexer specifically matches the "///" pattern
  • Emits informational warning FS3879 when /// appears on same line as code (consistent with other XML doc warnings)
  • The warning is enabled in the F# codebase via WarnOn in FSharp.Profiles.props but remains informational for external users
  • This approach properly tracks all token types and has direct access to the comment text

Additional Fixes

  • Fixed src/Compiler/Checking/infos.fs where /// was incorrectly used as a regular comment (changed to //)
  • Fixed tests/AheadOfTime/Trimming/Program.fs where /// was incorrectly used as a regular comment (changed to //)
  • Fixed vsintegration/src/FSharp.LanguageService/Intellisense.fs where /// was incorrectly used for inline parameter comments (changed to //)
  • Suppressed FS3879 warning in AOT trimming test projects (they have TreatWarningsAsErrors enabled but don't require XML documentation)
  • Updated XmlDocTests with --warnon:3879 to enable the informational warning for testing
  • Updated test expectations to expect Warning 3879 (promoted by --warnon)
  • Updated neg45.bsl baseline to include all three FS3879 warnings (lines 89, 97, and 102)
  • Added test for Discriminated Union cases with misplaced XML doc comments
  • Added test for XML doc comments after opening brace in record types
  • Added test for XML doc comments after equals in expression context
  • Added test for XML doc comments after brace in seq expression context
  • Removed no-op match statement in LexFilter.fs

Documentation

  • Created .github/skills/creating-skills/SKILL.md - Meta-skill guide with de-duplication checks, SEO-optimized descriptions for LLM discoverability, and token efficiency practices
  • Created .github/skills/hypothesis-driven-debugging/SKILL.md - Systematic debugging methodology with mandatory build/test verification

Test Results

XmlDocTests

  • Total tests: 63
  • Passed: 58
  • Failed: 0
  • Skipped: 5
  • Time: 2 seconds

Build Results

  • Command: ./build.sh -c Release
  • Time: 18.5s (incremental)
  • Errors: 0
  • Warnings: 0

Checklist

  • Test cases added (including DU case, record type case, expression context cases)
  • All XmlDocTests passing (58/58 non-skipped tests)
  • Performance benchmarks added in case of performance changes
  • Release notes entry updated (moved to 10.0.200.md)
  • GitHub Copilot Agent Skills created and refined per review feedback
  • Warning changed to informational as requested
  • Warning enabled in F# codebase via FSharp.Profiles.props
  • Fixed invalid XML doc comments in Intellisense.fs
  • Removed no-op match statement per code review
  • Added tests for equals and brace in expression contexts per code review

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Warn on triple-slash /// doc comments not being first text on line (lexer approach) Add warning for XML documentation comments not positioned as first non-whitespace on line Sep 8, 2025
Copilot AI requested a review from T-Gro September 8, 2025 12:34
Copy link
Member

@T-Gro T-Gro left a comment

Choose a reason for hiding this comment

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

@copilot :

You are correctly getting a build failure for src/FSharp.Core/Query.fs , Query.fs at line 1331.

This is a valid XML doc comment, but your approach is utter rubbish.
It must check for being the first non-whitespace token irrespective on any magic hausnumero, you cannot just put a clumsy > 16 in there!

Analyze available data in the lexbuf LexBuffer and make it work without hardcoding a magic number.

Have a look at where LexerStore.SaveXmlDocLine is called, this should give you a hint perharps.

Copilot AI changed the title Add warning for XML documentation comments not positioned as first non-whitespace on line [WIP] Add warning for XML documentation comments not positioned as first non-whitespace on line Sep 8, 2025
Copilot AI requested a review from T-Gro September 8, 2025 14:56
…orarily pending proper line analysis implementation

Co-authored-by: T-Gro <[email protected]>
@dotnet dotnet deleted a comment from Copilot AI Sep 19, 2025
@dotnet dotnet deleted a comment from github-actions bot Sep 19, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 19, 2025

⚠️ Release notes required, but author opted out

Warning

Author opted out of release notes, check is disabled for this pull request.
cc @dotnet/fsharp-team-msft

@dotnet dotnet deleted a comment from Copilot AI Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
@dotnet dotnet deleted a comment from Copilot AI Jan 12, 2026
@dotnet dotnet deleted a comment from Copilot AI Jan 12, 2026
@dotnet dotnet deleted a comment from Copilot AI Jan 12, 2026
@dotnet dotnet deleted a comment from Copilot AI Jan 12, 2026
@dotnet dotnet deleted a comment from abonie Jan 12, 2026
abonie
abonie previously requested changes Jan 12, 2026
…ions

Addressed review feedback:
1. Removed no-op match statement at line 2642 in LexFilter.fs
2. Added test cases for XML doc comments after equals and brace in expression contexts
3. Tests verify that FS3879 warning is generated for `let x = ///` and `seq { ///` patterns
4. Both tests also expect FS3520 informational message (XML comment not on valid element)

The current LBRACE/EQUALS exclusion works correctly:
- In record types like `type R = { /// field`, the exclusion prevents false positives
- In expressions like `let x = ///`, warnings ARE still generated because the preceding identifier updates the tracker

Build: 18.5s, 0 errors
Tests: 63 total, 58 passed (includes 2 new tests), 5 skipped

Co-authored-by: abonie <[email protected]>
auto-merge was automatically disabled January 12, 2026 20:33

Head branch was pushed to by a user without write access

@T-Gro T-Gro enabled auto-merge (squash) January 13, 2026 08:59
@T-Gro T-Gro merged commit e054f5c into main Jan 13, 2026
40 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in F# Compiler and Tooling Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

NO_RELEASE_NOTES Label for pull requests which signals, that user opted-out of providing release notes

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants