This project is read-only.
1

Resolved

Exception during dictionary initializer compilation

description

It is likely related to strange evaluation behavior of nested dictionary initializer:
using System;
using System.Collections.Generic;

public class Foo {
    public List<int> this[string _] => null;

    static int i;
    Foo foo = new Foo {
        ["Hello" + i++] = {
            1, 2, 3, 4, 5, 6
        }
    };
}
Crashes with:
An item with the same key has already been added.

   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.DeclareLocal(LocalSymbol local, Int32 stack) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 1623
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.DeclareLocals(ImmutableArray`1 locals, Int32 stack) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 1613
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.VisitSequence(BoundSequence node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 628
   at Microsoft.CodeAnalysis.CSharp.BoundSequence.Accept(BoundTreeVisitor visitor) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\obj\Debug\BoundNodes.xml.Generated.cs:line 1870
   at Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.Visit(BoundNode node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\BoundTree\BoundTreeVisitors.cs:line 143
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.BaseVisitExpression(BoundExpression node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 451
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.ReuseOrVisit(BoundExpression node, ExprContext context) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 435
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.VisitExpression(BoundExpression node, ExprContext context) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 463
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.Visit(BoundNode node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 343
   at Microsoft.CodeAnalysis.CSharp.BoundTreeRewriter.VisitConversion(BoundConversion node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\obj\Debug\BoundNodes.xml.Generated.cs:line 6821
   at Microsoft.CodeAnalysis.CSharp.BoundConversion.Accept(BoundTreeVisitor visitor) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\obj\Debug\BoundNodes.xml.Generated.cs:line 1478
   at Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.Visit(BoundNode node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\BoundTree\BoundTreeVisitors.cs:line 143
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.BaseVisitExpression(BoundExpression node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 451
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.ReuseOrVisit(BoundExpression node, ExprContext context) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 435
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.VisitExpression(BoundExpression node, ExprContext context) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 463
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.VisitArguments(ImmutableArray`1 arguments, ImmutableArray`1 parameters) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 1027
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.VisitCall(BoundCall node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 978
   at Microsoft.CodeAnalysis.CSharp.BoundCall.Accept(BoundTreeVisitor visitor) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\obj\Debug\BoundNodes.xml.Generated.cs:line 3530
   at Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.Visit(BoundNode node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\BoundTree\BoundTreeVisitors.cs:line 143
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.BaseVisitExpression(BoundExpression node) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 451
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.ReuseOrVisit(BoundExpression node, ExprContext context) in D:\home\site\!roslyn-sources\master\Src\Compilers\CSharp\Portable\CodeGen\Optimizer.cs:line 435
   at Microsoft.CodeAnalysis.CSharp.CodeGen.StackOptimizerPass1.VisitExpression(BoundExpression node, ExprContext context) in ...