Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7628caa

Browse files
calumgranthvitved
authored andcommitted
C#: Avoid typerefs for constructed types.
1 parent aa99269 commit 7628caa

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

  • csharp/extractor/Semmle.Extraction.CSharp/Entities/Types

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public override void Populate(TextWriter trapFile)
2929
return;
3030
}
3131

32-
trapFile.typeref_type((NamedTypeRef)TypeRef, this);
32+
if (UsesTypeRef)
33+
trapFile.typeref_type((NamedTypeRef)TypeRef, this);
3334

3435
if (symbol.IsGenericType)
3536
{
@@ -148,7 +149,14 @@ class NamedTypeFactory : ICachedEntityFactory<INamedTypeSymbol, NamedType>
148149
public NamedType Create(Context cx, INamedTypeSymbol init) => new NamedType(cx, init);
149150
}
150151

151-
public override Type TypeRef => NamedTypeRef.Create(Context, symbol);
152+
// Do not create typerefs of constructed generics as they are always in the current trap file, and there is a possibility
153+
// that a generic could make the typedef ambiguous which leads to performance problems in QL.
154+
// Create typerefs for constructed error types in case they are fully defined elsewhere.
155+
// We cannot use `!this.NeedsPopulation` because this would not be stable as it would depend on
156+
// the assembly that was being extracted at the time.
157+
bool UsesTypeRef => symbol.TypeKind == TypeKind.Error || SymbolEqualityComparer.Default.Equals(symbol.OriginalDefinition, symbol);
158+
159+
public override Type TypeRef => UsesTypeRef ? (Type)NamedTypeRef.Create(Context, symbol) : this;
152160
}
153161

154162
class NamedTypeRef : Type<INamedTypeSymbol>

0 commit comments

Comments
 (0)