diff --git a/src/Compiler/CodeGen/IlxGen.fs b/src/Compiler/CodeGen/IlxGen.fs index 829b7e03d20..05a2f0ac1be 100644 --- a/src/Compiler/CodeGen/IlxGen.fs +++ b/src/Compiler/CodeGen/IlxGen.fs @@ -611,7 +611,10 @@ let GenReadOnlyAttribute (g: TcGlobals) = mkILCustomAttribute (g.attrib_IsReadOnlyAttribute.TypeRef, [], [], []) let GenReadOnlyAttributeIfNecessary (g: TcGlobals) ty = - let add = isInByrefTy g ty && g.attrib_IsReadOnlyAttribute.TyconRef.CanDeref + let add = + g.isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable + && isInByrefTy g ty + && g.attrib_IsReadOnlyAttribute.TyconRef.CanDeref if add then let attr = GenReadOnlyAttribute g @@ -2120,7 +2123,11 @@ type AssemblyBuilder(cenv: cenv, anonTypeTable: AnonTypeGenerationTable) as mgbu let ilMethods = [ for propName, fldName, fldTy in flds -> - let attrs = if isStruct then [ GenReadOnlyAttribute g ] else [] + let attrs = + if g.isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable && isStruct then + [ GenReadOnlyAttribute g ] + else + [] mkLdfldMethodDef ("get_" + propName, ILMemberAccess.Public, false, ilTy, fldName, fldTy, attrs) |> g.AddMethodGeneratedAttributes @@ -10894,7 +10901,11 @@ and GenTypeDef cenv mgbuf lazyInitInfo eenv m (tycon: Tycon) = let isStruct = isStructTyconRef tcref let attrs = - if isStruct && not isStatic then + if + g.isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable + && isStruct + && not isStatic + then [ GenReadOnlyAttribute g ] else [] diff --git a/src/Compiler/TypedTree/TcGlobals.fs b/src/Compiler/TypedTree/TcGlobals.fs index 9ab7cf2f723..1e79c9d3ee6 100755 --- a/src/Compiler/TypedTree/TcGlobals.fs +++ b/src/Compiler/TypedTree/TcGlobals.fs @@ -1718,6 +1718,9 @@ type TcGlobals( /// Indicates if we can use System.Array.Empty when emitting IL for empty array literals member val isArrayEmptyAvailable = v_Array_tcref.ILTyconRawMetadata.Methods.FindByName "Empty" |> List.isEmpty |> not + /// Indicates if we can emit the System.Runtime.CompilerServices.IsReadOnlyAttribute + member val isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable = tryFindSysTypeCcu sysCompilerServices "IsReadOnlyAttribute" |> Option.isSome + member _.FindSysTyconRef path nm = findSysTyconRef path nm member _.TryFindSysTyconRef path nm = tryFindSysTyconRef path nm