Skip to content

Type-mismatched [<DefaultParameterValue>] is not ignored enough #18314

@IS4Code

Description

@IS4Code

There is an inconsistency in how an incorrect placement of [<DefaultParameterValue>] is handled. Usually, in most situations, the parameter is simply not treated as optional at all, but if it is of type obj, it seems it is still considered optional.

Repro steps

open System.Runtime.InteropServices

type C =
  static member Foo([<Optional>][<DefaultParameterValue("hello")>] obj: obj) =
    printfn "%A" obj

C.Foo()

Expected behavior

C.Foo() should fail to compile, since the attribute has mismatched type and thus even [<Optional>] is ignored.

Actual behavior

The code compiles and passes null to Foo, in contrary to the warning:

FS3211 The default value does not have the same type as the argument. The DefaultParameterValue attribute and any Optional attribute will be ignored. Note: 'null' needs to be annotated with the correct type, e.g. 'DefaultParameterValue(null:obj)'.

Known workarounds

Providing a different type than obj (as far as I can tell) correctly stops the code from being compilable at all.

Related information

Environment: https://sharplab.io/#v2:DYLgZgzgPg9gDgUwHYAIDKBPCAXBBbAOgCUBXJbASzwQIElyEAneNJgNwoGMEIBYAKAHYMiFAGEUAXgEoUOAIaVOKangBGTFADEYMABQBtADwB5OJRhJ5wAHwBdYwBEEYeSWDYACvMbzquRgA1axIEPQAiAAsEYGAYcIBKexQYNQArEBT0hKkZWRQ4RgpyMFRwgFIAQXCstIEBMQIdfQSgA=

Metadata

Metadata

Assignees

Type

Projects

Status

New

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions