Skip to content

Commit 83c160b

Browse files
committed
Update TypeCheckingTests.cs
1 parent 3a79e7a commit 83c160b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/Draco.Compiler.Tests/Semantics/TypeCheckingTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Draco.Compiler.Api.Syntax;
23
using Draco.Compiler.Internal.Binding;
34
using Draco.Compiler.Internal.Symbols;
@@ -2570,4 +2571,43 @@ public void OrderIndependenceInTypeInference()
25702571
Assert.Equal("List<Exception>", workingListVariable.Type.ToString());
25712572
Assert.Equal("List<Exception>", flippedListVariable.Type.ToString());
25722573
}
2574+
2575+
[Fact]
2576+
public void InferListElementTypeFromMultipleAddsAndUsingIndexerCompoundOperator()
2577+
{
2578+
// import System.Collections.Generic;
2579+
// func main() {
2580+
// var list = List();
2581+
// list.Add(0);
2582+
// list.Add(1);
2583+
// list[0] *= 2;
2584+
// }
2585+
var main = SyntaxTree.Create(CompilationUnit(
2586+
ImportDeclaration("System", "Collections", "Generic"),
2587+
FunctionDeclaration(
2588+
"main",
2589+
ParameterList(),
2590+
null,
2591+
BlockFunctionBody(
2592+
DeclarationStatement(VarDeclaration("list", null, CallExpression(NameExpression("List")))),
2593+
ExpressionStatement(CallExpression(MemberExpression(NameExpression("list"), "Add"), LiteralExpression(0))),
2594+
ExpressionStatement(CallExpression(MemberExpression(NameExpression("list"), "Add"), LiteralExpression(1))),
2595+
ExpressionStatement(BinaryExpression(
2596+
IndexExpression(NameExpression("list"), LiteralExpression(0)),
2597+
StarAssign,
2598+
LiteralExpression(2)))))));
2599+
2600+
// Act
2601+
var compilation = CreateCompilation(main);
2602+
var semanticModel = compilation.GetSemanticModel(main);
2603+
var diags = semanticModel.Diagnostics;
2604+
2605+
var listDecl = main.GetNode<VariableDeclarationSyntax>(0);
2606+
var listSym = GetInternalSymbol<LocalSymbol>(semanticModel.GetDeclaredSymbol(listDecl));
2607+
2608+
// Assert
2609+
Assert.Empty(diags);
2610+
Assert.False(listSym.IsError);
2611+
Assert.Equal("List<int32>", listSym.Type.ToString());
2612+
}
25732613
}

0 commit comments

Comments
 (0)