提交 28bbcbfc 编写于 作者: A AlekseyTs

Report ERR_MethodArgCantBeRefAny for “out var” declarations without...

Report ERR_MethodArgCantBeRefAny for “out var” declarations without initializers. This is a port of https://roslyn.codeplex.com/SourceControl/changeset/4b562c0061233f3e3a76de510ecd68b2d37fa1b1. (changeset 1250556)
上级 bfe67353
......@@ -2946,10 +2946,26 @@ private void BindArgumentsAndNames(BracketedArgumentListSyntax argumentListOpt,
}
else if (argument.Kind == BoundKind.UninitializedVarDeclarationExpression)
{
arguments[arg] = ((UninitializedVarDeclarationExpression)argument).SetInferredType(GetCorrespondingParameterType(ref result, parameters, arg), success: true);
TypeSymbol parameterType = GetCorrespondingParameterType(ref result, parameters, arg);
bool hasErrors = false;
if (this.ContainingMemberOrLambda.Kind == SymbolKind.Method
&& ((MethodSymbol)this.ContainingMemberOrLambda).IsAsync
&& parameterType.IsRestrictedType())
{
Error(diagnostics, ErrorCode.ERR_BadSpecialByRefLocal,
argument.Syntax.CSharpKind() == SyntaxKind.DeclarationExpression ?
((DeclarationExpressionSyntax)argument.Syntax).Type :
argument.Syntax,
parameterType);
hasErrors = true;
}
arguments[arg] = ((UninitializedVarDeclarationExpression)argument).SetInferredType(parameterType, success: !hasErrors);
}
}
}
}
private static TypeSymbol GetCorrespondingParameterType(ref MemberAnalysisResult result, ImmutableArray<ParameterSymbol> parameters, int arg)
{
......
......@@ -3994,5 +3994,48 @@ public static void Main()
);
}
[Fact]
public void RestrictedType()
{
var source = @"
using System.Threading.Tasks;
using System;
class Test
{
static void Main() {}
async Task M1()
{
M1(out TypedReference x);
M1(out var y);
await Task.Factory.StartNew(() => { });
}
void M1(out TypedReference tr)
{
tr = default(TypedReference);
}
Task M2()
{
M1(out TypedReference x);
M1(out var y);
return null;
}
}";
CreateCompilationWithMscorlib45(source, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental)).VerifyDiagnostics(
// (16,13): error CS1601: Cannot make reference to variable of type 'System.TypedReference'
// void M1(out TypedReference tr)
Diagnostic(ErrorCode.ERR_MethodArgCantBeRefAny, "out TypedReference tr").WithArguments("System.TypedReference").WithLocation(16, 13),
// (11,16): error CS4012: Parameters or locals of type 'System.TypedReference' cannot be declared in async methods or lambda expressions.
// M1(out TypedReference x);
Diagnostic(ErrorCode.ERR_BadSpecialByRefLocal, "TypedReference").WithArguments("System.TypedReference").WithLocation(11, 16),
// (12,16): error CS4012: Parameters or locals of type 'System.TypedReference' cannot be declared in async methods or lambda expressions.
// M1(out var y);
Diagnostic(ErrorCode.ERR_BadSpecialByRefLocal, "var").WithArguments("System.TypedReference").WithLocation(12, 16)
);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册