提交 1afc69a2 编写于 作者: A AlekseyTs 提交者: GitHub

Merge pull request #16354 from AlekseyTs/Issue12182

Improve code fix experience around constructor invocations.
......@@ -4684,10 +4684,8 @@ private BoundExpression BindTypeParameterCreationExpression(ObjectCreationExpres
// CS0122: 'MyBase.MyBase' is inaccessible due to its protection level
diagnostics.Add(ErrorCode.ERR_BadAccess, errorLocation, result.ValidResult.Member);
}
else if (!analyzedArguments.HasErrors)
else
{
// If the arguments had an error reported then do not report further errors for
// overload resolution failure.
result.ReportDiagnostics(this, errorLocation, diagnostics,
errorName, null, analyzedArguments, candidateConstructors, typeContainingConstructors, null);
}
......
......@@ -894,6 +894,13 @@ private static bool HadLambdaConversionError(DiagnosticBag diagnostics, Analyzed
int arg)
{
BoundExpression argument = arguments.Argument(arg);
if (argument.HasAnyErrors)
{
// If the argument had an error reported then do not report further errors for
// overload resolution failure.
return;
}
int parm = badArg.Result.ParameterFromArgument(arg);
SourceLocation sourceLocation = new SourceLocation(argument.Syntax);
......
......@@ -5936,55 +5936,58 @@ public class SomeAttr1: Attribute
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (7,15): warning CS0618: 'Test.F1' is obsolete: 'F1 is obsolete'
// [SomeAttr(F1)]
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F1").WithArguments("Test.F1", "F1 is obsolete"),
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F1").WithArguments("Test.F1", "F1 is obsolete").WithLocation(7, 15),
// (15,15): error CS0619: 'Test.F2' is obsolete: 'F2 is obsolete'
// [SomeAttr(F2)]
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F2").WithArguments("Test.F2", "F2 is obsolete"),
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F2").WithArguments("Test.F2", "F2 is obsolete").WithLocation(15, 15),
// (11,15): warning CS0618: 'Test.F3' is obsolete: 'F3 is obsolete'
// [SomeAttr(F3)]
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F3").WithArguments("Test.F3", "F3 is obsolete"),
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F3").WithArguments("Test.F3", "F3 is obsolete").WithLocation(11, 15),
// (18,15): error CS0619: 'Test.F4' is obsolete: 'blah'
// [Obsolete(F4, true)]
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F4").WithArguments("Test.F4", "blah"),
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F4").WithArguments("Test.F4", "blah").WithLocation(18, 15),
// (21,15): error CS0120: An object reference is required for the non-static field, method, or property 'Test.F5'
// [Obsolete(F5)]
Diagnostic(ErrorCode.ERR_ObjectRequired, "F5").WithArguments("Test.F5"),
Diagnostic(ErrorCode.ERR_ObjectRequired, "F5").WithArguments("Test.F5").WithLocation(21, 15),
// (24,15): error CS0120: An object reference is required for the non-static field, method, or property 'Test.P1'
// [Obsolete(P1, true)]
Diagnostic(ErrorCode.ERR_ObjectRequired, "P1").WithArguments("Test.P1"),
Diagnostic(ErrorCode.ERR_ObjectRequired, "P1").WithArguments("Test.P1").WithLocation(24, 15),
// (28,15): warning CS0612: 'Test.P2' is obsolete
// [SomeAttr(P2, true)]
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "P2").WithArguments("Test.P2"),
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "P2").WithArguments("Test.P2").WithLocation(28, 15),
// (28,15): error CS0120: An object reference is required for the non-static field, method, or property 'Test.P2'
// [SomeAttr(P2, true)]
Diagnostic(ErrorCode.ERR_ObjectRequired, "P2").WithArguments("Test.P2"),
Diagnostic(ErrorCode.ERR_ObjectRequired, "P2").WithArguments("Test.P2").WithLocation(28, 15),
// (28,6): error CS1729: 'SomeAttr' does not contain a constructor that takes 2 arguments
// [SomeAttr(P2, true)]
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "SomeAttr(P2, true)").WithArguments("SomeAttr", "2").WithLocation(28, 6),
// (31,15): error CS1503: Argument 1: cannot convert from 'method group' to 'string'
// [Obsolete(Method1)]
Diagnostic(ErrorCode.ERR_BadArgType, "Method1").WithArguments("1", "method group", "string"),
Diagnostic(ErrorCode.ERR_BadArgType, "Method1").WithArguments("1", "method group", "string").WithLocation(31, 15),
// (35,16): warning CS0612: 'Test.Method2()' is obsolete
// [SomeAttr1(Method2)]
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "Method2").WithArguments("Test.Method2()"),
// (35,6): error CS0181: Attribute constructor parameter 'x' has type 'System.Action', which is not a valid attribute parameter type
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "Method2").WithArguments("Test.Method2()").WithLocation(35, 16),
// (35,6): error CS0181: Attribute constructor parameter 'x' has type 'Action', which is not a valid attribute parameter type
// [SomeAttr1(Method2)]
Diagnostic(ErrorCode.ERR_BadAttributeParamType, "SomeAttr1").WithArguments("x", "System.Action"),
Diagnostic(ErrorCode.ERR_BadAttributeParamType, "SomeAttr1").WithArguments("x", "System.Action").WithLocation(35, 6),
// (43,15): error CS0619: 'Test.F7' is obsolete: 'F7 is obsolete'
// [Obsolete(F7, true)]
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F7").WithArguments("Test.F7", "F7 is obsolete"),
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F7").WithArguments("Test.F7", "F7 is obsolete").WithLocation(43, 15),
// (44,15): warning CS0618: 'Test.F6' is obsolete: 'F6 is obsolete'
// [SomeAttr(F6)]
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F6").WithArguments("Test.F6", "F6 is obsolete"),
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F6").WithArguments("Test.F6", "F6 is obsolete").WithLocation(44, 15),
// (45,15): error CS0619: 'Test.F7' is obsolete: 'F7 is obsolete'
// [SomeAttr(F7)]
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F7").WithArguments("Test.F7", "F7 is obsolete"),
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F7").WithArguments("Test.F7", "F7 is obsolete").WithLocation(45, 15),
// (38,15): warning CS0618: 'Test.F6' is obsolete: 'F6 is obsolete'
// [Obsolete(F6)]
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F6").WithArguments("Test.F6", "F6 is obsolete"),
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F6").WithArguments("Test.F6", "F6 is obsolete").WithLocation(38, 15),
// (39,15): warning CS0618: 'Test.F6' is obsolete: 'F6 is obsolete'
// [SomeAttr(F6)]
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F6").WithArguments("Test.F6", "F6 is obsolete"),
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "F6").WithArguments("Test.F6", "F6 is obsolete").WithLocation(39, 15),
// (40,15): error CS0619: 'Test.F7' is obsolete: 'F7 is obsolete'
// [SomeAttr(F7)]
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F7").WithArguments("Test.F7", "F7 is obsolete"));
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "F7").WithArguments("Test.F7", "F7 is obsolete").WithLocation(40, 15));
}
[WorkItem(546064, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546064")]
......
......@@ -54,6 +54,77 @@ static void Test2(object x, int y)
VerifyModelForOutVar(model, x1Decl, x1Ref);
}
[Fact]
[WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
[WorkItem(16348, "https://github.com/dotnet/roslyn/issues/16348")]
public void DiagnosticsDifferenceBetweenLanguageVersions_01()
{
var text = @"
public class Cls
{
public static void Test1()
{
Test(out int x1);
}
public static void Test2()
{
var x = new Cls(out int x2);
}
}";
var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.CSharp6));
compilation.VerifyDiagnostics(
// (6,22): error CS8059: Feature 'out variable declaration' is not available in C# 6. Please use language version 7 or greater.
// Test(out int x1);
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion6, "x1").WithArguments("out variable declaration", "7").WithLocation(6, 22),
// (11,33): error CS8059: Feature 'out variable declaration' is not available in C# 6. Please use language version 7 or greater.
// var x = new Cls(out int x2);
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion6, "x2").WithArguments("out variable declaration", "7").WithLocation(11, 33),
// (6,9): error CS0103: The name 'Test' does not exist in the current context
// Test(out int x1);
Diagnostic(ErrorCode.ERR_NameNotInContext, "Test").WithArguments("Test").WithLocation(6, 9),
// (11,21): error CS1729: 'Cls' does not contain a constructor that takes 1 arguments
// var x = new Cls(out int x2);
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Cls").WithArguments("Cls", "1").WithLocation(11, 21),
// (11,29): error CS0165: Use of unassigned local variable 'x2'
// var x = new Cls(out int x2);
Diagnostic(ErrorCode.ERR_UseDefViolation, "int x2").WithArguments("x2").WithLocation(11, 29)
);
var tree = compilation.SyntaxTrees.Single();
var model = compilation.GetSemanticModel(tree);
var x1Decl = GetOutVarDeclaration(tree, "x1");
VerifyModelForOutVar(model, x1Decl);
var x2Decl = GetOutVarDeclaration(tree, "x2");
//VerifyModelForOutVar(model, x2Decl); Probably fails due to https://github.com/dotnet/roslyn/issues/16348
VerifyModelForOutVarWithoutDataFlow(model, x2Decl);
compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular);
compilation.VerifyDiagnostics(
// (6,9): error CS0103: The name 'Test' does not exist in the current context
// Test(out int x1);
Diagnostic(ErrorCode.ERR_NameNotInContext, "Test").WithArguments("Test").WithLocation(6, 9),
// (11,21): error CS1729: 'Cls' does not contain a constructor that takes 1 arguments
// var x = new Cls(out int x2);
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Cls").WithArguments("Cls", "1").WithLocation(11, 21),
// (11,29): error CS0165: Use of unassigned local variable 'x2'
// var x = new Cls(out int x2);
Diagnostic(ErrorCode.ERR_UseDefViolation, "int x2").WithArguments("x2").WithLocation(11, 29)
);
tree = compilation.SyntaxTrees.Single();
model = compilation.GetSemanticModel(tree);
x1Decl = GetOutVarDeclaration(tree, "x1");
VerifyModelForOutVar(model, x1Decl);
x2Decl = GetOutVarDeclaration(tree, "x2");
//VerifyModelForOutVar(model, x2Decl); Probably fails due to https://github.com/dotnet/roslyn/issues/16348
VerifyModelForOutVarWithoutDataFlow(model, x2Decl);
}
[Fact]
[CompilerTrait(CompilerFeature.Tuples)]
[WorkItem(13148, "https://github.com/dotnet/roslyn/issues/13148")]
......@@ -2296,9 +2367,15 @@ class Test : System.Attribute
// (4,19): error CS0103: The name 'x3' does not exist in the current context
// [Test(out var x3)]
Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(4, 19),
// (4,6): error CS1729: 'Test' does not contain a constructor that takes 2 arguments
// [Test(out var x3)]
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Test(out var x3)").WithArguments("Test", "2").WithLocation(4, 6),
// (5,19): error CS0103: The name 'x4' does not exist in the current context
// [Test(out int x4)]
Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(5, 19),
// (5,6): error CS1729: 'Test' does not contain a constructor that takes 2 arguments
// [Test(out int x4)]
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Test(out int x4)").WithArguments("Test", "2").WithLocation(5, 6),
// (6,18): error CS0103: The name 'var' does not exist in the current context
// [Test(p: out var x5)]
Diagnostic(ErrorCode.ERR_NameNotInContext, "var").WithArguments("var").WithLocation(6, 18),
......@@ -2308,12 +2385,18 @@ class Test : System.Attribute
// (6,22): error CS0103: The name 'x5' does not exist in the current context
// [Test(p: out var x5)]
Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(6, 22),
// (6,6): error CS1729: 'Test' does not contain a constructor that takes 3 arguments
// [Test(p: out var x5)]
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Test(p: out var x5)").WithArguments("Test", "3").WithLocation(6, 6),
// (7,18): error CS1738: Named argument specifications must appear after all fixed arguments have been specified
// [Test(p: out int x6)]
Diagnostic(ErrorCode.ERR_NamedArgumentSpecificationBeforeFixedArgument, "int").WithLocation(7, 18),
// (7,22): error CS0103: The name 'x6' does not exist in the current context
// [Test(p: out int x6)]
Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(7, 22)
Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(7, 22),
// (7,6): error CS1729: 'Test' does not contain a constructor that takes 3 arguments
// [Test(p: out int x6)]
Diagnostic(ErrorCode.ERR_BadCtorArgCount, "Test(p: out int x6)").WithArguments("Test", "3").WithLocation(7, 6)
);
var tree = compilation.SyntaxTrees.Single();
......@@ -177,7 +177,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return New SynthesizedAttributeData(constructorSymbol, arguments, namedStringArguments)
End Function
Private Function ReturnNothingOrThrowIfAttributeNonOptional(constructor As WellKnownMember, Optional isOptionalUse As Boolean = False) As SynthesizedAttributeData
Private Shared Function ReturnNothingOrThrowIfAttributeNonOptional(constructor As WellKnownMember, Optional isOptionalUse As Boolean = False) As SynthesizedAttributeData
If isOptionalUse OrElse WellKnownMembers.IsSynthesizedAttributeOptional(constructor) Then
Return Nothing
Else
......
......@@ -2978,7 +2978,7 @@ void M()
withScriptOption: true);
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
public async Task TestOutVariableDeclaration_ImplicitlyTyped_CSharp6()
{
......@@ -3006,7 +3006,7 @@ void M()
withScriptOption: true);
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
public async Task TestOutVariableDeclaration_ImplicitlyTyped_NamedArgument_CSharp6()
{
......@@ -3034,7 +3034,7 @@ void M()
withScriptOption: true);
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
public async Task TestOutVariableDeclaration_ExplicitlyTyped_CSharp6()
{
......@@ -3062,7 +3062,7 @@ void M()
withScriptOption: true);
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
public async Task TestOutVariableDeclaration_ExplicitlyTyped_NamedArgument_CSharp6()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册