Skip to content

Commit a2c40cc

Browse files
[release/dev17.4] Don't emit IsReadOnlyAttribute if not available. (#14281)
Co-authored-by: nojaf <[email protected]>
1 parent 27c5d89 commit a2c40cc

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Compiler/CodeGen/IlxGen.fs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,10 @@ let GenReadOnlyAttribute (g: TcGlobals) =
611611
mkILCustomAttribute (g.attrib_IsReadOnlyAttribute.TypeRef, [], [], [])
612612

613613
let GenReadOnlyAttributeIfNecessary (g: TcGlobals) ty =
614-
let add = isInByrefTy g ty && g.attrib_IsReadOnlyAttribute.TyconRef.CanDeref
614+
let add =
615+
g.isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable
616+
&& isInByrefTy g ty
617+
&& g.attrib_IsReadOnlyAttribute.TyconRef.CanDeref
615618

616619
if add then
617620
let attr = GenReadOnlyAttribute g
@@ -2120,7 +2123,11 @@ type AssemblyBuilder(cenv: cenv, anonTypeTable: AnonTypeGenerationTable) as mgbu
21202123
let ilMethods =
21212124
[
21222125
for propName, fldName, fldTy in flds ->
2123-
let attrs = if isStruct then [ GenReadOnlyAttribute g ] else []
2126+
let attrs =
2127+
if g.isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable && isStruct then
2128+
[ GenReadOnlyAttribute g ]
2129+
else
2130+
[]
21242131

21252132
mkLdfldMethodDef ("get_" + propName, ILMemberAccess.Public, false, ilTy, fldName, fldTy, attrs)
21262133
|> g.AddMethodGeneratedAttributes
@@ -10894,7 +10901,11 @@ and GenTypeDef cenv mgbuf lazyInitInfo eenv m (tycon: Tycon) =
1089410901
let isStruct = isStructTyconRef tcref
1089510902

1089610903
let attrs =
10897-
if isStruct && not isStatic then
10904+
if
10905+
g.isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable
10906+
&& isStruct
10907+
&& not isStatic
10908+
then
1089810909
[ GenReadOnlyAttribute g ]
1089910910
else
1090010911
[]

src/Compiler/TypedTree/TcGlobals.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,9 @@ type TcGlobals(
17181718
/// Indicates if we can use System.Array.Empty when emitting IL for empty array literals
17191719
member val isArrayEmptyAvailable = v_Array_tcref.ILTyconRawMetadata.Methods.FindByName "Empty" |> List.isEmpty |> not
17201720

1721+
/// Indicates if we can emit the System.Runtime.CompilerServices.IsReadOnlyAttribute
1722+
member val isSystem_Runtime_CompilerServices_IsReadOnlyAttributeAvailable = tryFindSysTypeCcu sysCompilerServices "IsReadOnlyAttribute" |> Option.isSome
1723+
17211724
member _.FindSysTyconRef path nm = findSysTyconRef path nm
17221725

17231726
member _.TryFindSysTyconRef path nm = tryFindSysTyconRef path nm

0 commit comments

Comments
 (0)