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

Fix name-mangling of provided types #203

Closed
wants to merge 6 commits into from
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
Expand Down Expand Up @@ -65,6 +65,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="NUnitFrameworkShims.fs" Condition="'$(TargetFramework)' == 'sl3-wp'" />
<Compile Include="ManglingNameOfProvidedTypes.fs" />
<Compile Include="HashIfExpression.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace FSharp.Compiler.Unittests

open System
open System.Text
open NUnit.Framework
open Microsoft.FSharp.Compiler

[<TestFixture>]
type ManglingNamesOfProvidedTypesWithSingleParameter() =

[<Test>]
member this.MangleWithNonDefaultValue() =
let mangled =
PrettyNaming.computeMangledNameWithoutDefaultArgValues("MyNamespace.Test", [| "xyz" |], [| "Foo", Some "abc" |])
Assert.AreEqual("MyNamespace.Test,Foo=\"xyz\"", mangled)

[<Test>]
member this.MangleWithDefaultValue() =
let mangled =
PrettyNaming.computeMangledNameWithoutDefaultArgValues("MyNamespace.Test", [| "xyz" |], [| "Foo", Some "xyz" |])
Assert.AreEqual("MyNamespace.Test", mangled)

[<Test>]
member this.DemangleNonDefaultValue() =
let name, parameters = PrettyNaming.demangleProvidedTypeName "MyNamespace.Test,Foo=\"xyz\""
Assert.AreEqual("MyNamespace.Test", name)
Assert.AreEqual([| "Foo", "xyz" |], parameters)

[<Test>]
member this.DemangleDefaultValue() =
let name, parameters = PrettyNaming.demangleProvidedTypeName "MyNamespace.Test,"
Assert.AreEqual("MyNamespace.Test", name)
Assert.AreEqual([||], parameters)

[<Test>]
member this.DemangleNewDefaultValue() =
let name, parameters = PrettyNaming.demangleProvidedTypeName "MyNamespace.Test"
Assert.AreEqual("MyNamespace.Test", name)
Assert.AreEqual([||], parameters)

[<TestFixture>]
type ManglingNamesOfProvidedTypesWithMultipleParameter() =

[<Test>]
member this.MangleWithNonDefaultValue() =
let mangled =
PrettyNaming.computeMangledNameWithoutDefaultArgValues
("MyNamespace.Test", [| "xyz"; "abc" |],
[| "Foo", Some "foo"
"Foo2", Some "foo2" |])
Assert.AreEqual("MyNamespace.Test,Foo=\"xyz\",Foo2=\"abc\"", mangled)

[<Test>]
member this.MangleWithDefaultValue() =
let mangled =
PrettyNaming.computeMangledNameWithoutDefaultArgValues
("MyNamespace.Test", [| "xyz"; "abc" |],
[| "Foo", Some "xyz"
"Foo2", Some "abc" |])
Assert.AreEqual("MyNamespace.Test", mangled)

[<Test>]
member this.DemangleMultiParameter() =
let name, parameters = PrettyNaming.demangleProvidedTypeName "TestType,Foo=\"xyz\",Foo2=\"abc\""
Assert.AreEqual("TestType", name)
Assert.AreEqual([| "Foo", "xyz"
"Foo2", "abc" |], parameters)
39 changes: 25 additions & 14 deletions src/fsharp/PrettyNaming.fs
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,15 @@ module internal Microsoft.FSharp.Compiler.PrettyNaming

let demangleProvidedTypeName (typeLogicalName:string) =
if typeLogicalName.Contains "," then
let pieces = splitAroundQuotation typeLogicalName ','
if pieces.[1..] |> Array.forall (fun x -> tryDemangleStaticStringArg x |> Option.isSome) then
let argNamesAndValues =
pieces.[1..] |> Array.map (fun piece ->
match tryDemangleStaticStringArg piece with
| None -> raise (InvalidMangledStaticArg piece)
| Some v -> v)
pieces.[0], argNamesAndValues
else
typeLogicalName, [| |]
let pieces = splitAroundQuotation typeLogicalName ','
match pieces with
| [| x; "" |] -> x, [| |]
| _ ->
let argNamesAndValues = pieces.[1..] |> Array.choose tryDemangleStaticStringArg
if argNamesAndValues.Length = (pieces.Length - 1) then
pieces.[0], argNamesAndValues
else
typeLogicalName, [| |]
else
typeLogicalName, [| |]

Expand All @@ -463,8 +462,20 @@ module internal Microsoft.FSharp.Compiler.PrettyNaming
nonDefaultArgs
|> Array.map mangleStaticStringArg
|> String.concat ","
typeLogicalName+","+nonDefaultArgsText

//let testDemangleStaticStringArg() =
// for x in [ ""; "\""; "\"\""; "a"; "\\"; "\\\\"; "\\\""; "_"; "\"\"" ] do
// if demangleStaticStringArg (mangleStaticStringArg x) <> x then printfn "failed for <<%s>>" x
if nonDefaultArgsText = "" then
typeLogicalName
else
typeLogicalName + "," + nonDefaultArgsText


let computeMangledNameWithoutDefaultArgValues(nm,staticArgs,defaultArgValues) =
let nonDefaultArgs =
(staticArgs,defaultArgValues)
||> Array.zip
|> Array.choose (fun (staticArg, (defaultArgName, defaultArgValue)) ->
let actualArgValue = string staticArg
match defaultArgValue with
| Some v when v = actualArgValue -> None
| _ -> Some (defaultArgName, actualArgValue))
mangleProvidedTypeName (nm, nonDefaultArgs)
11 changes: 1 addition & 10 deletions src/fsharp/est.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,16 +1262,7 @@ module internal ExtensionTyping =
staticParams.PApply((fun ps -> ps |> Array.map (fun sp -> sp.Name, (if sp.IsOptional then Some (string sp.RawDefaultValue) else None ))),range=m)

let defaultArgValues = defaultArgValues.PUntaint(id,m)

let nonDefaultArgs =
(staticArgs,defaultArgValues)
||> Array.zip
|> Array.choose (fun (staticArg, (defaultArgName, defaultArgValue)) ->
let actualArgValue = string staticArg
match defaultArgValue with
| Some v when v = actualArgValue -> None
| _ -> Some (defaultArgName, actualArgValue))
PrettyNaming.mangleProvidedTypeName (nm, nonDefaultArgs)
PrettyNaming.computeMangledNameWithoutDefaultArgValues(nm,staticArgs,defaultArgValues)

/// Apply the given provided method to the given static arguments (the arguments are assumed to have been sorted into application order)
let TryApplyProvidedMethod(methBeforeArgs:Tainted<ProvidedMethodBase>, staticArgs:obj[], m:range) =
Expand Down