-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create OverloadingTests.cs * Update OverloadingTests.cs * Added cache to type provider * Cache base types and dedup them * Missing stuff * Update ConstraintSolver_Rules.cs * Update TypeProvider.cs * Update ConstraintSolver.cs * Shuffling logic around * Update SymbolEqualityComparer.cs * Fleshed out hashing a bit
- Loading branch information
1 parent
42b368f
commit 8dc2876
Showing
8 changed files
with
486 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
using System.Diagnostics; | ||
using Draco.Compiler.Api; | ||
using Draco.Compiler.Api.Syntax; | ||
using Draco.Compiler.Internal.Symbols; | ||
using static Draco.Compiler.Api.Syntax.SyntaxFactory; | ||
|
||
namespace Draco.Compiler.Tests.Semantics; | ||
|
||
public sealed class OverloadingTests : SemanticTestsBase | ||
{ | ||
// func foo(l: List<int32>) {} | ||
// func foo(l: List<string>) {} | ||
// func foo<T>(l: List<T>) {} | ||
// func foo(l: IEnumerable<int32>) {} | ||
// func foo(l: IEnumerable<string>) {} | ||
// func foo<T>(l: IEnumerable<T>) {} | ||
private static IEnumerable<DeclarationSyntax> GetGenericListOverloads() => [ | ||
FunctionDeclaration( | ||
"foo", | ||
ParameterList(Parameter("l", GenericType(NameType("List"), NameType("int32")))), | ||
null, | ||
BlockFunctionBody()), | ||
FunctionDeclaration( | ||
"foo", | ||
ParameterList(Parameter("l", GenericType(NameType("List"), NameType("string")))), | ||
null, | ||
BlockFunctionBody()), | ||
FunctionDeclaration( | ||
"foo", | ||
GenericParameterList(GenericParameter("T")), | ||
ParameterList(Parameter("l", GenericType(NameType("List"), NameType("T")))), | ||
null, | ||
BlockFunctionBody()), | ||
FunctionDeclaration( | ||
"foo", | ||
ParameterList(Parameter("l", GenericType(NameType("IEnumerable"), NameType("int32")))), | ||
null, | ||
BlockFunctionBody()), | ||
FunctionDeclaration( | ||
"foo", | ||
ParameterList(Parameter("l", GenericType(NameType("IEnumerable"), NameType("string")))), | ||
null, | ||
BlockFunctionBody()), | ||
FunctionDeclaration( | ||
"foo", | ||
GenericParameterList(GenericParameter("T")), | ||
ParameterList(Parameter("l", GenericType(NameType("IEnumerable"), NameType("T")))), | ||
null, | ||
BlockFunctionBody()), | ||
]; | ||
|
||
// import System.Collections.Generic; | ||
// import System.Linq.Enumerable; | ||
// | ||
// ... foo overloads ... | ||
// | ||
// func main() { | ||
// call(); | ||
// | ||
private static SyntaxTree CreateOverloadTree(CallExpressionSyntax call) => SyntaxTree.Create(CompilationUnit( | ||
new DeclarationSyntax[] | ||
{ | ||
ImportDeclaration("System", "Collections", "Generic"), | ||
ImportDeclaration("System", "Linq", "Enumerable"), | ||
}.Concat(GetGenericListOverloads()) | ||
.Append(FunctionDeclaration( | ||
"main", | ||
ParameterList(), | ||
null, | ||
BlockFunctionBody(ExpressionStatement(call)))))); | ||
|
||
private static FunctionSymbol GetDeclaredFunctionSymbol(Compilation compilation, int index) | ||
{ | ||
var syntaxTree = compilation.SyntaxTrees.Single(); | ||
var syntax = syntaxTree.FindInChildren<FunctionDeclarationSyntax>(index); | ||
Debug.Assert(syntax is not null); | ||
|
||
var semanticModel = compilation.GetSemanticModel(syntaxTree); | ||
var symbol = semanticModel.GetDeclaredSymbol(syntax); | ||
Debug.Assert(symbol!.Name == "foo"); | ||
|
||
return GetInternalSymbol<FunctionSymbol>(symbol); | ||
} | ||
|
||
private static FunctionSymbol GetCalledFunctionSymbol(Compilation compilation) | ||
{ | ||
var syntaxTree = compilation.SyntaxTrees.Single(); | ||
var syntax = syntaxTree.FindInChildren<CallExpressionSyntax>(); | ||
Debug.Assert(syntax is not null); | ||
|
||
var semanticModel = compilation.GetSemanticModel(syntaxTree); | ||
var symbol = semanticModel.GetReferencedSymbol(syntax); | ||
Debug.Assert(symbol!.Name == "foo"); | ||
|
||
return GetInternalSymbol<FunctionSymbol>(symbol); | ||
} | ||
|
||
[Fact] | ||
public void ListInt32Overload() | ||
{ | ||
// foo(List<int32>()); | ||
|
||
// Arrange | ||
var tree = CreateOverloadTree(CallExpression( | ||
NameExpression("foo"), | ||
CallExpression(GenericExpression(NameExpression("List"), NameType("int32"))))); | ||
|
||
// Act | ||
var compilation = CreateCompilation(tree); | ||
var expectedSymbol = GetDeclaredFunctionSymbol(compilation, 0); | ||
var calledSymbol = GetCalledFunctionSymbol(compilation); | ||
|
||
// Assert | ||
Assert.Empty(compilation.Diagnostics); | ||
Assert.Same(expectedSymbol, calledSymbol); | ||
} | ||
|
||
[Fact] | ||
public void ListStringOverload() | ||
{ | ||
// foo(List<string>()); | ||
|
||
// Arrange | ||
var tree = CreateOverloadTree(CallExpression( | ||
NameExpression("foo"), | ||
CallExpression(GenericExpression(NameExpression("List"), NameType("string"))))); | ||
var compilation = CreateCompilation(tree); | ||
|
||
// Act | ||
var expectedSymbol = GetDeclaredFunctionSymbol(compilation, 1); | ||
var calledSymbol = GetCalledFunctionSymbol(compilation); | ||
|
||
// Assert | ||
Assert.Empty(compilation.Diagnostics); | ||
Assert.Same(expectedSymbol, calledSymbol); | ||
} | ||
|
||
[Fact] | ||
public void ListGenericOverload() | ||
{ | ||
// foo(List<bool>()); | ||
|
||
// Arrange | ||
var tree = CreateOverloadTree(CallExpression( | ||
NameExpression("foo"), | ||
CallExpression(GenericExpression(NameExpression("List"), NameType("bool"))))); | ||
var compilation = CreateCompilation(tree); | ||
|
||
// Act | ||
var expectedSymbol = GetDeclaredFunctionSymbol(compilation, 2); | ||
var calledSymbol = GetCalledFunctionSymbol(compilation); | ||
|
||
// Assert | ||
Assert.Empty(compilation.Diagnostics); | ||
Assert.True(calledSymbol.IsGenericInstance); | ||
Assert.Same(expectedSymbol, calledSymbol.GenericDefinition); | ||
} | ||
|
||
[Fact] | ||
public void IEnumerableInt32Overload() | ||
{ | ||
// foo(AsEnumerable(List<int32>())); | ||
|
||
// Arrange | ||
var tree = CreateOverloadTree(CallExpression( | ||
NameExpression("foo"), | ||
CallExpression( | ||
NameExpression("AsEnumerable"), | ||
CallExpression(GenericExpression(NameExpression("List"), NameType("int32")))))); | ||
var compilation = CreateCompilation(tree); | ||
|
||
// Act | ||
var expectedSymbol = GetDeclaredFunctionSymbol(compilation, 3); | ||
var calledSymbol = GetCalledFunctionSymbol(compilation); | ||
|
||
// Assert | ||
Assert.Empty(compilation.Diagnostics); | ||
Assert.Same(expectedSymbol, calledSymbol); | ||
} | ||
|
||
[Fact] | ||
public void IEnumerableStringOverload() | ||
{ | ||
// foo(AsEnumerable(List<string>())); | ||
|
||
// Arrange | ||
var tree = CreateOverloadTree(CallExpression( | ||
NameExpression("foo"), | ||
CallExpression( | ||
NameExpression("AsEnumerable"), | ||
CallExpression(GenericExpression(NameExpression("List"), NameType("string")))))); | ||
var compilation = CreateCompilation(tree); | ||
|
||
// Act | ||
var expectedSymbol = GetDeclaredFunctionSymbol(compilation, 4); | ||
var calledSymbol = GetCalledFunctionSymbol(compilation); | ||
|
||
// Assert | ||
Assert.Empty(compilation.Diagnostics); | ||
Assert.Same(expectedSymbol, calledSymbol); | ||
} | ||
|
||
[Fact] | ||
public void IEnumerableGenericOverload() | ||
{ | ||
// foo(AsEnumerable(List<bool>())); | ||
|
||
// Arrange | ||
var tree = CreateOverloadTree(CallExpression( | ||
NameExpression("foo"), | ||
CallExpression( | ||
NameExpression("AsEnumerable"), | ||
CallExpression(GenericExpression(NameExpression("List"), NameType("bool")))))); | ||
var compilation = CreateCompilation(tree); | ||
|
||
// Act | ||
var expectedSymbol = GetDeclaredFunctionSymbol(compilation, 5); | ||
var calledSymbol = GetCalledFunctionSymbol(compilation); | ||
|
||
// Assert | ||
Assert.Empty(compilation.Diagnostics); | ||
Assert.True(calledSymbol.IsGenericInstance); | ||
Assert.Same(expectedSymbol, calledSymbol.GenericDefinition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.