Fix CodeWriter field dedup to update references via MemberExpression#10134
Fix CodeWriter field dedup to update references via MemberExpression#10134
Conversation
When CodeWriter deduplicates field identifiers (e.g., appending "0" to avoid name collisions), MemberExpression now uses the CodeWriterDeclaration to resolve the actual name instead of the original string. This ensures that references through FieldProvider.AsValueExpression use the deduped name consistently with the declaration. Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/12da064c-553f-40a0-a78f-c68d46f80fba
...ges/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/FieldProvider.cs
Show resolved
Hide resolved
|
@copilot something is not right here. The generated collectionresults definition is incorrectly referencing a field that doesn't exist Azure/azure-sdk-for-net#57468 |
…tions When MemberExpression.Write() used writer.Append(Declaration), the CodeWriter.Append method would call WriteDeclaration (registering a new deduped name) if the field hadn't been declared in the current scope yet. This caused field references like _token to become _token0. Added CodeWriter.AppendDeclarationReference() which resolves the deduped name when the declaration exists in scope, but falls back to the requested name otherwise (never creates new declarations). Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/45a95dc7-febe-41a8-b56b-b0aad13b473f
Fixed in 2504336. The root cause was that Added |
| } | ||
| } | ||
|
|
||
| internal void AppendDeclarationReference(CodeWriterDeclaration declaration) |
There was a problem hiding this comment.
@copilot this looks almost identical to the existing Append method. We should just update the existing method with an optional bool parameter
CodeWriterDeclarationdedup (e.g._scope→_scope0) only applied to the declaration site. References throughFieldProvider.AsValueExpressionwrote the original name viaAppendRaw(MemberName), bypassing the declaration's actual name entirely.Changes
MemberExpression: Added optionalCodeWriterDeclaration?property.Write()delegates towriter.AppendDeclarationReference(Declaration)when present, which resolves the actual deduped name via scope lookup without accidentally creating new declarations.CodeWriter.AppendDeclarationReference(): New method that resolves a declaration's deduped name when it exists in scope, but falls back toRequestedNameotherwise. UnlikeAppend(CodeWriterDeclaration), it never callsWriteDeclaration, so it cannot accidentally register new deduped names in the wrong scope.FieldProvider: Implicit operator toMemberExpressionnow setsDeclaration = field.Declaration.Update()syncs_asMember.Declarationwhen name changes (since_declarationis reset).FieldDedupUpdatesReferencesViaMemberExpression— writes two fields with the same name, verifies the reference to the second field uses the deduped name.FieldReferenceBeforeDeclarationUsesRequestedName— writes a field reference before its declaration, verifies it uses the original name without creating a spurious deduped name.💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.