Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,32 @@ public static Conversion GetOutConversion(this ICompoundAssignmentOperation comp
}
}

/// <summary>
/// Gets the underlying <see cref="Conversion"/> information from this <see cref="ICoalesceOperation"/>. This
/// conversion is applied to <see cref="ICoalesceOperation.Value"/> when it is not null.
/// </summary>
/// <remarks>
/// This coalesce operation must have been created from C# code.
/// </remarks>
public static Conversion GetValueConversion(this ICoalesceOperation coalesceExpression)
{
if (coalesceExpression == null)
{
throw new ArgumentNullException(nameof(coalesceExpression));
}

if (coalesceExpression.Language == LanguageNames.CSharp)
{
return (Conversion)((CoalesceOperation)coalesceExpression).ValueConversionConvertible;
}
else
{
throw new ArgumentException(string.Format(CSharpResources.ICoalesceOperationIsNotCSharpCoalesceOperation,
nameof(coalesceExpression)),
nameof(coalesceExpression));
}
}

/// <summary>
/// Gets the underlying element <see cref="Conversion"/> information from this <see cref="ISpreadOperation"/>.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5893,6 +5893,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ISpreadOperationIsNotCSharpSpread" xml:space="preserve">
<value>{0} is not a valid C# spread operation</value>
</data>
<data name="ICoalesceOperationIsNotCSharpCoalesceOperation" xml:space="preserve">
<value>{0} is not a valid C# coalesce operation</value>
</data>
<data name="WRN_FilterIsConstantFalse" xml:space="preserve">
<value>Filter expression is a constant 'false', consider removing the catch clause</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ Microsoft.CodeAnalysis.CSharp.SyntaxKind.ClosedKeyword = 8453 -> Microsoft.CodeA
[RSEXPERIMENTAL006]static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.UnsafeExpression(Microsoft.CodeAnalysis.SyntaxToken keyword, Microsoft.CodeAnalysis.SyntaxToken openParenToken, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax! expression, Microsoft.CodeAnalysis.SyntaxToken closeParenToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.UnsafeExpressionSyntax!
[RSEXPERIMENTAL006]virtual Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor.VisitUnsafeExpression(Microsoft.CodeAnalysis.CSharp.Syntax.UnsafeExpressionSyntax! node) -> void
[RSEXPERIMENTAL006]virtual Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor<TResult>.VisitUnsafeExpression(Microsoft.CodeAnalysis.CSharp.Syntax.UnsafeExpressionSyntax! node) -> TResult?
static Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetValueConversion(this Microsoft.CodeAnalysis.Operations.ICoalesceOperation! coalesceExpression) -> Microsoft.CodeAnalysis.CSharp.Conversion
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#nullable disable

using System;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -1607,5 +1609,108 @@ void M1(int? i, int j, int result)

VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>(compilation, expectedGraph, expectedDiagnostics);
}

[CompilerTrait(CompilerFeature.IOperation)]
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/84905")]
public void GetValueConversion_NullArgumentThrows()
{
ICoalesceOperation nullCoalesce = null;
Assert.Throws<ArgumentNullException>("coalesceExpression", () => nullCoalesce.GetValueConversion());
}

[CompilerTrait(CompilerFeature.IOperation)]
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/84905")]
public void GetValueConversion_IdentityConversion()
{
var source = @"
class C
{
void F(int? input, int alternative)
{
var result = /*<bind>*/input ?? alternative/*</bind>*/;
}
}";

var compilation = CreateCompilation(source);
compilation.VerifyDiagnostics();

var (operation, _) = GetOperationAndSyntaxForTest<BinaryExpressionSyntax>(compilation);
var coalesce = (ICoalesceOperation)operation;

Assert.Equal(Conversion.Identity, coalesce.GetValueConversion());
}

[CompilerTrait(CompilerFeature.IOperation)]
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/84905")]
public void GetValueConversion_BoxingConversion()
{
var source = @"
class C
{
void F(int? input, object alternative)
{
var result = /*<bind>*/input ?? alternative/*</bind>*/;
}
}";

var compilation = CreateCompilation(source);
compilation.VerifyDiagnostics();

var (operation, _) = GetOperationAndSyntaxForTest<BinaryExpressionSyntax>(compilation);
var coalesce = (ICoalesceOperation)operation;

var conversion = coalesce.GetValueConversion();
Assert.True(conversion.IsBoxing);
Assert.Equal(ConversionKind.Boxing, conversion.Kind);
}

[CompilerTrait(CompilerFeature.IOperation)]
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/84905")]
public void GetValueConversion_UserDefinedConversion()
{
var source = @"
class C
{
void F(int? input, C alternative)
{
var result = /*<bind>*/input ?? alternative/*</bind>*/;
}

public static implicit operator C(int i) => null;
}";

var compilation = CreateCompilation(source);
compilation.VerifyDiagnostics();

var (operation, _) = GetOperationAndSyntaxForTest<BinaryExpressionSyntax>(compilation);
var coalesce = (ICoalesceOperation)operation;

var conversion = coalesce.GetValueConversion();
Assert.Equal(ConversionKind.ImplicitUserDefined, conversion.Kind);
Assert.Equal("C.implicit operator C(int)", conversion.MethodSymbol.ToDisplayString());
}

[CompilerTrait(CompilerFeature.IOperation)]
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/84905")]
public void GetValueConversion_FromVisualBasicExtensionThrows()
{
var source = @"
class C
{
void F(int? input, int alternative)
{
var result = /*<bind>*/input ?? alternative/*</bind>*/;
}
}";

var compilation = CreateCompilation(source);
compilation.VerifyDiagnostics();

var (operation, _) = GetOperationAndSyntaxForTest<BinaryExpressionSyntax>(compilation);
var coalesce = (ICoalesceOperation)operation;

// We are calling the VB extension method on an ICoalesceOperation created from C# code, therefore an exception is expected here.
Assert.Throws<ArgumentException>("coalesceExpression", () => Microsoft.CodeAnalysis.VisualBasic.VisualBasicExtensions.GetValueConversion(coalesce));
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@

Microsoft.CodeAnalysis.VisualBasic.VisualBasicExtensions.GetValueConversion(coalesceExpression As Microsoft.CodeAnalysis.Operations.ICoalesceOperation) -> Microsoft.CodeAnalysis.VisualBasic.Conversion
3 changes: 3 additions & 0 deletions src/Compilers/VisualBasic/Portable/VBResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5542,6 +5542,9 @@
<data name="ICompoundAssignmentOperationIsNotVisualBasicCompoundAssignment" xml:space="preserve">
<value>{0} is not a valid Visual Basic compound assignment operation</value>
</data>
<data name="ICoalesceOperationIsNotVisualBasicCoalesceOperation" xml:space="preserve">
<value>{0} is not a valid Visual Basic coalesce operation</value>
</data>
<data name="FEATURE_InterpolatedStrings" xml:space="preserve">
<value>interpolated strings</value>
</data>
Expand Down
22 changes: 22 additions & 0 deletions src/Compilers/VisualBasic/Portable/VisualBasicExtensions.vb
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,28 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End If
End Function

''' <summary>
''' Gets the underlying <see cref="Conversion"/> information from this <see cref="ICoalesceOperation"/>. This
''' conversion is applied to <see cref="ICoalesceOperation.Value"/> when it is not null.
''' </summary>
''' <remarks>
''' This coalesce operation must have been created from Visual Basic code.
''' </remarks>
<Extension>
Public Function GetValueConversion(coalesceExpression As ICoalesceOperation) As Conversion
If coalesceExpression Is Nothing Then
Throw New ArgumentNullException(NameOf(coalesceExpression))
End If

If coalesceExpression.Language = LanguageNames.VisualBasic Then
Return DirectCast(DirectCast(coalesceExpression, CoalesceOperation).ValueConversionConvertible, Conversion)
Else
Throw New ArgumentException(String.Format(VBResources.ICoalesceOperationIsNotVisualBasicCoalesceOperation,
NameOf(coalesceExpression)),
NameOf(coalesceExpression))
End If
End Function

<Extension>
Public Function GetSpeculativeConversion(semanticModel As SemanticModel, position As Integer, expression As ExpressionSyntax, bindingOption As SpeculativeBindingOption) As Conversion
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/VisualBasic/Portable/xlf/VBResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/VisualBasic/Portable/xlf/VBResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading