Skip to content

Parse .api files back into the model - #95

Merged
Mpdreamz merged 1 commit into
mainfrom
api-parser
Nov 14, 2026
Merged

Mpdreamz merged 1 commit into
mainfrom
api-parser

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

Step 2 of the public API files: ApiParser.Parse reads a .api file back into the ApiAssembly that ApiReader produced from the assembly.

  • ApiDocument.LineOf(node) gives the line each type and member is on, for the path reports.
  • A file the parser can't read throws ApiParseException, with the line and column.

What the file now says

Parsing needs everything the reader knew to be in the text. So the writer changes in five ways:

  • using struct System: DateTime, DayOfWeek lists the value types from other assemblies. Without it, DateTime? (a Nullable<T>) and string? (an annotated reference) can't be told apart.
  • using interface System: IComparable<>, IDisposable lists the interfaces that a base list names, so the base class can be told from the interfaces without the other assembly.
  • The header lists every external type, the fully qualified ones too. Then System.Threading.Tasks.Task resolves without guessing where the namespace ends.
  • [IndexerName("Chars")] is written for an indexer that isn't called Item.
  • Delegates and interface members: a delegate's ref return and its return attributes are written. An interface member that is neither abstract nor virtual reads sealed … { … }.

The snapshot changes are only in those header lines.

Tests

ParserTests cover the samples and six framework assemblies: CoreLib, System.Collections.Immutable, System.Text.Json, System.Linq, System.Net.Http and System.Memory. For each one they require two things:

  • The same text: parsing and writing again gives the identical file. CoreLib's is 35,751 lines.
  • The same model: ModelDump lists every field of both models and compares them line by line. That includes value types, Nullable<T> versus annotated T?, base class versus interface, constraints, accessors, defaults and attributes.

Two mutations make the suite fail, so it does catch real breakage: reading T? as an annotation even when T is a struct, and ignoring using interface.

What isn't round-tripped is the version, the public key token and the target framework, which the file doesn't hold, and a delegate's Invoke attributes.

The plan said CsCheck for the round trip. I used the six framework assemblies instead, which cover tens of thousands of real declarations. A generator of random APIs would mostly repeat what they already exercise.

🤖 Generated with Claude Code

ApiParser reads a .api file into the ApiAssembly the reader produced from the assembly, with the line each type
and member is on for reports, and an ApiParseException with line and column for a file it cannot read.

For that the file has to say what the reader knew, so the writer's header now lists every type from another
assembly, the value types in a "using struct" line and the interfaces a base list names in a "using interface"
line. An indexer that is not called Item is written with [IndexerName], a delegate's ref return and return
attributes are written, and an interface member that is neither abstract nor virtual reads "sealed ... { … }".

ParserTests require the same text and, field by field, the same model for the samples and six framework
assemblies, CoreLib's 35,751 lines among them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mpdreamz Mpdreamz added the feature A new capability (minor bump) label Nov 14, 2026
@Mpdreamz
Mpdreamz merged commit fa2d46a into main Nov 14, 2026
6 of 7 checks passed
@Mpdreamz
Mpdreamz deleted the api-parser branch November 14, 2026 18:59
Mpdreamz added a commit that referenced this pull request Nov 14, 2026
…ound (#101)

Until now the round trip was checked against six framework assemblies
rather than generated APIs, as #95 said. This adds CsCheck properties
over generated libraries, and they found seven bugs.

## The generator

`GeneratedLibrary` writes a C# library from a sequence of numbers, one
decision per number, the way Hypothesis builds values from a choice
sequence. CsCheck generates the sequences and shrinks a failing one:
fewer and smaller numbers mean fewer types, members and features, so a
failure is reported as a small library.
- **Compiles:** every generated library compiles; a failure would show
its source.
- **Varied:** over 1,000 samples, libraries run from 8 to 94 lines with
a median of 38. Every feature in the list below shows up, the rarest
(static abstract interface members) in 44 of them.
- **Covers:** kinds and modifiers, generics with constraints and
variance, base classes and interfaces, nullability, tuples, `dynamic`,
`nint`, ref kinds, `params`, defaults, constants, `required` and `init`,
protected setters, events, operators, conversions, indexers, extension
methods, records, nested and protected types, and default and static
abstract interface members.

## The properties

| Property | Samples per run |
|---|---|
| the reader agrees with Roslyn on every visible signature, modifier,
constraint and base | 150 |
| writing and parsing gives the same text and, field by field, the same
model | 150 |
| split by namespace or top-level namespace, the files parse back to the
same model | 150 |
| an API compared with itself, or with its own `.api` file, has no
changes | 150 |
| comparing two libraries one way mirrors comparing them the other way |
60 pairs |

One soak run with 2,000 libraries per property and 800 pairs passed.
Each regular run takes about 3 seconds.

## What it found, fixed here

**Reader**
- `notnull` was missed when a type constraint sat next to it, as in
`where T : notnull, IComparable<T>`.
- A property's type took its nullability from the getter's context
instead of the type's. That showed up on indexers.
- `dynamic` was lost in an `in` parameter or a `ref readonly` return
that carries a `modreq`, because the modifier takes a flag of its own.

**Parser**
- A delegate whose return type is a tuple lost its name.

**Comparison** (found by the mirror property)
- A class that became a struct also reported its constructors changing,
and only in one direction.
- A class that became an enum compared its base types in one direction
and its underlying type in the other.
- Virtual and abstract changes were named differently depending on the
direction.

Mutation check: putting the delegate-name bug back fails 3 properties,
and putting the `dynamic` offset bug back fails 1.

`api/` is unchanged by the reader fixes; neither library hits those
cases. All 630 tool tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature A new capability (minor bump)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant