Skip to content

Commit

Permalink
Fixed a stupid bug where arity wasn't taken into account when multipl…
Browse files Browse the repository at this point in the history
…e types share the same name.
  • Loading branch information
MeltyPlayer committed Apr 24, 2024
1 parent c85189a commit e089768
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
55 changes: 55 additions & 0 deletions Schema Tests/readOnly/ReadOnlyReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,60 @@ public interface IReadOnlyWrapper {
""");
}

[Test]
public void TestGenericInOtherNamespace() {
ReadOnlyGeneratorTestUtil.AssertGenerated(
"""
using schema.readOnly;
using foo.bar.wrong;
using foo.bar.correct;
namespace foo.bar.wrong {
[GenerateReadOnly]
public partial interface IOther;
}
namespace foo.bar.correct {
[GenerateReadOnly]
public partial interface IOther<T> : wrong.IOther;
}
namespace foo.bar {
[GenerateReadOnly]
public partial interface IWrapper<T> {
IReadOnlyOther<T> Field { get; set; }
}
}
""",
"""
namespace foo.bar.wrong {
public partial interface IOther : IReadOnlyOther;
public interface IReadOnlyOther;
}
""",
"""
namespace foo.bar.correct {
public partial interface IOther<T> : IReadOnlyOther<T>;
public interface IReadOnlyOther<out T> : foo.bar.wrong.IReadOnlyOther;
}
""",
"""
namespace foo.bar {
public partial interface IWrapper<T> : IReadOnlyWrapper<T> {
correct.IReadOnlyOther<T> IReadOnlyWrapper<T>.Field => Field;
}
public interface IReadOnlyWrapper<T> {
public correct.IReadOnlyOther<T> Field { get; }
}
}
""");
}
}
}
33 changes: 21 additions & 12 deletions Schema/src/readOnly/ReadOnlyTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,14 +594,16 @@ public static string GetConstInterfaceName(
return $"{PREFIX}{baseName}";
}

public static IEnumerable<INamedTypeSymbol> LookupTypesWithName(
public static IEnumerable<INamedTypeSymbol> LookupTypesWithNameAndArity(
SemanticModel semanticModel,
TypeDeclarationSyntax syntax,
string searchString)
string searchString,
int arity)
=> semanticModel
.LookupNamespacesAndTypes(syntax.SpanStart, null, searchString)
.Where(symbol => symbol is INamedTypeSymbol)
.Select(symbol => symbol as INamedTypeSymbol);
.Select(symbol => symbol as INamedTypeSymbol)
.Where(symbol => symbol.Arity == arity);

public static IEnumerable<string> GetNamespaceOfType(
this ITypeSymbol typeSymbol,
Expand All @@ -614,21 +616,27 @@ public static IEnumerable<string> GetNamespaceOfType(
var nameWithoutPrefix
= typeName.Substring(
ReadOnlyTypeGeneratorUtil.PREFIX.Length);
var arity = typeSymbol.GetArity();

var typesWithName
= LookupTypesWithName(semanticModel, syntax, nameWithoutPrefix)
= LookupTypesWithNameAndArity(semanticModel,
syntax,
nameWithoutPrefix,
arity)
.ToArray();
if (typesWithName.Length == 0) {
typesWithName = LookupTypesWithName(semanticModel,
syntax,
$"B{nameWithoutPrefix}")
typesWithName = LookupTypesWithNameAndArity(semanticModel,
syntax,
$"B{nameWithoutPrefix}",
arity)
.ToArray();
}

if (typesWithName.Length == 0) {
typesWithName = LookupTypesWithName(semanticModel,
syntax,
$"I{nameWithoutPrefix}")
typesWithName = LookupTypesWithNameAndArity(semanticModel,
syntax,
$"I{nameWithoutPrefix}",
arity)
.ToArray();
}

Expand Down Expand Up @@ -720,8 +728,9 @@ public static bool IsCastNeeded(this ITypeSymbol symbol,
return true;
}

return symbol.HasBuiltInReadOnlyType_(out _, out var canImplicitlyConvert) &&
!canImplicitlyConvert;
return symbol.HasBuiltInReadOnlyType_(out _,
out var canImplicitlyConvert) &&
!canImplicitlyConvert;
}
}
}

0 comments on commit e089768

Please sign in to comment.