Roslyn failed to compile code

前端 未结 4 2077
臣服心动
臣服心动 2021-01-30 07:46

After I have migrated my project from VS2013 to VS2015 the project no longer builds. A compilation error occurs in the following LINQ statement:

static void Mai         


        
相关标签:
4条回答
  • 2021-01-30 08:27

    Since I got schooled so hard in the bug report, I'm going to try to explain this myself.


    Imagine T is some user-defined type with an implicit cast to bool that alternates between false and true, starting with false. As far as the compiler knows, the dynamic first argument to the first && might evaluate to that type, so it has to be pessimistic.

    If, then, it let the code compile, this could happen:

    • When the dynamic binder evaluates the first &&, it does the following:
      • Evaluate the first argument
      • It's a T - implicitly cast it to bool.
      • Oh, it's false, so we don't need to evaluate the second argument.
      • Make the result of the && evaluate as the first argument. (No, not false, for some reason.)
    • When the dynamic binder evaluates the second &&, it does the following:
      • Evaluate the first argument.
      • It's a T - implicitly cast it to bool.
      • Oh, it's true, so evaluate the second argument.
      • ... Oh crap, b isn't assigned.

    In spec terms, in short, there are special "definite assignment" rules that let us say not only whether a variable is "definitely assigned" or "not definitely assigned", but also if it is "definitely assigned after false statement" or "definitely assigned after true statement".

    These exist so that when dealing with && and || (and ! and ?? and ?:) the compiler can examine whether variables may be assigned in particular branches of a complex boolean expression.

    However, these only work while the expressions' types remain boolean. When part of the expression is dynamic (or a non-boolean static type) we can no longer reliably say that the expression is true or false - the next time we cast it to bool to decide which branch to take, it may have changed its mind.


    Update: this has now been resolved and documented:

    The definite assignment rules implemented by previous compilers for dynamic expressions allowed some cases of code that could result in variables being read that are not definitely assigned. See https://github.com/dotnet/roslyn/issues/4509 for one report of this.

    ...

    Because of this possibility the compiler must not allow this program to be compiled if val has no initial value. Previous versions of the compiler (prior to VS2015) allowed this program to compile even if val has no initial value. Roslyn now diagnoses this attempt to read a possibly uninitialized variable.

    0 讨论(0)
  • 2021-01-30 08:39

    This does appear to be a bug, or at the least a regression, in the Roslyn compiler. The following bug has been filed to track it:

    https://github.com/dotnet/roslyn/issues/4509

    In the meantime, Jon's excellent answer has a couple of work arounds.

    0 讨论(0)
  • 2021-01-30 08:40

    This is not a bug. See https://github.com/dotnet/roslyn/issues/4509#issuecomment-130872713 for an example of how a dynamic expression of this form can leave such an out variable unassigned.

    0 讨论(0)
  • 2021-01-30 08:47

    What does cause this issue?

    Looks like a compiler bug to me. At least, it did. Although the decimal.TryParse(v, out a) and decimal.TryParse(v, out b) expressions are evaluated dynamically, I expected the compiler to still understand that by the time it reaches a <= b, both a and b are definitely assigned. Even with the weirdnesses you can come up with in dynamic typing, I'd expect to only ever evaluate a <= b after evaluating both of the TryParse calls.

    However, it turns out that through operator and conversion tricky, it's entirely feasible to have an expression A && B && C which evaluates A and C but not B - if you're cunning enough. See the Roslyn bug report for Neal Gafter's ingenious example.

    Making that work with dynamic is even harder - the semantics involved when the operands are dynamic are harder to describe, because in order to perform overload resolution, you need to evaluate operands to find out what types are involved, which can be counter-intuitive. However, again Neal has come up with an example which shows that the compiler error is required... this isn't a bug, it's a bug fix. Huge amounts of kudos to Neal for proving it.

    Is it possible to fix it through compiler settings?

    No, but there are alternatives which avoid the error.

    Firstly, you could stop it from being dynamic - if you know that you'll only ever use strings, then you could use IEnumerable<string> or give the range variable v a type of string (i.e. from string v in array). That would be my preferred option.

    If you really need to keep it dynamic, just give b a value to start with:

    decimal a, b = 0m;
    

    This won't do any harm - we know that actually your dynamic evaluation won't do anything crazy, so you'll still end up assigning a value to b before you use it, making the initial value irrelevant.

    Additionally, it seems that adding parentheses works too:

    where decimal.TryParse(v, out a) && (decimal.TryParse("15", out b) && a <= b)
    

    That changes the point at which various pieces of overload resolution are triggered, and happens to make the compiler happy.

    There is one issue still remaining - the spec's rules on definite assignment with the && operator need to be clarified to state that they only apply when the && operator is being used in its "regular" implementation with two bool operands. I'll try to make sure this is fixed for the next ECMA standard.

    0 讨论(0)
提交回复
热议问题