dotnet / dotnet/fsharp

Error when using `[<OptionalArgument>]` with caller information attributes in delegate definitions

Open
#18,868 0 comments 0 reactions 0 assignees View on GitHub
Area-Compiler-CodeGen Bug
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

Error when using `[]` with caller information attributes in delegate definitions.

```fsharp
> type A = delegate of [] a: string -> unit;;

type A = delegate of [] a: string -> unit;;
------------------------------------------------------------------------------------------^

/stdin(5,91): error FS1246: 'CallerFilePath' must be applied to an argument of type 'string', but has been applied to an argument of type 'string'

> type A = delegate of [] a: string option -> unit;;
type A =
delegate of string option option -> unit

> let f = A(printfn "%A");;
val f: A

> f.Invoke();;
System.MissingMethodException: Method not found: 'Void A.Invoke(Microsoft.FSharp.Core.FSharpOption`1)'.
at .$FSI_0010.main@()
at System.RuntimeMethodHandle.InvokeMethod(ObjectHandleOnStack target, Void** arguments, ObjectHandleOnStack sig, BOOL isConstructor, ObjectHandleOnStack result)
at System.RuntimeMethodHandle.InvokeMethod(ObjectHandleOnStack target, Void** arguments, ObjectHandleOnStack sig, BOOL isConstructor, ObjectHandleOnStack result)
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
Stopped due to error

> f.Invoke(None);;

f.Invoke(None);;
---------^^^^

/stdin(12,10): error FS0001: This expression was expected to have type
'string'
but here has type
''a option'
```

**Expected behavior**

It should compile successfully.

**Actual behavior**

It provides a strange error message.

**Known workarounds**

Use `?a: string` instead of use `[]`.

```fsharp
> type A = delegate of [] ?a: string -> unit;;
type A =
delegate of string option -> unit

> let f = A(printfn "%A");;
val f: A

> f.Invoke();;
Some "/stdin"
val it: unit = ()
```

**Related information**

Provide any related information (optional):

* Operating system: Ubuntu 24.04.3
* .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET 10.0.0-preview.7.25322.101
* Editing Tools (e.g. Visual Studio Version, Visual Studio): Microsoft (R) F# Interactive version F# 10.0 的 14.0.100.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.