Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -114115,5 +114115,77 @@ public Base(bool b) {}
// Program(int x) : base((T() || M<string?>(out string? s6)) && s6.Length > 1) {} // 7
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s6").WithLocation(48, 66));
}

[Fact]
[WorkItem(38183, "https://github.com/dotnet/roslyn/issues/38183")]
public void UnboundGenericTypeReference_StructConstraint()
{
var source =
@"class Program
{
static void Main(string[] args)
{
F<Boxed<int>>();
}

static void F<T>()
{
if (typeof(T).GetGenericTypeDefinition() == typeof(Boxed<>))
{
}
}
}

class Boxed<T>
where T : struct
{
public bool Equals(Boxed<T>? other) =>
false;

public override bool Equals(object? obj) =>
Equals(obj as Boxed<T>);

public override int GetHashCode() =>
0;
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue(TestOptions.ReleaseExe));
CompileAndVerify(comp, expectedOutput: "");
}

[Fact]
[WorkItem(38183, "https://github.com/dotnet/roslyn/issues/38183")]
public void UnboundGenericTypeReference_ClassConstraint()
{
var source =
@"class Program
{
static void Main(string[] args)
{
F<Boxed<object>>();
}

static void F<T>()
{
if (typeof(T).GetGenericTypeDefinition() == typeof(Boxed<>))
{
}
}
}

class Boxed<T>
where T : class
{
public bool Equals(Boxed<T>? other) =>
false;

public override bool Equals(object? obj) =>
Equals(obj as Boxed<T>);

public override int GetHashCode() =>
0;
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue(TestOptions.ReleaseExe));
CompileAndVerify(comp, expectedOutput: "");
}
}
}
11 changes: 6 additions & 5 deletions src/Compilers/Core/Portable/CodeGen/TokenMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CodeGen
Expand All @@ -25,9 +21,14 @@ internal sealed class TokenMap<T> where T : class
{
private readonly ConcurrentDictionary<T, uint> _itemIdentityToToken = new ConcurrentDictionary<T, uint>(ReferenceEqualityComparer.Instance);

private readonly Dictionary<T, uint> _itemToToken = new Dictionary<T, uint>();
private readonly Dictionary<T, uint> _itemToToken;
private readonly ArrayBuilder<T> _items = new ArrayBuilder<T>();

internal TokenMap(IEqualityComparer<T> comparer)
{
_itemToToken = new Dictionary<T, uint>(comparer);
}

public uint GetOrAddTokenFor(T item, out bool referenceAdded)
{
uint tmp;
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/Emit/CommonPEModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal abstract class CommonPEModuleBuilder : Cci.IUnit, Cci.IModuleReference
internal Cci.IMethodReference DebugEntryPoint;

private readonly ConcurrentDictionary<IMethodSymbol, Cci.IMethodBody> _methodBodyMap;
private readonly TokenMap<Cci.IReference> _referencesInILMap = new TokenMap<Cci.IReference>();
private readonly TokenMap<Cci.IReference> _referencesInILMap = new TokenMap<Cci.IReference>(MetadataEntityReferenceComparer.ConsiderEverything);
private readonly ItemTokenMap<string> _stringsInILMap = new ItemTokenMap<string>();
private readonly ItemTokenMap<Cci.DebugSourceDocument> _sourceDocumentsInILMap = new ItemTokenMap<Cci.DebugSourceDocument>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.CodeAnalysis.Symbols;

namespace Microsoft.CodeAnalysis.Emit
{
internal sealed class MetadataEntityReferenceComparer : IEqualityComparer<Cci.IReference>
{
internal static readonly MetadataEntityReferenceComparer ConsiderEverything = new MetadataEntityReferenceComparer(TypeCompareKind.ConsiderEverything);

private readonly TypeCompareKind _compareKind;

private MetadataEntityReferenceComparer(TypeCompareKind compareKind)
{
_compareKind = compareKind;
}

public bool Equals(Cci.IReference x, Cci.IReference y)
{
if (x is null)
{
return y is null;
}
else if (ReferenceEquals(x, y))
{
return true;
}
else if (x is ISymbolInternal sx && y is ISymbolInternal sy)
{
return sx.Equals(sy, _compareKind);
}
else
{
return x.Equals(y);
}
}

public int GetHashCode(Cci.IReference obj)
{
return obj?.GetHashCode() ?? 0;
}
}
}