Skip to content

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

@ijklam

Description

@ijklam

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

> type A = delegate of [<OptionalArgument; System.Runtime.CompilerServices.CallerFilePath>] a: string -> unit;;

  type A = delegate of [<OptionalArgument; System.Runtime.CompilerServices.CallerFilePath>] 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 [<OptionalArgument; System.Runtime.CompilerServices.CallerFilePath>] 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<System.String>)'.
   at <StartupCode$FSI_0010>.$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 [<OptionalArgument>].

> type A = delegate of [<System.Runtime.CompilerServices.CallerFilePath>] ?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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions