Repository navigation
Parse .api files back into the model - #95
Merged
Merged
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Step 2 of the public API files:
ApiParser.Parsereads a.apifile back into theApiAssemblythatApiReaderproduced from the assembly.ApiDocument.LineOf(node)gives the line each type and member is on, for the path reports.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, DayOfWeeklists the value types from other assemblies. Without it,DateTime?(aNullable<T>) andstring?(an annotated reference) can't be told apart.using interface System: IComparable<>, IDisposablelists the interfaces that a base list names, so the base class can be told from the interfaces without the other assembly.System.Threading.Tasks.Taskresolves without guessing where the namespace ends.[IndexerName("Chars")]is written for an indexer that isn't calledItem.refreturn and its return attributes are written. An interface member that is neither abstract nor virtual readssealed … { … }.The snapshot changes are only in those header lines.
Tests
ParserTestscover 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:ModelDumplists every field of both models and compares them line by line. That includes value types,Nullable<T>versus annotatedT?, 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 whenTis a struct, and ignoringusing 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
Invokeattributes.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