Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Inline Temporary Variable Refactoring produces invalid code (VS2017 RTM) #17746

Closed
axel-habermaier opened this issue Mar 11, 2017 · 3 comments

Comments

@axel-habermaier
Copy link
Contributor

Version Used:
VS 2017 RTM

Steps to Reproduce:

  1. Add the following code to a C# project:
class X
{
	void M<T>(T o) // where T : class does not make a difference
	{
		var x = o as X;
		if (x != null)
			;
	}
}
  1. Invoke the "Inline Temporary Variable" refactoring on variable x.

Actual Behavior:
The refactoring preview shows that the generated code is going to be invalid. The refactoring can still be applied, yielding

class X
{
	void M<T>(T o)
	{
		if (o is X x)
			;
	}
}

The following compiler error results:
error CS8121: An expression of type T cannot be handled by a pattern of type X.

Expected Behavior:
Ideally, the compiler would support the code generated by the refactoring, as I understand the is pattern match in this case to be semantically equivalent to the original code.

At the very least, though, the refactoring should not be offered by the IDE.

@alrz
Copy link
Member

alrz commented Mar 11, 2017

This was reported before and is per spec as (X)o is not permitted. Perhaps the analyzer should check convertibility of the expression in the same sense of direct cast to cover corner-cases until this is fixed in the compiler (if possible). /cc @CyrusNajmabadi

@alrz
Copy link
Member

alrz commented Mar 11, 2017

Related: dotnet/csharplang#154

@axel-habermaier
Copy link
Contributor Author

axel-habermaier commented Mar 11, 2017

Thanks @alrz. A very unfortunate language limitation, especially given that the refactoring is offered (so even the IDE does not expect this limitation 😄).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants