Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce noisy C# compilation errors #2067

Merged
merged 6 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading