Skip to content

The compiler automatically generates an IsReadOnlyAttribute on downlevel frameworks even if we have already defined it. #15313

@teo-tsirpanis

Description

@teo-tsirpanis

Please provide a succinct description of the issue.

Repro steps

Source:

#if NETSTANDARD2_0
namespace System.Runtime.CompilerServices

type IsReadOnlyAttribute() =
    inherit System.Attribute()
#endif

namespace fsharp_ns2_repro

[<Struct;System.Runtime.CompilerServices.IsReadOnly>]
type MyRecord = {X : int}

Project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="Library.fs" />
  </ItemGroup>
</Project>

Expected behavior

Project compiles successfully on both frameworks.

Actual behavior

Project compiles successfully on .NET 7, fails to compile on .NET Standard 2.0 with:

FSC : error FS2014: A problem occurred writing the binary 'C:\Users\teo\code\fsharp-ns2-repro\obj\Debug\netstandard2.0\fsharp-ns2-repro.dll': duplicate entry 'System.Runtime.CompilerServices.IsReadOnlyAttribute' in type index table [C:\Users\teo
\code\fsharp-ns2-repro\fsharp-ns2-repro.fsproj::TargetFramework=netstandard2.0]

Known workarounds

Don't bother with IsReadOnlyAttribute and let the compiler apply it when it sees fit. I guessed that it would apply it to MyRecord, but it applies it only on X's getter. It's the same thing in this case, but I tried defining a method to the record, and was not marked as readonly.

Related information

Encountered when updating the .NET SDK from 7.0.2xx to 7.0.3xx (teo-tsirpanis/Farkle#104). Likely introduced in #14941.

Metadata

Metadata

Assignees

Labels

Area-Compiler-CodeGenIlxGen, ilwrite and things at the backendBugImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions