Repository navigation
Description
SyntaxTraverse.Traverse(pos, ast, SyntaxVisitorBase<_>) passes defaultTraverse to all visitor.VisitXXX(...) -> implementation of SyntaxVisitorBase doesn't have to handle all Syntax constructs, but can pass current element on to defaultTraverse.
But for SynPats (VisitPat), defaultTraverse doesn't handle all cases with nested Syntax:
traversePat.defaultTraverse is missing:
SynPat.As(lhsPat: SynPat; rhsPat: SynPat)SynPat.Record(fieldPats: (_*_*SynPat) listSynPat.IsInst(pat: SynType)SynPat.QuoteExpr(expr: SynExpr)- (
SynPat.FromParseError(pat: SynPat))
Repro steps
Given F# source (-> text=):
let (value1) = 42
let (_ as value2) = 42
let (value3 as _) = 42
type R = { Value: int option }
let ({ Value = Some value4 }) = { Value = Some 42 }and seeking SynPat.Named that starts with value:
let walkToNamed pos (name: string) input =
SyntaxTraversal.Traverse(pos, input, { new SyntaxVisitorBase<_>() with
member _.VisitPat(_, defaultTraverse, pat) =
match pat with
| SynPat.Named (ident=ident) when ident.idText.StartsWith name ->
Some ident.idText
| _ -> defaultTraverse pat
})
let tree = getUntypedTree (SourceText.ofString text)
let poss = [
Position.mkPos 2 5 // value1
Position.mkPos 3 10 // value2
Position.mkPos 4 5 // value3
Position.mkPos 7 20 // value4
]
for pos in poss do
let res = walkToNamed pos "value" tree
printfn "%A -> %A" pos ressource with nuget package (without SynPat.Record example)
(unfortunately not as single Script File for F# interactive because I ran into #12703)
source for current main (with SynPat.Record example)
Expected behavior
Walk down to each value and output:
(2,5) -> Some "value1"
(3,10) -> Some "value2"
(4,5) -> Some "value3"
(7,20) -> Some "value4"
Actual behavior
Only finds first Named:
(2,5) -> Some "value1"
(3,10) -> None
(4,5) -> None
(7,20) -> None
(current nuget package)
and doesn't find Record on current main:
(2,5) -> Some "value1"
(3,10) -> Some "value2"
(4,5) -> Some "value3"
(7,20) -> None
Known workarounds
Custom handling of SynPat.AsSynPat.Record (etc. when necessary).
But I think we now have to custom handle everything because defaultTraverse is
- just for
SynPat - its path is fixed to current
VisitPatinvocation (-> just for current pat, but not its children) - no
traverseXXXis exposed (-> cannot get back intoSyntaxTraversal.Traverse.traverseXXXcalls)
(traverseSynExpr exposes itself to visitor.VisitExpr -- maybe there's a way to always expose all traverseXXX functions to visitors so visitor implementation can handle children?)
Related information
Provide any related information (optional):
dotnet --version:6.0.202
Edit: SynPat.As is handled in current main (#13114 (comment))
-> Updated example to include SynPat.Record
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
kerams commentedon May 8, 2022
As and IsInst (behind As) have been fixed in #12837 / #12930
Booksbaum commentedon May 8, 2022
You're right.
I got nuget package and main branch mixed up: I tested with nuget, and only checked
defaultTraversein main branch -- and overlookedSynPat.As. Sorry about that 😞For other patterns it's of course still the case
-> Added example for
SynPat.Recordto descriptionI cannot find
SynPat.IsInstindefaultTraverse?kerams commentedon May 8, 2022
Now I am sorry :P. It's not part of the default traverse, but I am overriding it in a custom walker. I don't see why
traverseSynTypeshould not be called on the node by default though. As forRecord, I don't know. Where do we draw the line of what should be visited by default?Booksbaum commentedon May 8, 2022
I'm currently trying to detect if parens are necessary when a type annotation gets added.
What I do is: Walk down to
SynPat.Named(+ a couple of other locations -- butNamedis most common) and then inspect the parent (or ancestors) and decide if parens are necessary.To get to
SynPat.Named("value")I have to go downSynPat.Record(Ast)
(Probably not the most common location to have a type annotation -- but I like to keep it general)
-> without
defaultTraversehandlingSynPat.RecordI have to implement a customtraversePat-- which is basically the same as existingdefaultTraversebut with addedSynPat.Recordhandling (and removedSynTypetraversal because I don't have access to existing traversal methods but fortunately don't need to traverseSynType...) -- I literally just copieddefaultTraverseforSynPats fromdotnet/fsharpand just applied these two changes....And: It's unexpected when it doesn't go down a path. It should at least be documented which elements gets traversed and which don't.
At least: there should be a
traverseSynXXXto visit children like it's available inVisitExpr(though just forSynExpr, not for other elements)The issue with a custom walker is: There's no available default traversal
-> you have to implement all traverse functions yourself:
-> We have to traverse
SynType-> must implement a defaultTraverse forSynType(-> to use directly to traverse or to callVisitType(path, defaultTraverse, ty)).But to go further down we have to implement more traversals:
SynType.StaticConstantExpr(expr=expr: SynExpr)-> must implement traversal for
SynExprtooAnd
SynExprcontains a couple of otherSynXXXs too-> again additional traversals
And now we basically have our own
SyntaxTraversal.Traverseimplementation....hmm...maybe
SyntaxTraversalshould make itstraverseSynXXXfunctions public?BTW: same issue when you want to select something that's not traversed by default.
I wanted to go down
SynExpr.Lambda(parsedData=Some (pats, expr))-- but what gets traversed isSynExpr.Lambda(args; body). Fortunately:patsis relative small and I don't need to go down Types (-> relatively simple custom traversal) andVisitExprprovides atraverseSynExprwhich allows to walk down children :)Add SyntaxTraversal from FCS
Add SyntaxTraversal from FCS
Add SyntaxTraversal from FCS
Add SyntaxTraversal from FCS
github-actions commentedon Mar 18, 2026
🤖 This is an automated response from Repo Assist.
The missing
SynPat.RecordandSynPat.QuoteExprcases indefaultTraverse(forSynPatinSyntaxTraverse.Traverse) appear to have been fixed in the current codebase.Looking at
src/Compiler/Service/ServiceParseTreeWalk.fs, thetraversePatfunction'sdefaultTraversenow handles:SynPat.Record(line ~790): iterates all field patterns recursivelySynPat.QuoteExpr(line ~803): traverses into the quoted expressionThese were the two remaining missing cases called out in the updated issue description. The
SynPat.Ascase was already fixed earlier (PR #12837/#12930).This issue has been labelled
AI-thinks-issue-fixed. Maintainers please verify and close if appropriate.auduchinok commentedon Mar 23, 2026
The bot only covers some of the reported cases in the comment. I believe it should not be closed.
Add regression test for #13114: SynPat.Record and QuoteExpr traversal
Add regression test for #13114: SynPat.Record and QuoteExpr traversal
Add regression test for #13114: SynPat.Record and QuoteExpr traversal
Fix regression tests for #13114: remove unrelated test, add IsInst an…
github-actions commentedon Mar 31, 2026
🤖
SynPat.IsInstis still not handled indefaultTraverseforSynPatinServiceParseTreeWalk.fs. A regression test added in #19468 confirming it fails was removed because it was breaking CI. TheSynPat.RecordandSynPat.QuoteExprcases remain fixed. TheAI-thinks-issue-fixedlabel has been removed. Apologies for the incorrect label.Fix #13114: Handle SynPat.IsInst and SynPat.FromParseError in default…
Add regression test: #13114, SynPat.Record and QuoteExpr traversal (#…