Skip to content

FS0193 "An element with the same key but a different value already exists" when a forwarded type has two nested forwarders #20975

Open

Description

@zion-sati

fsc fails with FS0193 when a referenced assembly forwards a type that has two or more nested types, and the nested forwarders carry nested visibility, which is how ilasm and Mono.Cecil write them. Found while working on NetWasm.

Repro steps

  1. Create three projects. Impl defines the type:

    Impl/Impl.csproj

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net11.0</TargetFramework>
      </PropertyGroup>
    </Project>

    Impl/Outer.cs

    namespace Sample;
    
    public class Outer
    {
        public class First { }
        public class Second { }
    }

    Facade forwards it. It is written in IL so the nested forwarders are emitted as nested public:

    Facade/Facade.ilproj

    <Project Sdk="Microsoft.NET.Sdk.IL/11.0.0">
      <PropertyGroup>
        <TargetFramework>net11.0</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <ProjectReference Include="../Impl/Impl.csproj" />
      </ItemGroup>
    </Project>

    Facade/Facade.il

    .assembly extern Impl { }
    .assembly Facade { }
    .module Facade.dll
    
    .class extern forwarder Sample.Outer { .assembly extern Impl }
    .class extern nested public First { .class extern Sample.Outer }
    .class extern nested public Second { .class extern Sample.Outer }
    

    App references the facade:

    App/App.fsproj

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net11.0</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <Compile Include="Program.fs" />
        <ProjectReference Include="../Facade/Facade.ilproj" />
      </ItemGroup>
    </Project>

    App/Program.fs

    printfn "%s" (Sample.Outer().GetType().FullName)
  2. Run dotnet build App.

Expected behavior

The build succeeds and the program prints Sample.Outer. It does when Facade.il has only one nested forwarder.

Actual behavior

error FS0193 : internal error : An element with the same key but a different value already exists. Key: 'Sample.Outer' [App/App.fsproj]

The cause looks like addNested in ImportILAssemblyTypeForwarders (src/Compiler/Checking/import.fs):

let tcref = lazy ImportILTypeRefUncached (amap ()) m (ILTypeRef.Create(exportedType.ScopeRef, enc, net.Name))
addToTree tree enc exportedType.Name tcref

Each nested forwarder is added under the outer type's name (exportedType.Name) instead of its own (net.Name). The first nested type takes the key Sample.Outer. The second adds the same key with a different value, and ImmutableDictionary.Add throws. With a single nested forwarder there is no clash, but the entry is still stored under the outer type's name.

.NET's own reference facades and Roslyn-built facades don't hit this. They write nested forwarders with flags 0 (no nested visibility), and seekReadTopExportedTypes skips those rows, so they never reach addNested.

Known workarounds

Leave nested types out of the facade's exported types, or write them with flags 0 as Roslyn does. A reference to a nested type resolves through its enclosing type's reference, so the outer type's forwarder is enough.

Related information

  • macOS 26.6 (arm64)
  • .NET SDK 11.0.100, F# compiler 15.2.101.0 for F# 11.0. The F# 10 compiler in SDK 10.0.403 fails the same way.
  • The same code is on main (59c7477).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions