C#: Handle some broken types in BMN.

This commit is contained in:
Michael Nebel
2025-02-28 11:57:08 +01:00
parent e835d8b168
commit fc5a49ef84
2 changed files with 22 additions and 1 deletions

View File

@@ -166,7 +166,9 @@ namespace Semmle.Extraction.CSharp.Entities
// Create typerefs for constructed error types in case they are fully defined elsewhere.
// We cannot use `!this.NeedsPopulation` because this would not be stable as it would depend on
// the assembly that was being extracted at the time.
private bool UsesTypeRef => Symbol.TypeKind == TypeKind.Error || SymbolEqualityComparer.Default.Equals(Symbol.OriginalDefinition, Symbol);
private bool UsesTypeRef =>
Symbol.TypeKind == TypeKind.Error ||
SymbolEqualityComparer.Default.Equals(Symbol.OriginalDefinition, Symbol);
public override Type TypeRef => UsesTypeRef ? (Type)NamedTypeRef.Create(Context, Symbol) : this;
}

View File

@@ -25,6 +25,22 @@ namespace Semmle.Extraction.CSharp.Entities
symbol.ContainingType is not null && ConstructedOrParentIsConstructed(symbol.ContainingType);
}
/// <summary>
/// Returns true in case we suspect this is broken type.
/// </summary>
/// <param name="symbol">Type symbol</param>
private bool IsBrokenType(ITypeSymbol symbol)
{
if (!Context.ExtractionContext.IsStandalone || !symbol.FromSource())
{
return false;
}
// (1) public class { ... } is a broken type and doesn't have a name.
// (2) public class var { ... } is a an allowed type, but it overrides the var keyword for all uses.
// It is probably a better heuristic to treat it as a broken type.
return string.IsNullOrEmpty(symbol.Name) || symbol.Name == "var";
}
public Kinds.TypeKind GetTypeKind(Context cx, bool constructUnderlyingTupleType)
{
switch (Symbol.SpecialType)
@@ -48,6 +64,9 @@ namespace Semmle.Extraction.CSharp.Entities
if (Symbol.IsBoundNullable())
return Kinds.TypeKind.NULLABLE;
if (IsBrokenType(Symbol))
return Kinds.TypeKind.UNKNOWN;
switch (Symbol.TypeKind)
{
case TypeKind.Class: return Kinds.TypeKind.CLASS;