Skip to content

Commit

Permalink
Reduce noisy C# compilation errors (#2067)
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser authored Dec 30, 2024
1 parent 43ad8be commit 78b48c9
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 977 deletions.
11 changes: 9 additions & 2 deletions crates/bindings-csharp/BSATN.Codegen/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ public MemberDeclaration(ISymbol member, ITypeSymbol type, DiagReporter diag)
{
TypeInfo = GetTypeInfo(type);
}
catch (UnresolvedTypeException)
{
// If it's an unresolved type, this error will have been already highlighted by .NET itself, no need to add noise.
// Just add some dummy type to avoid further errors.
// Note that we just use `object` here because emitting the unresolved type's name again would produce more of said noise.
TypeInfo = "SpacetimeDB.BSATN.Unsupported<object>";
}
catch (Exception e)
{
diag.Report(ErrorDescriptor.UnsupportedType, (member, type, e));
// dummy type; can't instantiate an interface, but at least it will produce fewer noisy errors
TypeInfo = $"SpacetimeDB.BSATN.IReadWrite<{Type}>";
// dummy BSATN implementation to produce fewer noisy errors
TypeInfo = $"SpacetimeDB.BSATN.Unsupported<{Type}>";
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/bindings-csharp/BSATN.Codegen/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ IncrementalGeneratorInitializationContext context

public static string MakeRwTypeParam(string typeParam) => typeParam + "RW";

public class UnresolvedTypeException(INamedTypeSymbol type)
: InvalidOperationException($"Could not resolve type {type}") { }

public static string GetTypeInfo(ITypeSymbol type)
{
// We need to distinguish handle nullable reference types specially:
Expand Down Expand Up @@ -120,7 +123,7 @@ static string GetTypeInfoForNamedType(INamedTypeSymbol type)
{
if (type.TypeKind == Microsoft.CodeAnalysis.TypeKind.Error)
{
throw new InvalidOperationException($"Could not resolve type {type}");
throw new UnresolvedTypeException(type);
}
if (type.TypeKind == Microsoft.CodeAnalysis.TypeKind.Enum)
{
Expand Down
14 changes: 14 additions & 0 deletions crates/bindings-csharp/BSATN.Runtime/BSATN/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,17 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
enumerable.GetAlgebraicType(registrar);
}

// This is a dummy type, mainly used by codegen as a diagnostics placeholder to
// reduce amount of noisy compilation errors when a used type is not supported by BSATN.
public readonly struct Unsupported<T> : IReadWrite<T>
{
private static readonly NotSupportedException Exception =
new($"Type {typeof(T)} is not supported by BSATN.");

public T Read(BinaryReader reader) => throw Exception;

public void Write(BinaryWriter writer, T value) => throw Exception;

public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) => throw Exception;
}
Loading

1 comment on commit 78b48c9

@github-actions
Copy link

@github-actions github-actions bot commented on 78b48c9 Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Criterion benchmark results

Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

Please sign in to comment.