Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Compiler/Checking/ConstraintSolver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3213,6 +3213,10 @@ and ArgsMustSubsumeOrConvert
match usesTDC with
| TypeDirectedConversionUsed.Yes(warn, _, _) -> do! WarnD(warn csenv.DisplayEnv)
| TypeDirectedConversionUsed.No -> ()
let callerTy =
let g = csenv.g
if isValueOptionTy g calledArgTy && isOptionTy g callerTy then mkValueOptionTy g (destOptionTy g callerTy)
else callerTy
do! SolveTypeSubsumesTypeWithReport csenv ndeep m trace cxsln (Some calledArg.CalledArgumentType) calledArgTy callerTy
if g.langVersion.SupportsFeature(LanguageFeature.WarnWhenUnitPassedToObjArg) && isUnitTy g callerTy && isObjTyAnyNullness g calledArgTy then
do! WarnD(Error(FSComp.SR.tcUnitToObjSubsumption(), m))
Expand Down Expand Up @@ -3246,6 +3250,10 @@ and ArgsMustSubsumeOrConvertWithContextualReport
match usesTDC with
| TypeDirectedConversionUsed.Yes(warn, _, _) -> do! WarnD(warn csenv.DisplayEnv)
| TypeDirectedConversionUsed.No -> ()
let callerArgTy =
let g = csenv.g
if isValueOptionTy g calledArgTy && isOptionTy g callerArgTy then mkValueOptionTy g (destOptionTy g callerArgTy)
else callerArgTy
do! SolveTypeSubsumesTypeWithWrappedContextualReport csenv ndeep m trace cxsln (Some calledArg.CalledArgumentType) calledArgTy callerArgTy (fun e -> ArgDoesNotMatchError(e :?> _, calledMeth, calledArg, callerArg))
return usesTDC
}
Expand Down
26 changes: 25 additions & 1 deletion src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10092,7 +10092,31 @@ and TcMethodApplication_CheckArguments
// This is the case where some explicit arguments have been given.

let unnamedCurriedCallerArgs = unnamedCurriedCallerArgs |> List.mapSquared (fun (argExpr, argTy, mArg) -> CallerArg(argTy, mArg, false, argExpr))
let namedCurriedCallerArgs = namedCurriedCallerArgs |> List.mapSquared (fun (id, isOpt, argExpr, argTy, mArg) -> CallerNamedArg(id, CallerArg(argTy, mArg, isOpt, argExpr)))
let namedCurriedCallerArgs =
if
g.langVersion.SupportsFeature LanguageFeature.SupportValueOptionsAsOptionalParameters
&& namedCurriedCallerArgs |> List.existsSquared (fun (_, isOpt, _, _, _) -> isOpt)
then
let voptionParams =
(Set.empty, preArgumentTypeCheckingCalledMethGroup)
||> List.fold (fun acc calledMeth ->
(acc, calledMeth.AssignedNamedArgs)
||> List.fold (fun acc args ->
(acc, args)
||> List.fold (fun acc arg ->
match arg.CalledArg.OptArgInfo, arg.NamedArgIdOpt with
| CalleeSide, Some argId when isValueOptionTy g arg.CalledArg.CalledArgumentType && arg.CallerArg.IsExplicitOptional -> acc |> Set.add argId.idText
| _ -> acc)))

namedCurriedCallerArgs |> List.mapSquared (fun (id : Ident, isOpt, argExpr, argTy, mArg) ->
let argTy =
if isOpt && voptionParams |> Set.contains id.idText && isOptionTy g argTy then mkValueOptionTy g (destOptionTy g argTy)
else argTy
Comment on lines +10112 to +10114
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of hack and might cause other problems.

I wonder if it could/should be done here instead?

and TcMethodApplication_UniqueOverloadInference

// Build the CallerArg values for the caller's arguments.
// Fake up some arguments if this is the use of a method as a first class function
let unnamedCurriedCallerArgs, namedCurriedCallerArgs, returnTy =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...Lol, yeah, it broke a bunch of stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. Won't work there, either.

We'd really need to somehow defer checking the kind of the optional param till overload resolution...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, silly. I forgot to check whether the caller arg was an explicit optional arg here:

let callerTy =
let g = csenv.g
if isValueOptionTy g calledArgTy && isOptionTy g callerTy then mkValueOptionTy g (destOptionTy g callerTy)
else callerTy

But even if I did check that, this approach seems potentially wrong: is it possible to know here that a different overload wouldn't be picked if we didn't swap out the caller arg type? Would doing some subset of the overload resolution analysis here be enough, e.g., at least making sure that the explicit optional caller arg's position lined up with at least one struct optional arg's position on a candidate method?


CallerNamedArg(id, CallerArg(argTy, mArg, isOpt, argExpr)))
else
namedCurriedCallerArgs |> List.mapSquared (fun (id, isOpt, argExpr, argTy, mArg) -> CallerNamedArg(id, CallerArg(argTy, mArg, isOpt, argExpr)))


// Collect the information for F# 3.1 lambda propagation rule, and apply the caller's object type to the method's object type if the rule is relevant.
let lambdaPropagationInfo =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ module MemberDefinitions_OptionalArguments =
|> verifyCompileAndRun
|> shouldSucceed

[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"RefAndStructOptionalArgParity.fs"|])>]
let ``RefAndStructOptionalArgParity_fs`` compilation =
compilation
|> verifyCompileAndRun
|> shouldSucceed

[<Fact>]
let ``Optional Arguments can't be a ValueOption+StructAttribute attribute with langversion=9`` () =
let source =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module M

type T =
static member MRef (?x : int) = ()
static member MStruct ([<Struct>] ?x : int) = ()

T.MRef 3
T.MRef ()
T.MRef (x=3)
T.MRef (?x=None)
T.MRef (?x=Some 3)

T.MStruct 3
T.MStruct ()
T.MStruct (x=3)
T.MStruct (?x=ValueNone)
T.MStruct (?x=ValueSome 3)
Loading