Skip to content

Commit

Permalink
C#: Handle some broken types in BMN.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnebel committed Feb 28, 2025
1 parent c4033f2 commit d3aa6e2
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -28,11 +28,32 @@ public static NamedType Create(Context cx, INamedTypeSymbol type) =>
public static NamedType CreateNamedTypeFromTupleType(Context cx, INamedTypeSymbol type) =>
UnderlyingTupleTypeFactory.Instance.CreateEntity(cx, (new SymbolEqualityWrapper(type), typeof(TupleType)), type);

public override bool NeedsPopulation => base.NeedsPopulation || Symbol.TypeKind == TypeKind.Error;
public override bool NeedsPopulation => base.NeedsPopulation || IsBrokenType(Symbol);

/// <summary>
/// Returns true in case we suspect this is broken type.
/// </summary>
/// <param name="symbol">Type symbol</param>
private bool IsBrokenType(ITypeSymbol symbol)
{
if (symbol.TypeKind == TypeKind.Error)
{
return true;
}

if (!Context.ExtractionContext.IsStandalone)
{
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 override void Populate(TextWriter trapFile)
{
if (Symbol.TypeKind == TypeKind.Error)
if (IsBrokenType(Symbol))
{
UnknownType.Create(Context); // make sure this exists so we can use it in `TypeRef::getReferencedType`
Context.ExtractionContext.MissingType(Symbol.ToString()!, Context.FromSource);
@@ -166,7 +187,9 @@ private class UnderlyingTupleTypeFactory : CachedEntityFactory<INamedTypeSymbol,
// 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 =>
IsBrokenType(Symbol) ||
SymbolEqualityComparer.Default.Equals(Symbol.OriginalDefinition, Symbol);

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

0 comments on commit d3aa6e2

Please sign in to comment.