From 3c1af1f175cbd40e8b7479c1a9ac9c48f1a39d28 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Mon, 5 Oct 2026 13:12:28 +0200 Subject: [PATCH 1/2] Keep struct in a when ^T struct static optimization The printer wrote only the type parameter of a WhenTyparIsStruct constraint, so `when ^T struct = 0` became `when ^T = 0`, which does not parse. The case that pinned the bug as ignored now runs as a regular negative case. Fixes #3514 --- CHANGELOG.md | 1 + .../negative/3514-when-a-type-parameter-is-a-struct.fs | 1 + .../negative/when-a-type-parameter-is-a-struct.ignore.fs | 4 ---- src/Fantomas.Core/CodePrinter.fs | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) create mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/3514-when-a-type-parameter-is-a-struct.fs delete mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/when-a-type-parameter-is-a-struct.ignore.fs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eee24c613..edc96e4f59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed - Exception abbreviation loses its right-hand side. [#3511](https://github.com/fsprojects/fantomas/issues/3511) +- Static optimization when ^T struct loses struct. [#3514](https://github.com/fsprojects/fantomas/issues/3514) ## [8.0.6] - 2026-10-01 diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/3514-when-a-type-parameter-is-a-struct.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/3514-when-a-type-parameter-is-a-struct.fs new file mode 100644 index 0000000000..3c42c151a3 --- /dev/null +++ b/src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/3514-when-a-type-parameter-is-a-struct.fs @@ -0,0 +1 @@ +let inline retype (x: ^T) : ^U = (# "" x : ^U #) when ^T struct = 0 diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/when-a-type-parameter-is-a-struct.ignore.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/when-a-type-parameter-is-a-struct.ignore.fs deleted file mode 100644 index 5ebc3fae80..0000000000 --- a/src/Fantomas.Core.SnapshotTests/cases/oak/StaticOptimizationConstraint/WhenTyparIsStruct/negative/when-a-type-parameter-is-a-struct.ignore.fs +++ /dev/null @@ -1,4 +0,0 @@ -(*--- -# Formatting drops the `struct` of the constraint, and the result does not parse. ----*) -let inline retype (x: ^T) : ^U = (# "" x : ^U #) when ^T struct = 0 diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 026e0f6203..4a75f23953 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -2342,7 +2342,7 @@ let genExpr (e: Expr) = sepSpace node.Constraints (function - | StaticOptimizationConstraint.WhenTyparIsStruct t -> genSingleTextNode t + | StaticOptimizationConstraint.WhenTyparIsStruct t -> genSingleTextNode t +> !-" struct" | StaticOptimizationConstraint.WhenTyparTyconEqualsTycon n -> genSingleTextNode n.TypeParameter +> sepColon +> sepSpace +> genType n.Type ) From dd062d87f6d7931a790593cfca698e567abf42f4 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Mon, 5 Oct 2026 13:23:20 +0200 Subject: [PATCH 2/2] Keep the and and the order of static optimization conditions Conditions of one `when` were printed with only a space between them, so `when ^T : int and ^U struct` lost its `and`. The parser also keeps those conditions in reverse, and nests several `when` clauses with the first one outermost, so formatting reversed both. Reversed conditions put the Oak children out of source order, and reversed clauses made the result swap back on every format. The transformer now rebuilds the clauses and their conditions in source order, and the printer joins conditions with `and`. --- CHANGELOG.md | 1 + .../conditions-joined-by-and.fs | 1 + .../conditions-joined-by-and.gold.fs | 1 + .../several-when-clauses.fs | 1 + .../several-when-clauses.gold.fs | 1 + src/Fantomas.Core/ASTTransformer.fs | 55 ++++++++++++------- src/Fantomas.Core/CodePrinter.fs | 2 +- 7 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.fs create mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.gold.fs create mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.fs create mode 100644 src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.gold.fs diff --git a/CHANGELOG.md b/CHANGELOG.md index edc96e4f59..79ca3400ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Exception abbreviation loses its right-hand side. [#3511](https://github.com/fsprojects/fantomas/issues/3511) - Static optimization when ^T struct loses struct. [#3514](https://github.com/fsprojects/fantomas/issues/3514) +- Static optimization loses the `and` between its conditions, and reverses its conditions and `when` clauses. [#3516](https://github.com/fsprojects/fantomas/pull/3516) ## [8.0.6] - 2026-10-01 diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.fs new file mode 100644 index 0000000000..76928964b7 --- /dev/null +++ b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.fs @@ -0,0 +1 @@ +let inline f (x: ^T) (y: ^U) = 0 when ^T : int and ^U struct = 1 diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.gold.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.gold.fs new file mode 100644 index 0000000000..66e98056c6 --- /dev/null +++ b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/conditions-joined-by-and.gold.fs @@ -0,0 +1 @@ +let inline f (x: ^T) (y: ^U) = 0 when ^T: int and ^U struct = 1 diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.fs new file mode 100644 index 0000000000..7b0a94bb86 --- /dev/null +++ b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.fs @@ -0,0 +1 @@ +let inline f (x: ^T) = 0 when ^T : int = 1 when ^T : float = 2 diff --git a/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.gold.fs b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.gold.fs new file mode 100644 index 0000000000..96b489a780 --- /dev/null +++ b/src/Fantomas.Core.SnapshotTests/cases/oak/Expr/LibraryOnlyStaticOptimization/several-when-clauses.gold.fs @@ -0,0 +1 @@ +let inline f (x: ^T) = 0 when ^T: int = 1 when ^T: float = 2 diff --git a/src/Fantomas.Core/ASTTransformer.fs b/src/Fantomas.Core/ASTTransformer.fs index a0529b10c9..4ab5eda56e 100644 --- a/src/Fantomas.Core/ASTTransformer.fs +++ b/src/Fantomas.Core/ASTTransformer.fs @@ -1923,29 +1923,42 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr = ExprSetNode(mkExpr creationAide e1, mkExpr creationAide e2, exprRange) |> Expr.Set - | SynExpr.LibraryOnlyStaticOptimization(constraints, e, optExpr, _) -> - let constraints = - constraints - |> List.map ( - function - | SynStaticOptimizationConstraint.WhenTyparTyconEqualsTycon(t1, t2, _) -> - StaticOptimizationConstraintWhenTyparTyconEqualsTyconNode( - mkSynTypar t1, - mkType creationAide t2, - unionRanges t1.Range t2.Range + | SynExpr.LibraryOnlyStaticOptimization _ -> + let mkConstraint (c: SynStaticOptimizationConstraint) : StaticOptimizationConstraint = + match c with + | SynStaticOptimizationConstraint.WhenTyparTyconEqualsTycon(t1, t2, _) -> + StaticOptimizationConstraintWhenTyparTyconEqualsTyconNode( + mkSynTypar t1, + mkType creationAide t2, + unionRanges t1.Range t2.Range + ) + |> StaticOptimizationConstraint.WhenTyparTyconEqualsTycon + | SynStaticOptimizationConstraint.WhenTyparIsStruct(t, _) -> + mkSynTypar t |> StaticOptimizationConstraint.WhenTyparIsStruct + + // The parser nests `e when A = a when B = b` as A(a, B(b, e)), with the first clause outermost, while the + // Oak puts the last clause outermost: B(b, A(a, e)). `wrap` builds the clauses seen so far around `e`. + let rec collect (wrap: Expr -> Expr) (expr: SynExpr) : Expr = + match expr with + | SynExpr.LibraryOnlyStaticOptimization(constraints, e, optExpr, _) -> + collect + (fun inner -> + let optimizedExpr: Expr = wrap inner + let expr: Expr = mkExpr creationAide e + + ExprLibraryOnlyStaticOptimizationNode( + optimizedExpr, + // The parser keeps the conditions of a `when` in reverse. + constraints |> List.rev |> List.map mkConstraint, + expr, + unionRanges (Expr.Node optimizedExpr).Range (Expr.Node expr).Range + ) + |> Expr.LibraryOnlyStaticOptimization ) - |> StaticOptimizationConstraint.WhenTyparTyconEqualsTycon - | SynStaticOptimizationConstraint.WhenTyparIsStruct(t, _) -> - mkSynTypar t |> StaticOptimizationConstraint.WhenTyparIsStruct - ) + optExpr + | _ -> wrap (mkExpr creationAide expr) - ExprLibraryOnlyStaticOptimizationNode( - mkExpr creationAide optExpr, - constraints, - mkExpr creationAide e, - exprRange - ) - |> Expr.LibraryOnlyStaticOptimization + collect id e | SynExpr.InterpolatedString(parts, _, _) -> let lastIndex = parts.Length - 1 diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 4a75f23953..debfce79f0 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -2339,7 +2339,7 @@ let genExpr (e: Expr) = genExpr node.OptimizedExpr +> onlyIfNot node.Constraints.IsEmpty (!-" when ") +> col - sepSpace + (sepSpace +> wordAndFixed +> sepSpace) node.Constraints (function | StaticOptimizationConstraint.WhenTyparIsStruct t -> genSingleTextNode t +> !-" struct"