Skip to content

Unresolved type symbol #1006

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

Merged
merged 2 commits into from
Aug 5, 2022
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
8 changes: 4 additions & 4 deletions src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ private bool TryResolveTypeRef(string text, [NotNullWhen(true)] out TypeReferenc
// NOTE: This does not visit types as in class/struct, but visits *references* to types. Like from methods or fields.
private IEnumerable<Symbol> VisitType(XmlElement type)
{
return
TryResolveTypeRef(type.InnerText, out var r)
? new[] { r }
: Array.Empty<Symbol>();
return new[]
{
new UnresolvedTypeReference(type.InnerText)
};
}

private IEnumerable<Symbol> VisitStruct(XmlElement @struct)
Expand Down
4 changes: 4 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Symbols/SymbolVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ protected virtual FieldSymbol VisitField(FieldSymbol fieldSymbol)
/// <remarks>
/// The order in which the parts of the struct are visited is kept as an implementation detail. Do not rely on this order.
/// </remarks>
/// <remarks>
/// By default visiting <see cref="UnresolvedTypeReference"/> will throw. Visitors involved in type resolution should override this method directly.
/// </remarks>
protected virtual TypeReference VisitTypeReference(TypeReference typeReference)
{
if (typeReference is ExternalTypeReference etr) return VisitExternalTypeReference(etr);
if (typeReference is InternalTypeReference itr) return VisitInternalTypeReference(itr);
if (typeReference is UnresolvedTypeReference utr) UnresolvedTypeReference.ThrowInvalidSymbol();
return ThrowUnknownSymbol<TypeReference>(typeReference);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// Represents an unresolved <see cref="TypeReference"/>. The Text is arbitrary data that should be interpreted into another <see cref="TypeReference"/>.
/// Visitors are free to throw <see cref="InvalidOperationException"/> using <see cref="ThrowInvalidSymbol"/> when they encounter this symbol, but type resolution should've already happened.
/// </summary>
/// <param name="Text">Arbitrary text to interpret</param>
public sealed record UnresolvedTypeReference(string Text) : TypeReference
{
/// <summary>
/// Throw helper to use when encountering <see cref="UnresolvedTypeReference"/> when type resolution should've already happened.
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public static void ThrowInvalidSymbol()
{
throw new InvalidOperationException
($"Visited {nameof(UnresolvedTypeReference)}, but type resolution should have already happened.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Silk.NET.SilkTouch.IntegrationTests;
[UsesVerify]
public class StructIntegrationTests
{
[Fact,
[Fact(Skip = "TODO: Reenable after proper type support"),
Trait("Category", "Integration"),
Trait("Source Language", "C++"),
Trait("Target Language", "C#"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void CorrectType()

var symbol = Assert.Single(symbols);
var field = Assert.IsType<FieldSymbol>(symbol);
var type = Assert.IsType<ExternalTypeReference>(field.Type);
Assert.Equal("int", type.TypeIdentifier.Value);
Assert.Null(type.Namespace);
var type = Assert.IsType<UnresolvedTypeReference>(field.Type);
Assert.Equal("int", type.Text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Moq;
using Moq.Protected;
using Xunit;

namespace Silk.NET.SilkTouch.Symbols.Tests.SymbolVisitorTests;

public class UnresolvedTypeReferenceTests
{
[Fact, Trait("Category", "Symbols")]
public void VisitingUnresolvedRefThrows()
{
var symbol = new UnresolvedTypeReference("");
var visitor = new Mock<SymbolVisitor>
{
CallBase = true
};

Assert.Throws<InvalidOperationException>(() => visitor.Object.Visit(symbol));
}
}