diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eee24c613..79ca3400ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### 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) +- 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.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/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 026e0f6203..debfce79f0 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -2339,10 +2339,10 @@ 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 + | StaticOptimizationConstraint.WhenTyparIsStruct t -> genSingleTextNode t +> !-" struct" | StaticOptimizationConstraint.WhenTyparTyconEqualsTycon n -> genSingleTextNode n.TypeParameter +> sepColon +> sepSpace +> genType n.Type )