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

[release/dev17.4] Don't emit IsReadOnlyAttribute if not available. #14281

Merged
merged 1 commit into from
Nov 9, 2022
Merged
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
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