Skip to content

Back compat issue: ref conditional expression allowed without explicit 7.2 #24306

@jskeet

Description

@jskeet

Version Used: Multiple, see below

Steps to Reproduce:

Create a new library project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

Edit Class1.cs (or create a new file):

class BugDemo
{
    static ref int Valid70(bool condition, ref int x, ref int y)
    {
        if (condition)
        {
            return ref x;
        }
        else
        {
            return ref y;
        }
    }

    static ref int Invalid70(bool condition, ref int x, ref int y)
    {
        return ref condition ? ref x : ref y;
    }
}

Build on the 2.0.0 SDK and 2.1.4 SDK.

(I happened to use 2.0.0 in Bash under WSL and 2.1.4 on Windows 10; I don't expect that to make any difference.)

Expected Behavior:

Shouldn't build on either, without LangVersion being explicitly set.

Actual Behavior:

Build with a 2.0 SDK (so C # 7.0): Invalid70 fails to compile (several issues on the return statement).

Build with a 2.1.4 SDK (so C# 7.2): Builds fine

Reasoning

Perhaps the fix to the conditional operator is regarded as a bugfix - but it means that a project which is intended to only require C# 7.0 will build on some machines but not others.

cc @BillWagner, who I discovered this with.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions