You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IDE0340 "Use unbound generic type" breaks the build when nameof goes through a member typed by a type parameterΒ #86572
Build with dotnet_diagnostic.IDE0340.severity = warning: warning IDE0340: Use unbound generic type.
Apply the fix (dotnet format style --diagnostics IDE0340). The line becomes nameof(Holder<>.Value.Length).
Build again.
Expected Behavior: IDE0340 isn't offered, since Value has type T once the type is unbound and T has no Length.
Actual Behavior: after the fix the build fails with error CS1061: 'T' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'T' could be found.
The same happens with properties (Holder<string>.Prop.Length) and nested generics (Holder<int>.Inner<string>.Y.Length). nameof(List<int>.Count) and chains through constrained type parameters convert correctly.
Cause: CSharpUseUnboundGenericTypeInNameOfDiagnosticAnalyzer only checks the syntax: it reports for any TypeArgumentListSyntax with non-omitted arguments inside nameof, without checking that the unbound form still binds. The C# 14 proposal notes that members of an unbound type have no type information beyond object and the constraints, so the error after the fix is expected language behavior. The tests in UseUnboundGenericTypeInNameOfTests only cover List<>, Dictionary<,> and Outer<>.Inner<>.
Possible fix: bind the unbound form speculatively and only report when it binds without errors (or bail when a member access in the chain has a type that uses a type parameter of a type being unbound), plus tests for both cases. I'd be glad to send a PR if that sounds right.
Fair point, the repro is minimized, and for that exact line nameof(string.Length) is simpler. The chained form is used when the name should stay tied to a specific path, e.g. nameof(Envelope<Order>.Payload.Total) in a validation message or log key: renaming Payload or changing its type then breaks the build instead of leaving a stale string.
Either way, the issue is narrower than the pattern itself: IDE0340 (and Fix All) turns code that compiles into a CS1061 error. Not offering the diagnostic when the unbound form doesn't bind would be a small change, and I'm happy to send it. If you don't think it's worth it, closing is fine too.
Will do. I'll keep it to the analyzer: only report IDE0340 when the rewritten nameof still binds, with tests for the failing chains and for the cases that should still be offered (List<>.Count, chains through constrained type parameters).
Activity
CyrusNajmabadi commentedon Nov 22, 2026
This is very odd. Why not do
nameof(string.Length).Manish-1kumar commentedon Nov 22, 2026
Fair point, the repro is minimized, and for that exact line
nameof(string.Length)is simpler. The chained form is used when the name should stay tied to a specific path, e.g.nameof(Envelope<Order>.Payload.Total)in a validation message or log key: renamingPayloador changing its type then breaks the build instead of leaving a stale string.Either way, the issue is narrower than the pattern itself: IDE0340 (and Fix All) turns code that compiles into a CS1061 error. Not offering the diagnostic when the unbound form doesn't bind would be a small change, and I'm happy to send it. If you don't think it's worth it, closing is fine too.
CyrusNajmabadi commentedon Nov 22, 2026
depends on the fix. can you make one. if ti's very narrow in terms of what it does, and it solves this, i'm ok with it :)
Manish-1kumar commentedon Nov 22, 2026
Will do. I'll keep it to the analyzer: only report IDE0340 when the rewritten
nameofstill binds, with tests for the failing chains and for the cases that should still be offered (List<>.Count, chains through constrained type parameters).