fsharp / fsharp/fslang-suggestions

Inline compiler warnings and errors

Open
#1,439 8 comments 1 reaction 0 assignees View on GitHub
area: diagnostics
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

**I propose we** add `#warning` and `#error` [from C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#error-and-warning-information)

Given we have `#line` already for code generation scenarios, we should also support inline warning and error messages to further support code generation scenarios. Compared to C#'s preprocessor directives `#warning` and `#error`, an even better way to support this without necessitating parser changes would be two compiler intrinsics called `warning` and `error` which are functions that take a string and return a unit, to be added to FSharp.Core. An additional check would be needed to ensure that the string passed in is a compile-time constant though. Doing this would further allow multi-line messages easily unlike C#'s preprocessor directives.
```fs
let workInProgress() =
warning "Work in progress" // Generates a warning by using the warning function in FSharp.Core
1
let generatedStub() = error "Implement this" // Generates an error by using the error function in FSharp.Core
```

**The existing way of approaching this problem in F# is**
```fs
let [] warning = ()
let workInProgress() =
warning // Generates a warning
1
let [] error = ()
let generatedStub() = error // Generates an error
```
However, attributes can't always be used at a local `let`, e.g. within a local function, leading to a huge distance between definition and usage. Moreover, the definition and usage must be separated, which is undesirable for use-once messages.

## Pros and Cons

**The advantages of making this adjustment to F# are**
1. Better support for marking WIP code and generated stubs to be implemented
2. Clarity of intent

**The disadvantage of making this adjustment to F# is** one more thing to learn.

## Extra information

**Estimated cost (XS, S, M, L, XL, XXL):** S

**Related suggestions:**
- https://github.com/fsharp/fslang-suggestions/issues/90
It might also be worthwhile to add
```fs
[]
[]
let inline tba<'a> : 'a = raise(NotImplementedException("To be added"))
```
To FSharp.Core too.

## Affidavit (please submit!)

Please tick these items by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [StackOverflow](http://stackoverflow.com)) and I have searched StackOverflow for discussions of this issue
* [x] This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to [the compiler and tooling repository](https://github.com/dotnet/fsharp)
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate

Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

## For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Research direction

No implementation files, tests, or entry points are named. Start by reviewing the proposed F# intrinsics and related suggestion #90; determine whether compiler directives or FSharp.Core functions are intended, with done defined as compile-time-constant messages producing warnings or errors in local functions and generated code.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.