|
| 1 | +# F# RFC FS-1332 - `assert` keyword enhancement |
| 2 | + |
| 3 | +The design suggestion [`assert` keyword: have compiler emit call to `Debug.Assert` overload with CallerArgumentExpression when possible](https://github.com/dotnet/fsharp/issues/18489) has been marked "approved in principle". |
| 4 | + |
| 5 | +This RFC covers the detailed proposal for this suggestion. |
| 6 | + |
| 7 | +- [x] [Suggestion](https://github.com/dotnet/fsharp/issues/18489) |
| 8 | +- [x] Approved in principle |
| 9 | +- [x] [Implementation](https://github.com/dotnet/fsharp/pull/17519) |
| 10 | +- [ ] [Discussion](https://github.com/fsharp/fslang-design/discussions/FILL-ME-IN) |
| 11 | + |
| 12 | +# Summary |
| 13 | + |
| 14 | +When an `assert` expression failed, it will shows the text of the expression in the exception message. |
| 15 | + |
| 16 | +# Motivation |
| 17 | + |
| 18 | +When the `assert` expression failed, it does not provide the details of the expression, but the `System.Diagnostics.Debug.Assert` does. |
| 19 | + |
| 20 | +# Detailed design |
| 21 | + |
| 22 | +`assert <bool expression>` will be translated into `System.Diagnostics.Debug.Assert(<bool expression>, "<bool expression>")`. |
| 23 | + |
| 24 | +```fsharp |
| 25 | +assert (1 + 1 = 2) // This will be translated into |
| 26 | +System.Diagnostics.Debug.Assert((1 + 1 = 2), "(1 + 1 = 2)") |
| 27 | +``` |
| 28 | + |
| 29 | +Since the [Debug.Assert(Boolean, String)](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=net-8.0#system-diagnostics-debug-assert(system-boolean-system-string)) overload has the same supporting runtime as the [Debug.Assert(Boolean)](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=net-8.0#system-diagnostics-debug-assert(system-boolean)), changing the `assert` expression to the former overload will not cause runtime errors. |
| 30 | + |
| 31 | +# Changes to the F# spec |
| 32 | + |
| 33 | +In [6.5.12 Assertion Expressions](https://github.com/fsharp/fslang-spec/blob/main/releases/FSharp-Spec-latest.md#6512-assertion-expressions), `System.Diagnostics.Debug.Assert(expr)` changes to `System.Diagnostics.Debug.Assert(<expr>, "<expr>")`. |
| 34 | + |
| 35 | +# Drawbacks |
| 36 | + |
| 37 | +No. |
| 38 | + |
| 39 | +# Alternatives |
| 40 | + |
| 41 | +By supporting [`[<OverloadResolutionPriorityAttribute>]` introduced in .NET 9](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.overloadresolutionpriorityattribute?view=net-9.0), the compiler can auto take the overload with `CallerArgumentExpression` when possible, and will not need this particular RFC. See [the comment](https://github.com/dotnet/fsharp/issues/18489#issuecomment-2831042424) from the original suggestion. |
| 42 | + |
| 43 | +The drawback of this alternative is that it will not work with .NET 8 and below. |
| 44 | + |
| 45 | +# Prior art |
| 46 | + |
| 47 | +With [supporting `[<CallerArgumentExpressionAttribute>]`](./FS-1149-support-CallerArgumentExpression.md), the `System.Diagnostics.Debug.Assert(<bool expression>)` can show the text of the expression in the exception message. This RFC is making `assert` expression works the same way. |
| 48 | + |
| 49 | +# Compatibility |
| 50 | + |
| 51 | +Please address all necessary compatibility questions: |
| 52 | + |
| 53 | +* Is this a breaking change? |
| 54 | + > No |
| 55 | +* What happens when previous versions of the F# compiler encounter this design addition as source code? |
| 56 | + > It will works the same as before. |
| 57 | +* What happens when previous versions of the F# compiler encounter this design addition in compiled binaries? |
| 58 | + > It will works the same as before. |
| 59 | +* If this is a change or extension to FSharp.Core, what happens when previous versions of the F# compiler encounter this construct? |
| 60 | + > N/A |
| 61 | +# Interop |
| 62 | + |
| 63 | +* What happens when this feature is consumed by another .NET language? |
| 64 | + > It will works the same as before. |
| 65 | +* Are there any planned or proposed features for another .NET language (e.g., [C#](https://github.com/dotnet/csharplang)) that we would want this feature to interoperate with? |
| 66 | + > N/A |
| 67 | +
|
| 68 | +# Pragmatics |
| 69 | + |
| 70 | +## Performance |
| 71 | + |
| 72 | +This feature may impact the compilation speed when the code file is too large and has many `assert` expressions, since it needs get substrings from the file. |
| 73 | + |
| 74 | +## Scaling |
| 75 | + |
| 76 | +Please list the dimensions that describe the inputs for this new feature, e.g. "number of widgets" etc. For each, estimate a reasonable upper bound for the expected size in human-written code and machine-generated code that the compiler will accept. |
| 77 | + |
| 78 | +For example |
| 79 | + |
| 80 | +* Expected maximum number of widgets in reasonable hand-written code: 100 |
| 81 | +* Expected reasonable upper bound for number of widgets accepted: 500 |
| 82 | + |
| 83 | +Testing should particularly check that compilation is linear (or log-linear or similar) along these dimensions. If quadratic or worse this should ideally be noted in the RFC. |
0 commit comments