Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected warning The default value does not have the same type as the argument #18312

Open
BoundedChenn31 opened this issue Feb 12, 2025 · 1 comment
Labels
Area-Compiler-Checking Type checking, attributes and all aspects of logic checking Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code.
Milestone

Comments

@BoundedChenn31
Copy link
Contributor

Repro steps

Consider the following code

open System
open System.Runtime.InteropServices
open System.Threading

type Bar() =
    let consumeToken (ct: CancellationToken) = ()

    member _.Foo(s: string, [<Optional; DefaultParameterValue(CancellationToken())>] ct) =
        consumeToken ct
        Unchecked.defaultof<string>

    member x.Foo(i: int, [<Optional; DefaultParameterValue(CancellationToken())>] ct) =
        let s = i.ToString()
        x.Foo(s, ct)

For second overload compiler produces 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)'.

ct is of type CancellationToken and also gets compiled to it. The warning seems redundant. It's possible to get rid of the warning by specifying type explicitly ct: CancellationToken.

Expected behavior

No warning issued.

Actual behavior

Seemingly unnecessary warning.

Known workarounds

Provide type explicitly.

Related information

.NET SDK 9.0.102

@T-Gro
Copy link
Member

T-Gro commented Feb 13, 2025

Since the issue is gone if you use a type annotation:

This may be caused by combination of method resolution for x.Foo and type inference for ct.
There likely is a timing issue, perharps the checks for DefaultParameterValue could be moved later in the typechecking pipeline.

@abonie abonie added Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. Area-Compiler-Checking Type checking, attributes and all aspects of logic checking and removed Needs-Triage labels Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-Checking Type checking, attributes and all aspects of logic checking Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code.
Projects
Status: New
Development

No branches or pull requests

3 participants