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

Improve implied lambda and delegate argument names #15277

Merged
merged 9 commits into from
Jun 5, 2023
Merged
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
44 changes: 33 additions & 11 deletions src/Compiler/Checking/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8445,7 +8445,19 @@ and TcUnionCaseOrExnCaseOrActivePatternResultItemThen (cenv: cenv) overallTy env
// This is where the constructor expects arguments but is not applied to arguments, hence build a lambda
numArgTys,
(fun () ->
let vs, args = argTys |> List.mapi (fun i ty -> mkCompGenLocal mItem ("arg" + string i) ty) |> List.unzip
let argNamesIfFeatureEnabled =
if g.langVersion.SupportsFeature LanguageFeature.ImprovedImpliedArgumentNames then
argNames
else
[]

let vs, args =
argTys
|> List.mapi (fun i ty ->
let argName = argNamesIfFeatureEnabled |> List.tryItem i |> Option.map (fun x -> x.idText) |> Option.defaultWith (fun () -> "arg" + string i)
mkCompGenLocal mItem argName ty)
|> List.unzip

let constrApp = mkConstrApp mItem args
let lam = mkMultiLambda mItem vs (constrApp, tyOfExpr g constrApp)
lam)
Expand Down Expand Up @@ -9535,7 +9547,13 @@ and TcMethodApplication_CheckArguments
let denv = env.DisplayEnv
match curriedCallerArgsOpt with
| None ->
let curriedArgTys, returnTy =
let curriedArgTys, curriedArgNamesIfFeatureEnabled, returnTy =
let paramNamesIfFeatureEnabled (g: TcGlobals) (meth: MethInfo) =
if g.langVersion.SupportsFeature LanguageFeature.ImprovedImpliedArgumentNames then
meth.GetParamNames()
else
[]

match candidates with
// "single named item" rule. This is where we have a single accessible method
// member x.M(arg1, ..., argN)
Expand All @@ -9547,19 +9565,23 @@ and TcMethodApplication_CheckArguments
// to their default values (for optionals) and be part of the return tuple (for out args).
| [calledMeth] ->
let curriedArgTys, returnTy = UnifyMatchingSimpleArgumentTypes cenv env exprTy.Commit calledMeth mMethExpr mItem
curriedArgTys, MustEqual returnTy
curriedArgTys, paramNamesIfFeatureEnabled g calledMeth, MustEqual returnTy
| _ ->
let domainTy, returnTy = UnifyFunctionType None cenv denv mMethExpr exprTy.Commit
let argTys = if isUnitTy g domainTy then [] else tryDestRefTupleTy g domainTy
// Only apply this rule if a candidate method exists with this number of arguments
let argTys =
if candidates |> List.exists (CalledMethHasSingleArgumentGroupOfThisLength argTys.Length) then
argTys
else
[domainTy]
[argTys], MustEqual returnTy

let lambdaVarsAndExprs = curriedArgTys |> List.mapiSquared (fun i j ty -> mkCompGenLocal mMethExpr ("arg"+string i+string j) ty)
let argTys, argNames =
match candidates |> List.tryFind (CalledMethHasSingleArgumentGroupOfThisLength argTys.Length) with
| Some meth -> argTys, paramNamesIfFeatureEnabled g meth
| None -> [domainTy], [[None]]
[argTys], argNames, MustEqual returnTy

let lambdaVarsAndExprs =
curriedArgTys
|> List.mapiSquared (fun i j ty ->
let argName = curriedArgNamesIfFeatureEnabled |> List.tryItem i |> Option.bind (List.tryItem j) |> Option.flatten |> Option.defaultWith (fun () -> "arg" + string i + string j)
mkCompGenLocal mMethExpr argName ty)

let unnamedCurriedCallerArgs = lambdaVarsAndExprs |> List.mapSquared (fun (_, e) -> CallerArg(tyOfExpr g e, e.Range, false, e))
let namedCurriedCallerArgs = lambdaVarsAndExprs |> List.map (fun _ -> [])
let lambdaVars = List.mapSquared fst lambdaVarsAndExprs
Expand Down
17 changes: 16 additions & 1 deletion src/Compiler/Checking/MethodCalls.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,8 +1266,23 @@ let BuildNewDelegateExpr (eventInfoOpt: EventInfo option, g, amap, delegateTy, d
if List.exists (isByrefTy g) delArgTys then
error(Error(FSComp.SR.tcFunctionRequiresExplicitLambda(delArgTys.Length), m))

let delFuncArgNamesIfFeatureEnabled =
match delFuncExpr with
| Expr.Val (valRef = vref) when g.langVersion.SupportsFeature LanguageFeature.ImprovedImpliedArgumentNames ->
match vref.ValReprInfo with
| Some repr when repr.ArgNames.Length = delArgTys.Length -> Some repr.ArgNames
| _ -> None
| _ -> None

let delArgVals =
delArgTys |> List.mapi (fun i argTy -> fst (mkCompGenLocal m ("delegateArg" + string i) argTy))
delArgTys
|> List.mapi (fun i argTy ->
let argName =
match delFuncArgNamesIfFeatureEnabled with
| Some argNames -> argNames[i]
| None -> "delegateArg" + string i

fst (mkCompGenLocal m argName argTy))

let expr =
let args =
Expand Down
15 changes: 15 additions & 0 deletions src/Compiler/Checking/infos.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,21 @@ type MethInfo =
member x.GetFSharpReturnType(amap, m, minst) =
x.GetCompiledReturnType(amap, m, minst) |> GetFSharpViewOfReturnType amap.g

member x.GetParamNames() =
match x with
| FSMeth (g, _, vref, _) ->
ParamNameAndType.FromMember x.IsCSharpStyleExtensionMember g vref |> List.mapSquared (fun (ParamNameAndType (name, _)) -> name |> Option.map (fun x -> x.idText))
| ILMeth (ilMethInfo = ilminfo) ->
// A single group of tupled arguments
[ ilminfo.ParamMetadata |> List.map (fun x -> x.Name) ]
#if !NO_TYPEPROVIDERS
| ProvidedMeth (_, mi, _, m) ->
// A single group of tupled arguments
[ [ for p in mi.PApplyArray((fun mi -> mi.GetParameters()), "GetParameters", m) do
yield p.PUntaint((fun p -> Some p.Name), m) ] ]
#endif
| _ -> []
kerams marked this conversation as resolved.
Show resolved Hide resolved

/// Get the parameter types of a method info
member x.GetParamTypes(amap, m, minst) =
match x with
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/Checking/infos.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ type MethInfo =
/// Get the ParamData objects for the parameters of a MethInfo
member GetParamDatas: amap: ImportMap * m: range * minst: TType list -> ParamData list list

/// Get the parameter names of a MethInfo
member GetParamNames: unit -> string option list list

/// Get the parameter types of a method info
member GetParamTypes: amap: ImportMap * m: range * minst: TType list -> TType list list

Expand Down
1 change: 1 addition & 0 deletions src/Compiler/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ featureNonInlineLiteralsAsPrintfFormat,"String values marked as literals and IL
featureNestedCopyAndUpdate,"Nested record field copy-and-update"
featureExtendedStringInterpolation,"Extended string interpolation similar to C# raw string literals."
featureWarningWhenMultipleRecdTypeChoice,"Raises warnings when multiple record type matches were found during name resolution because of overlapping field names."
featureImprovedImpliedArgumentNames,"Improved implied argument names"
3353,fsiInvalidDirective,"Invalid directive '#%s %s'"
3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/Facilities/LanguageFeatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type LanguageFeature =
| NestedCopyAndUpdate
| ExtendedStringInterpolation
| WarningWhenMultipleRecdTypeChoice
| ImprovedImpliedArgumentNames

/// LanguageVersion management
type LanguageVersion(versionText) =
Expand Down Expand Up @@ -159,6 +160,7 @@ type LanguageVersion(versionText) =
LanguageFeature.NestedCopyAndUpdate, previewVersion
LanguageFeature.ExtendedStringInterpolation, previewVersion
LanguageFeature.WarningWhenMultipleRecdTypeChoice, previewVersion
LanguageFeature.ImprovedImpliedArgumentNames, previewVersion

]

Expand Down Expand Up @@ -282,6 +284,7 @@ type LanguageVersion(versionText) =
| LanguageFeature.NestedCopyAndUpdate -> FSComp.SR.featureNestedCopyAndUpdate ()
| LanguageFeature.ExtendedStringInterpolation -> FSComp.SR.featureExtendedStringInterpolation ()
| LanguageFeature.WarningWhenMultipleRecdTypeChoice -> FSComp.SR.featureWarningWhenMultipleRecdTypeChoice ()
| LanguageFeature.ImprovedImpliedArgumentNames -> FSComp.SR.featureImprovedImpliedArgumentNames ()

/// Get a version string associated with the given feature.
static member GetFeatureVersionString feature =
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Facilities/LanguageFeatures.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type LanguageFeature =
| NestedCopyAndUpdate
| ExtendedStringInterpolation
| WarningWhenMultipleRecdTypeChoice
| ImprovedImpliedArgumentNames

/// LanguageVersion management
type LanguageVersion =
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">implicitní yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">Notace expr[idx] pro indexování a vytváření řezů</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">implizite yield-Anweisung</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">expr[idx]-Notation zum Indizieren und Aufteilen</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">elemento yield implícito</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">Notación para indexación y segmentación expr[idx]</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">yield implicite</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">Notation expr[idx] pour l’indexation et le découpage</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">istruzione yield implicita</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">Notazione expr[idx] per l'indicizzazione e il sezionamento</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">暗黙的な yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">インデックス作成とスライス用の expr[idx] 表記</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">암시적 yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">인덱싱 및 슬라이싱을 위한 expr[idx] 표기법</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">niejawne słowo kluczowe yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">notacja wyrażenia expr[idx] do indeksowania i fragmentowania</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">yield implícito</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">notação expr[idx] para indexação e fatia</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">неявное использование yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">expr[idx] для индексации и среза</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">örtük yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">Dizin oluşturma ve dilimleme için expr[idx] gösterimi</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">隐式 yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">用于索引和切片的 expr[idx] 表示法</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<target state="translated">隱含 yield</target>
<note />
</trans-unit>
<trans-unit id="featureImprovedImpliedArgumentNames">
<source>Improved implied argument names</source>
<target state="new">Improved implied argument names</target>
<note />
</trans-unit>
<trans-unit id="featureIndexerNotationWithoutDot">
<source>expr[idx] notation for indexing and slicing</source>
<target state="translated">用於編製索引和分割的 expr[idx] 註釋</target>
Expand Down
Loading