Skip to content

Commit fc5a49e

Browse files
committed
C#: Handle some broken types in BMN.
1 parent e835d8b commit fc5a49e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ private class UnderlyingTupleTypeFactory : CachedEntityFactory<INamedTypeSymbol,
166166
// Create typerefs for constructed error types in case they are fully defined elsewhere.
167167
// We cannot use `!this.NeedsPopulation` because this would not be stable as it would depend on
168168
// the assembly that was being extracted at the time.
169-
private bool UsesTypeRef => Symbol.TypeKind == TypeKind.Error || SymbolEqualityComparer.Default.Equals(Symbol.OriginalDefinition, Symbol);
169+
private bool UsesTypeRef =>
170+
Symbol.TypeKind == TypeKind.Error ||
171+
SymbolEqualityComparer.Default.Equals(Symbol.OriginalDefinition, Symbol);
170172

171173
public override Type TypeRef => UsesTypeRef ? (Type)NamedTypeRef.Create(Context, Symbol) : this;
172174
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ public static bool ConstructedOrParentIsConstructed(INamedTypeSymbol symbol)
2525
symbol.ContainingType is not null && ConstructedOrParentIsConstructed(symbol.ContainingType);
2626
}
2727

28+
/// <summary>
29+
/// Returns true in case we suspect this is broken type.
30+
/// </summary>
31+
/// <param name="symbol">Type symbol</param>
32+
private bool IsBrokenType(ITypeSymbol symbol)
33+
{
34+
if (!Context.ExtractionContext.IsStandalone || !symbol.FromSource())
35+
{
36+
return false;
37+
}
38+
// (1) public class { ... } is a broken type and doesn't have a name.
39+
// (2) public class var { ... } is a an allowed type, but it overrides the var keyword for all uses.
40+
// It is probably a better heuristic to treat it as a broken type.
41+
return string.IsNullOrEmpty(symbol.Name) || symbol.Name == "var";
42+
}
43+
2844
public Kinds.TypeKind GetTypeKind(Context cx, bool constructUnderlyingTupleType)
2945
{
3046
switch (Symbol.SpecialType)
@@ -48,6 +64,9 @@ public Kinds.TypeKind GetTypeKind(Context cx, bool constructUnderlyingTupleType)
4864
if (Symbol.IsBoundNullable())
4965
return Kinds.TypeKind.NULLABLE;
5066

67+
if (IsBrokenType(Symbol))
68+
return Kinds.TypeKind.UNKNOWN;
69+
5170
switch (Symbol.TypeKind)
5271
{
5372
case TypeKind.Class: return Kinds.TypeKind.CLASS;

0 commit comments

Comments
 (0)