未验证 提交 f364873d 编写于 作者: D dotnet-automerge-bot 提交者: GitHub

Merge pull request #37786 from dotnet/merges/master-to-master-vs-deps

Merge master to master-vs-deps
......@@ -3031,7 +3031,7 @@ static void Main(string[] args)
await TestAsync(text, "global::Program", testNode: false);
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/37310"), Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestInferringThroughGenericFunctionWithNullableReturn()
{
var text =
......@@ -3049,5 +3049,39 @@ static void Main(string[] args)
await TestAsync(text, "global::System.String?");
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestInferringThroughGenericFunctionMissingArgument()
{
var text =
@"class Program
{
static void Main(string[] args)
{
string s = Identity([||]);
}
static T Identity<T>(T value) { return value; }
}";
await TestAsync(text, "global::System.String", testNode: false);
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestInferringThroughGenericFunctionTooManyArguments()
{
var text =
@"class Program
{
static void Main(string[] args)
{
string s = Identity(""test"", [||]);
}
static T Identity<T>(T value) { return value; }
}";
await TestAsync(text, "global::System.Object");
}
}
}
......@@ -34,12 +34,12 @@ internal sealed class RunningDocumentTableEventTracker : IVsRunningDocTableEvent
Contract.ThrowIfNull(runningDocumentTable);
Contract.ThrowIfNull(listener);
// Advise / Unadvise for the RDT is free threaded past 16.0
_foregroundAffinitization = new ForegroundThreadAffinitizedObject(threadingContext, assertIsForeground: false);
_runningDocumentTable = (IVsRunningDocumentTable4)runningDocumentTable;
_editorAdaptersFactoryService = editorAdaptersFactoryService;
_listener = listener;
// Advise / Unadvise for the RDT is free threaded past 16.0
((IVsRunningDocumentTable)_runningDocumentTable).AdviseRunningDocTableEvents(this, out _runningDocumentTableEventsCookie);
}
......
......@@ -184,6 +184,11 @@ protected void AddBraceSuppressOperations(List<SuppressOperation> list, SyntaxNo
// include lambda itself.
firstTokenOfNode = node.Parent.GetFirstToken(includeZeroWidth: true);
}
else if (node.IsKind(SyntaxKindEx.PropertyPatternClause))
{
// include the pattern recursive pattern syntax and/or subpattern
firstTokenOfNode = firstTokenOfNode.GetPreviousToken();
}
// suppress wrapping on whole construct that owns braces and also brace pair itself if
// it is on same line
......
......@@ -370,7 +370,7 @@ public override AdjustSpacesOperation GetAdjustSpacesOperation(SyntaxToken previ
// nullable
if (currentToken.Kind() == SyntaxKind.QuestionToken &&
currentToken.Parent.Kind() == SyntaxKind.NullableType)
currentToken.Parent.IsKind(SyntaxKind.NullableType, SyntaxKind.ClassConstraint))
{
return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
}
......
......@@ -345,7 +345,7 @@ private IEnumerable<TypeInferenceInfo> InferTypeInAnonymousObjectCreation(Anonym
.Select(tupleType => new TypeInferenceInfo(tupleType.TupleElements[index].GetTypeWithAnnotatedNullability()));
}
private IEnumerable<TypeInferenceInfo> InferTypeInAttributeArgument(AttributeArgumentSyntax argument, SyntaxToken? previousToken = null, ArgumentSyntax argumentOpt = null)
private IEnumerable<TypeInferenceInfo> InferTypeInAttributeArgument(AttributeArgumentSyntax argument, SyntaxToken? previousToken = null)
{
if (previousToken.HasValue)
{
......@@ -372,7 +372,7 @@ private IEnumerable<TypeInferenceInfo> InferTypeInConstructorInitializer(Constru
{
var info = SemanticModel.GetSymbolInfo(initializer, CancellationToken);
var methods = info.GetBestOrAllSymbols().OfType<IMethodSymbol>();
return InferTypeInArgument(index, methods, argument);
return InferTypeInArgument(index, methods, argument, parentInvocationExpressionToTypeInfer: null);
}
private IEnumerable<TypeInferenceInfo> InferTypeInObjectCreationExpression(ObjectCreationExpressionSyntax expression, SyntaxToken previousToken)
......@@ -435,7 +435,7 @@ private IEnumerable<TypeInferenceInfo> InferTypeInObjectCreationExpression(Objec
}
var constructors = type.InstanceConstructors.Where(m => m.Parameters.Length > index);
return InferTypeInArgument(index, constructors, argumentOpt);
return InferTypeInArgument(index, constructors, argumentOpt, parentInvocationExpressionToTypeInfer: null);
}
private IEnumerable<TypeInferenceInfo> InferTypeInInvocationExpression(
......@@ -460,7 +460,7 @@ private IEnumerable<TypeInferenceInfo> InferTypeInObjectCreationExpression(Objec
methods = methods.Concat(memberGroupMethods).Distinct();
}
return InferTypeInArgument(index, methods, argumentOpt);
return InferTypeInArgument(index, methods, argumentOpt, invocation);
}
private IEnumerable<TypeInferenceInfo> InferTypeInArgumentList(ArgumentListSyntax argumentList, SyntaxToken previousToken)
......@@ -549,29 +549,26 @@ private IEnumerable<TypeInferenceInfo> InferTypeInAttributeArgument(int index, I
return InferTypeInAttributeArgument(index, methods.Select(m => m.Parameters), argumentOpt);
}
private IEnumerable<TypeInferenceInfo> InferTypeInArgument(int index, IEnumerable<IMethodSymbol> methods, ArgumentSyntax argumentOpt)
private IEnumerable<TypeInferenceInfo> InferTypeInArgument(int index, IEnumerable<IMethodSymbol> methods, ArgumentSyntax argumentOpt, InvocationExpressionSyntax parentInvocationExpressionToTypeInfer)
{
if (argumentOpt != null)
if (parentInvocationExpressionToTypeInfer != null)
{
if (argumentOpt?.Parent?.Parent is InvocationExpressionSyntax invocation)
{
// We're trying to figure out the signature of a method we're an argument to.
// That method may be generic, and we might end up using one of its generic
// type parameters in the type we infer. First, let's see if we can instantiate
// the methods so that the type can be inferred better.
var invocationTypes = this.InferTypes(invocation).Select(t => t.InferredType).ToList();
var instantiatedMethods = methods.Select(m => Instantiate(m, invocationTypes)).ToList();
// Now that we've instantiated the methods, filter down to the ones that
// will actually return a viable type given where this invocation expression
// is.
var filteredMethods = instantiatedMethods.Where(m =>
invocationTypes.Any(t => Compilation.ClassifyConversion(m.ReturnType.WithoutNullability(), t.WithoutNullability()).IsImplicit)).ToList();
// If we filtered down to nothing, then just fall back to the instantiated list.
// this is a best effort after all.
methods = filteredMethods.Any() ? filteredMethods : instantiatedMethods;
}
// We're trying to figure out the signature of a method we're an argument to.
// That method may be generic, and we might end up using one of its generic
// type parameters in the type we infer. First, let's see if we can instantiate
// the methods so that the type can be inferred better.
var invocationTypes = this.InferTypes(parentInvocationExpressionToTypeInfer).Select(t => t.InferredType).ToList();
var instantiatedMethods = methods.Select(m => Instantiate(m, invocationTypes)).ToList();
// Now that we've instantiated the methods, filter down to the ones that
// will actually return a viable type given where this invocation expression
// is.
var filteredMethods = instantiatedMethods.Where(m =>
invocationTypes.Any(t => Compilation.ClassifyConversion(m.ReturnType.WithoutNullability(), t.WithoutNullability()).IsImplicit)).ToList();
// If we filtered down to nothing, then just fall back to the instantiated list.
// this is a best effort after all.
methods = filteredMethods.Any() ? filteredMethods : instantiatedMethods;
}
return InferTypeInArgument(index, methods.Select(m => m.Parameters), argumentOpt);
......@@ -618,10 +615,9 @@ private IMethodSymbol Instantiate(IMethodSymbol method, IList<ITypeSymbol> invoc
return method;
}
// TODO: pass nullability once https://github.com/dotnet/roslyn/issues/37310 is fixed
var typeArguments = method.ConstructedFrom.TypeParameters
.Select(tp => (bestMap.GetValueOrDefault(tp) ?? tp).WithoutNullability()).ToArray();
return method.ConstructedFrom.Construct(typeArguments);
.Select(tp => bestMap.GetValueOrDefault(tp) ?? tp).ToArray();
return method.ConstructedFrom.ConstructWithNullability(typeArguments);
}
private Dictionary<ITypeParameterSymbol, ITypeSymbol> DetermineTypeParameterMapping(ITypeSymbol inferredType, ITypeSymbol returnType)
......
......@@ -9280,5 +9280,75 @@ public void FixMyType()
}
}", changedOptionSet: changingOptions);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task ClassConstraint()
{
await AssertFormatAsync(
@"
class Program<T>
where T : class?
{
}",
@"
class Program<T>
where T : class ?
{
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task SingleLinePropertyPattern1()
{
await AssertFormatAsync(
@"
using System.Collections.Generic;
class Program
{
public void FixMyType()
{
_ = new List<int>() is
{
Count: { },
};
}
}",
@"
using System.Collections.Generic;
class Program
{
public void FixMyType()
{
_ = new List<int>() is
{
Count:{},
};
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task SingleLinePropertyPattern2()
{
await AssertFormatAsync(
@"
using System.Collections.Generic;
class Program
{
public void FixMyType(object o)
{
_ = o is List<int> { Count: { } };
}
}",
@"
using System.Collections.Generic;
class Program
{
public void FixMyType(object o)
{
_ = o is List<int>{Count:{}};
}
}");
}
}
}
......@@ -115,5 +115,10 @@ public static INamedTypeSymbol ConstructWithNullability(this INamedTypeSymbol ty
{
return typeSymbol.Construct(typeArguments.SelectAsArray(t => t.WithoutNullability()), typeArguments.SelectAsArray(t => t.GetNullability()));
}
public static IMethodSymbol ConstructWithNullability(this IMethodSymbol methodSymbol, params ITypeSymbol[] typeArguments)
{
return methodSymbol.Construct(typeArguments.SelectAsArray(t => t.WithoutNullability()), typeArguments.SelectAsArray(t => t.GetNullability()));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册