|
| 1 | +using System; |
1 | 2 | using Draco.Compiler.Api.Syntax; |
2 | 3 | using Draco.Compiler.Internal.Binding; |
3 | 4 | using Draco.Compiler.Internal.Symbols; |
@@ -2570,4 +2571,43 @@ public void OrderIndependenceInTypeInference() |
2570 | 2571 | Assert.Equal("List<Exception>", workingListVariable.Type.ToString()); |
2571 | 2572 | Assert.Equal("List<Exception>", flippedListVariable.Type.ToString()); |
2572 | 2573 | } |
| 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 | + } |
2573 | 2613 | } |
0 commit comments