Repository navigation
Read attribute and convention arguments as their constant values - #110
Merged
Merged
Conversation
Order, Using, Members and Lifetime were parsed from the source text of the argument. A constant declared elsewhere, a digit separator or a fully qualified enum member then fell back to the default with no diagnostic. The [DependencyModule] flags were compared with the text "true". ConstantArgumentReader now reads each value with GetConstantValue, and the enum values arrive as their underlying numbers. A convention call with an argument that is not a constant is now refused with DM0009, as the namespace calls already were. Before, WithName, WithoutName and the environment calls dropped that argument, so IfEnvironmentValue(key, "on") tested the key "on". Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Closes #74. Closes #75.
The generator now reads attribute and convention arguments with
GetConstantValue, not from their source text. A constant declared elsewhere, a digit separator and a fully qualified enum member now give the same result as the literal. A convention call with an argument that is not a constant is refused with DM0009.#74: arguments read as source text
The new
ConstantArgumentReaderreads each value from the semantic model. The generator does not reference the runtime assembly, so an enum argument arrives as its underlying number. The reader maps it to the generator's own enum:RegistrationTypehas the same member order in both.ServiceLifetimenumbers its members in the reverse order ofServiceLifestyle.InterceptedMemberKindshas the flag values ofInterceptedMembers.It now reads these arguments:
OrderandUsingon the service attributes and[CrossWireService]Usingon[DependencyModule], and the convention callUsing(...)Orderon[Decorator]OrderandMemberson[Intercept]The same text parsing was also in places the issue does not list. These now use the reader too:
Lifetimeon[CrossWireService]and on[Intercept]. A constant fell back toSingleton, or matched only when its name happened to beScopedorTransient.[DependencyModule]:OnlyRealm,GenerateAttribute,RegisterJsonSerializersandGenerateFactorieswere compared with the text"true", andGenerateUseMethodtook the text with its quotes removed.GetLifestyle(string)inServiceModelUtilityhas no callers now, so it is removed.BaseSourceGenerator.GetRegistrationType(string)stays, because theDependencyModules_RegistrationTypebuild property is text.An attribute argument is always a constant, or the compiler reports an error. So the attribute path adds no diagnostic of its own. In a convention,
Using(...)with a value that is not a constant now gets DM0009. Before,GetRegistrationTypenever returned null, so that DM0009 could not be reported.#75: convention arguments that are not constant
ReadPatternsnow stops at the first argument that is not a constant string.WithName,WithoutNameand the environment calls then refuse the whole call with DM0009, and the message names the argument, for example'Keys.Feature' in 'IfEnvironmentValue' is not a compile-time constant. This is how the namespace calls already worked. Before, the argument was dropped, soIfEnvironmentValue(Keys.Feature, "on")tested the keyon.Public API
The
DependencyModules.SourceGeneratorsnapshot addsConstantArgumentReader.Docs
The warnings for #74 and #75 are on
docs-ste-rewrite, inservices.md,decorators.md,interception.mdandconventions.md. They will be removed in one docs pass after the fixes.Verified
ConstantArgumentTests. Where the issue is about behavior, they compile the generated code and resolve from it: registration order,Try, decorator nesting, which wrapper is outermost, and a call counter forMembers. 11 fail on main and pass with this change. The other one checks aconstconvention argument, which already worked.dotnet csharpier check .is clean.git merge-treeshows that this branch merges cleanly with Put ExcludeFromCodeCoverage on generated members only, and hide Internal members with EditorBrowsable #104, Make [CrossWireService] compile and run for keys, every registration type, and inherited interfaces #105, Read a module's parameters and equality from its own members, and skip modules DM0003 or DM0017 rejects #106, Register a service class from its own attributes only #107, Intercept only the registration the intercepted class makes #108 andfix/decorators.scripts/coverage.shorscripts/verify-packages.shlocally. CI runs both.🤖 Generated with Claude Code