Skip to content

Commit 16bca5a

Browse files
authored
ProvidedTypeContext refactoring (#9235)
* key type changed to providedType * fixed singleton * moved up
1 parent 526f35b commit 16bca5a

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/fsharp/ExtensionTyping.fs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ module internal ExtensionTyping =
236236
BindingFlags.Instance |||
237237
BindingFlags.Public
238238

239+
type CustomAttributeData = System.Reflection.CustomAttributeData
240+
type CustomAttributeNamedArgument = System.Reflection.CustomAttributeNamedArgument
241+
type CustomAttributeTypedArgument = System.Reflection.CustomAttributeTypedArgument
242+
239243
// NOTE: for the purposes of remapping the closure of generated types, the FullName is sufficient.
240244
// We do _not_ rely on object identity or any other notion of equivalence provided by System.Type
241245
// itself. The mscorlib implementations of System.Type equality relations are not suitable: for
@@ -246,11 +250,12 @@ module internal ExtensionTyping =
246250
// providers can implement wrap-and-filter "views" over existing System.Type clusters without needing
247251
// to preserve object identity when presenting the types to the F# compiler.
248252

249-
let providedSystemTypeComparer =
250-
let key (ty: System.Type) = (ty.Assembly.FullName, ty.FullName)
251-
{ new IEqualityComparer<Type> with
252-
member __.GetHashCode(ty: Type) = hash (key ty)
253-
member __.Equals(ty1: Type, ty2: Type) = (key ty1 = key ty2) }
253+
type ProvidedTypeComparer() =
254+
let key (ty: ProvidedType) = (ty.Assembly.FullName, ty.FullName)
255+
static member val Instance = ProvidedTypeComparer()
256+
interface IEqualityComparer<ProvidedType> with
257+
member __.GetHashCode(ty: ProvidedType) = hash (key ty)
258+
member __.Equals(ty1: ProvidedType, ty2: ProvidedType) = (key ty1 = key ty2)
254259

255260
/// The context used to interpret information in the closure of System.Type, System.MethodInfo and other
256261
/// info objects coming from the type provider.
@@ -260,9 +265,9 @@ module internal ExtensionTyping =
260265
///
261266
/// Laziness is used "to prevent needless computation for every type during remapping". However it
262267
/// appears that the laziness likely serves no purpose and could be safely removed.
263-
type ProvidedTypeContext =
268+
and ProvidedTypeContext =
264269
| NoEntries
265-
| Entries of Dictionary<System.Type, ILTypeRef> * Lazy<Dictionary<System.Type, obj>>
270+
| Entries of Dictionary<ProvidedType, ILTypeRef> * Lazy<Dictionary<ProvidedType, obj>>
266271

267272
static member Empty = NoEntries
268273

@@ -271,7 +276,7 @@ module internal ExtensionTyping =
271276
member ctxt.GetDictionaries() =
272277
match ctxt with
273278
| NoEntries ->
274-
Dictionary<System.Type, ILTypeRef>(providedSystemTypeComparer), Dictionary<System.Type, obj>(providedSystemTypeComparer)
279+
Dictionary<ProvidedType, ILTypeRef>(ProvidedTypeComparer.Instance), Dictionary<ProvidedType, obj>(ProvidedTypeComparer.Instance)
275280
| Entries (lookupILTR, lookupILTCR) ->
276281
lookupILTR, lookupILTCR.Force()
277282

@@ -296,16 +301,12 @@ module internal ExtensionTyping =
296301
match ctxt with
297302
| NoEntries -> NoEntries
298303
| Entries(d1, d2) ->
299-
Entries(d1, lazy (let dict = new Dictionary<System.Type, obj>(providedSystemTypeComparer)
304+
Entries(d1, lazy (let dict = Dictionary<ProvidedType, obj>(ProvidedTypeComparer.Instance)
300305
for KeyValue (st, tcref) in d2.Force() do dict.Add(st, f tcref)
301306
dict))
302307

303-
type CustomAttributeData = System.Reflection.CustomAttributeData
304-
type CustomAttributeNamedArgument = System.Reflection.CustomAttributeNamedArgument
305-
type CustomAttributeTypedArgument = System.Reflection.CustomAttributeTypedArgument
306-
307-
[<AllowNullLiteral; Sealed>]
308-
type ProvidedType (x: System.Type, ctxt: ProvidedTypeContext) =
308+
and [<AllowNullLiteral; Sealed>]
309+
ProvidedType (x: System.Type, ctxt: ProvidedTypeContext) =
309310
inherit ProvidedMemberInfo(x, ctxt)
310311
let isMeasure =
311312
lazy
@@ -330,7 +331,7 @@ module internal ExtensionTyping =
330331
member __.Namespace = x.Namespace
331332
member __.FullName = x.FullName
332333
member __.IsArray = x.IsArray
333-
member __.Assembly = x.Assembly |> ProvidedAssembly.Create
334+
member __.Assembly: ProvidedAssembly = x.Assembly |> ProvidedAssembly.Create
334335
member __.GetInterfaces() = x.GetInterfaces() |> ProvidedType.CreateArray ctxt
335336
member __.GetMethods() = x.GetMethods bindingFlags |> ProvidedMethodInfo.CreateArray ctxt
336337
member __.GetEvents() = x.GetEvents bindingFlags |> ProvidedEventInfo.CreateArray ctxt
@@ -390,9 +391,9 @@ module internal ExtensionTyping =
390391
member __.Handle = x
391392
override __.Equals y = assert false; match y with :? ProvidedType as y -> x.Equals y.Handle | _ -> false
392393
override __.GetHashCode() = assert false; x.GetHashCode()
393-
member __.TryGetILTypeRef() = ctxt.TryGetILTypeRef x
394-
member __.TryGetTyconRef() = ctxt.TryGetTyconRef x
395394
member __.Context = ctxt
395+
member this.TryGetILTypeRef() = this.Context.TryGetILTypeRef this
396+
member this.TryGetTyconRef() = this.Context.TryGetTyconRef this
396397
static member ApplyContext (pt: ProvidedType, ctxt) = ProvidedType(pt.Handle, ctxt)
397398
static member TaintedEquals (pt1: Tainted<ProvidedType>, pt2: Tainted<ProvidedType>) =
398399
Tainted.EqTainted (pt1.PApplyNoFailure(fun st -> st.Handle)) (pt2.PApplyNoFailure(fun st -> st.Handle))

src/fsharp/ExtensionTyping.fsi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ module internal ExtensionTyping =
7474
[<Sealed>]
7575
type ProvidedTypeContext =
7676

77-
member TryGetILTypeRef : System.Type -> ILTypeRef option
77+
member TryGetILTypeRef : ProvidedType -> ILTypeRef option
7878

79-
member TryGetTyconRef : System.Type -> obj option
79+
member TryGetTyconRef : ProvidedType -> obj option
8080

8181
static member Empty : ProvidedTypeContext
8282

83-
static member Create : Dictionary<System.Type,ILTypeRef> * Dictionary<System.Type,obj (* TyconRef *) > -> ProvidedTypeContext
83+
static member Create : Dictionary<ProvidedType, ILTypeRef> * Dictionary<ProvidedType, obj (* TyconRef *) > -> ProvidedTypeContext
8484

85-
member GetDictionaries : unit -> Dictionary<System.Type,ILTypeRef> * Dictionary<System.Type,obj (* TyconRef *) >
85+
member GetDictionaries : unit -> Dictionary<ProvidedType, ILTypeRef> * Dictionary<ProvidedType, obj (* TyconRef *) >
8686

8787
/// Map the TyconRef objects, if any
8888
member RemapTyconRefs : (obj -> obj) -> ProvidedTypeContext
8989

90-
type [<AllowNullLiteral; Sealed; Class>]
90+
and [<AllowNullLiteral; Sealed; Class>]
9191
ProvidedType =
9292
inherit ProvidedMemberInfo
9393
member IsSuppressRelocate : bool

src/fsharp/TypeChecker.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15729,12 +15729,12 @@ module EstablishTypeDefinitionCores =
1572915729
tycon.entity_tycon_repr <- repr
1573015730
// Record the details so we can map System.Type --> TyconRef
1573115731
let ilOrigRootTypeRef = GetOriginalILTypeRefOfProvidedType (theRootTypeWithRemapping, m)
15732-
theRootTypeWithRemapping.PUntaint ((fun st -> ignore(lookupTyconRef.Remove(st.RawSystemType)) ; lookupTyconRef.Add(st.RawSystemType, tcref)), m)
15732+
theRootTypeWithRemapping.PUntaint ((fun st -> ignore(lookupTyconRef.Remove(st)) ; lookupTyconRef.Add(st, tcref)), m)
1573315733

1573415734
// Record the details so we can map System.Type --> ILTypeRef, including the relocation if any
1573515735
if not isSuppressRelocate then
1573615736
let ilTgtRootTyRef = tycon.CompiledRepresentationForNamedType
15737-
theRootTypeWithRemapping.PUntaint ((fun st -> ignore(lookupILTypeRef.Remove(st.RawSystemType)) ; lookupILTypeRef.Add(st.RawSystemType, ilTgtRootTyRef)), m)
15737+
theRootTypeWithRemapping.PUntaint ((fun st -> ignore(lookupILTypeRef.Remove(st)) ; lookupILTypeRef.Add(st, ilTgtRootTyRef)), m)
1573815738

1573915739
// Iterate all nested types and force their embedding, to populate the mapping from System.Type --> TyconRef/ILTypeRef.
1574015740
// This is only needed for generated types, because for other types the System.Type objects self-describe
@@ -15767,12 +15767,12 @@ module EstablishTypeDefinitionCores =
1576715767
let ilOrigTypeRef = GetOriginalILTypeRefOfProvidedType (st, m)
1576815768

1576915769
// Record the details so we can map System.Type --> TyconRef
15770-
st.PUntaint ((fun st -> ignore(lookupTyconRef.Remove(st.RawSystemType)) ; lookupTyconRef.Add(st.RawSystemType, nestedTyRef)), m)
15770+
st.PUntaint ((fun st -> ignore(lookupTyconRef.Remove(st)) ; lookupTyconRef.Add(st, nestedTyRef)), m)
1577115771

1577215772
if isGenerated then
1577315773
let ilTgtTyRef = nestedTycon.CompiledRepresentationForNamedType
1577415774
// Record the details so we can map System.Type --> ILTypeRef
15775-
st.PUntaint ((fun st -> ignore(lookupILTypeRef.Remove(st.RawSystemType)) ; lookupILTypeRef.Add(st.RawSystemType, ilTgtTyRef)), m)
15775+
st.PUntaint ((fun st -> ignore(lookupILTypeRef.Remove(st)) ; lookupILTypeRef.Add(st, ilTgtTyRef)), m)
1577615776

1577715777
// Record the details so we can build correct ILTypeDefs during static linking rewriting
1577815778
if not isSuppressRelocate then

0 commit comments

Comments
 (0)