Fix attributes silently dropped on unparenthesized tuple return types#19714
Fix attributes silently dropped on unparenthesized tuple return types#19714edgarfgp wants to merge 7 commits into
Conversation
…dotnet#462) In `pars.fsy`, the `topType → topTupleType` rule discarded all `SynArgInfo` entries via `SynInfo.unnamedRetVal` whenever the return type was a bare multi-element tuple (e.g. `string * string`). Attributes placed before the tuple were parsed into the first element's `SynArgInfo` but then silently dropped, never reaching `SynBindingReturnInfo` or IL emission. The fix promotes attributes from the first element's `SynArgInfo` to the overall return `SynArgInfo` when they are non-empty, so that: static member Foo() : [<ReturnDescription("...")>] string * string = ... now correctly emits the attribute to `.param [0]` in IL, matching the behaviour of the parenthesized form `(string * string)`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
These were side effects of running TEST_UPDATE_BSL=1 and are unrelated to the fix for dotnet#462.
❗ Release notes required
|
T-Gro
left a comment
There was a problem hiding this comment.
Clean, well-scoped fix for a 10-year-old bug. LGTM.
What the fix does: In the topType -> topTupleType grammar rule, when the return type is a bare multi-element tuple, the old code discarded all SynArgInfo data (including attributes) by falling through to SynInfo.unnamedRetVal. The new code promotes attributes from the first tuple element's SynArgInfo to the return position, which is correct because attributes placed before the whole tuple syntactically belong to the return, not to the first element specifically.
Correctness verified:
- Single-element case (
[md]): unchanged, existing behavior preserved. - Multi-element with first-element attrs: promoted to return
SynArgInfo(attrs, false, None)-- consistent withunnamedRetValstructure, just carrying the attributes. - Multi-element with no first-element attrs: falls through to
unnamedRetVal-- correct. - Named parameters in tuple return types: name/optionality correctly stays per-element in the
SynType.SignatureParameter, only attrs promoted to return position. - Downstream flow verified:
SynArgInfo.rAttribs->SynReturnInfo->SynBindingReturnInfo-> IL emission (SyntaxTreeOps.fs:749-750).
Test coverage:
- Runtime reflection test (
ReturnType04.fs) withAllowMultipleattributes on both parenthesized and bare tuple -- directly verifies IL emission. - Two syntax tree baselines documenting the AST structure for both cases.
- Parenthesized tuple baseline serves as the control case.
Minor suggestion (non-blocking): Could consider adding a test for let bindings and .fsi signature files, since topType is shared across all contexts. But the grammar is shared so the fix applies everywhere.
Fixes #462
Problem
When a method return type is an unparenthesized tuple, any attributes placed before it are silently dropped and never emitted to IL:
Root cause
In
pars.fsy, thetopType → topTupleTypegrammar rule handles the return type annotation. When the return type is a bare multi-element tuple,rmdata(the list ofSynArgInfoper tuple element) has more than one entry, so the rule fell into a catch-all branch that replaced everything withSynInfo.unnamedRetVal— an emptySynArgInfowith no attributes:The attributes were parsed correctly into the first element's
SynArgInfobut then discarded here, never reachingSynBindingReturnInfoor IL emission.Fix
Promote attributes from the first element's
SynArgInfoto the overall returnSynArgInfowhen they are non-empty, since attributes placed before the whole tuple belong to the return position:Tests
ReturnType04.fs— runtime reflection test asserting attributes appear on the return parameter for both parenthesized and bare tuple formsAttribute/ReturnTypeAttributeOnBareTuple.fs+.bsl— SyntaxTree parser test documenting thatSynValInfonow carries the correctSynArgInfowith attributes in the return position for bare tuples