dotnet/roslyn

ref local gives bad error when inited with ternary initializer

Open

#22.048 aperta il 11 set 2017

Vedi su GitHub
 (5 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-CompilersConcept-Diagnostic ClarityFeature - Readonly ReferencesLanguage-C#Testhelp wanted

Metriche repository

Star
 (20.414 star)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

Version Used: 2.3.1.61919 (57c81319) - on OSX with mono 5.4.0.135

Steps to Reproduce:

Compile this:

using System;

class Driver {
	static int X;
	static void Main () {
		ref int x = X == 0 ? ref X : ref X;
	}
}

Expected Behavior:

It should either compile or give a clear error message that what I'm doing is wrong

Actual Behavior:

Microsoft (R) Visual C# Compiler version 2.3.1.61919 (57c81319)
Copyright (C) Microsoft Corporation. All rights reserved.

ref2.cs(6,24): error CS1525: Invalid expression term 'ref'
ref2.cs(6,24): error CS1003: Syntax error, ':' expected
ref2.cs(6,24): error CS1002: ; expected
ref2.cs(6,30): error CS1001: Identifier expected
ref2.cs(6,30): error CS1002: ; expected
ref2.cs(6,30): error CS1513: } expected
ref2.cs(6,37): error CS1001: Identifier expected

I tried all combinations of adding/removing ref and parens and nothing worked. The only one that gave me something good was ref int x = ref (X == 0 ? X : X); with: error CS1510: A ref or out value must be an assignable variable

Guida contributor