title: "variable '' of type '' referenced from scope '', but it is not defined"
body: <p>I am getting this error message for my code: "variable 'assignVal' of type 'System.Int32' referenced from scope '', but it is not defined"</p>
  
  <p>I checked out </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/16342842/lambda-compilation-throws-variable-of-type-referenced-from-scope-but">Lambda compilation throws &quot;variable &#39;&#39; of type &#39;&#39; referenced from scope &#39;&#39;, but it is not defined&quot;</a> </li>
  <li><a href="https://stackoverflow.com/questions/30556911/variable-of-type-referenced-from-scope-but-it-is-not-defined">variable &#39;&#39; of type &#39;&#39; referenced from scope &#39;&#39;, but it is not defined</a></li>
  </ul>
  
  <p>but unfortunately my sample seems way more simple and still doesn't work for some reason.</p>
  
  <p>This is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq.Expressions;
  using System.Reflection.Emit;
  using System.Threading.Tasks;
  using static System.Linq.Expressions.Expression;
  
  namespace ExpressionTests
  {
      class Program
      {
          static async Task Main(string[] args)
          {
              Console.WriteLine(GetSyncAddExpression()(5) == 6);
              Console.ReadKey();
  
              Console.WriteLine(await GetTaskAddExpression()(5) == 6);
              Console.ReadKey();
          }
  
          private static Func&lt;int, Task&lt;int&gt;&gt; GetTaskAddExpression()
          {
              var fromResultMethod = typeof(Task).GetMethod(nameof(Task.FromResult)).MakeGenericMethod(typeof(int));
  
              var inParam = Parameter(typeof(int), "p1");
              var assignmentValue = Variable(typeof(int), "assignVal");
              var retVal = Variable(typeof(Task&lt;int&gt;));
  
              var lambda = Lambda&lt;Func&lt;int, Task&lt;int&gt;&gt;&gt;(Block(
                  Assign(assignmentValue, Add(inParam, Constant(1))),
                  Assign(retVal, Call(null, fromResultMethod, assignmentValue)),
                  retVal
              ), inParam);
  
              if (Debugger.IsAttached)
                  Debugger.Break();
  
              return lambda.Compile();
          }
  
          private static Func&lt;int, int&gt; GetSyncAddExpression()
          {
              var inParam = Parameter(typeof(int), "p1");
              var assignmentValue = Variable(typeof(int), "assignVal");
              var retVal = Variable(typeof(int));
  
              var lambda = Lambda&lt;Func&lt;int, int&gt;&gt;(Block(
                  Assign(assignmentValue, Add(inParam, Constant(1))),
                  Assign(retVal, assignmentValue),
                  retVal
              ), inParam);
  
              if (Debugger.IsAttached)
                  Debugger.Break();
  
              return lambda.Compile();
          }
      }
  }
  </code></pre>
  
  <p>This code sample seems simple enough that it should be working as is and i'm reusing the expressions, so i don't quite understand why i'm getting this error.</p>