Skip to content

Commit

Permalink
[release/dev17.4] Don't emit IsReadOnlyAttribute if not available. (#…
Browse files Browse the repository at this point in the history
…14281)

Co-authored-by: nojaf <[email protected]>
  • Loading branch information
github-actions[bot] and nojaf authored Nov 9, 2022
1 parent 27c5d89 commit a2c40cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Compiler/CodeGen/IlxGen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
[]
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/TypedTree/TcGlobals.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2c40cc

Please sign in to comment.