提交 c9ff8d3e 编写于 作者: C Charles Stoner

Report error for await in static script variable initializer

上级 ea420004
......@@ -125,7 +125,14 @@ private bool ReportBadAwaitWithoutAsync(CSharpSyntaxNode node, DiagnosticBag dia
case SymbolKind.Field:
if (containingMemberOrLambda.ContainingType.IsScriptClass)
{
return false;
if (((FieldSymbol)containingMemberOrLambda).IsStatic)
{
info = new CSDiagnosticInfo(ErrorCode.ERR_BadAwaitInStaticVariableInitializer);
}
else
{
return false;
}
}
break;
case SymbolKind.Method:
......
......@@ -1051,6 +1051,15 @@ internal class CSharpResources {
}
}
/// <summary>
/// Looks up a localized string similar to The &apos;await&apos; operator cannot be used in a static script variable initializer..
/// </summary>
internal static string ERR_BadAwaitInStaticVariableInitializer {
get {
return ResourceManager.GetString("ERR_BadAwaitInStaticVariableInitializer", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The &apos;await&apos; operator can only be used when contained within a method or lambda expression marked with the &apos;async&apos; modifier.
/// </summary>
......
......@@ -3530,6 +3530,9 @@ Give the compiler some way to differentiate the methods. For example, you can gi
<data name="ERR_BadAwaitInLock" xml:space="preserve">
<value>Cannot await in the body of a lock statement</value>
</data>
<data name="ERR_BadAwaitInStaticVariableInitializer" xml:space="preserve">
<value>The 'await' operator cannot be used in a static script variable initializer.</value>
</data>
<data name="ERR_AwaitInUnsafeContext" xml:space="preserve">
<value>Cannot await in an unsafe context</value>
</data>
......
......@@ -1315,5 +1315,6 @@ internal enum ErrorCode
ERR_LoadDirectiveOnlyAllowedInScripts = 8097,
ERR_PPLoadFollowsToken = 8098,
ERR_SourceFileReferencesNotSupported = 8099,
ERR_BadAwaitInStaticVariableInitializer = 8100,
}
}
......@@ -3672,20 +3672,38 @@ public void AwaitInInteractiveDeclaration()
/// <summary>
/// await should be disallowed in static field initializer
/// since the static initialization of the class should
/// complete before other members are used.
/// since the static initialization of the class must be
/// handled synchronously in the .cctor.
/// </summary>
[Fact(Skip = "Not handled")]
public void AwaitInStaticInitializer()
[WorkItem(5787)]
[Fact]
public void AwaitInScriptStaticInitializer()
{
var source =
@"static int x = 1 +
await System.Threading.Tasks.Task.FromResult(1);
int y = x +
await System.Threading.Tasks.Task.FromResult(2);";
var compilation = CreateCompilationWithMscorlib45(source, parseOptions: TestOptions.Script, options: TestOptions.DebugExe);
compilation.VerifyDiagnostics(
// (2,5): error CS8100: The 'await' operator cannot be used in a static script variable initializer.
// await System.Threading.Tasks.Task.FromResult(1);
Diagnostic(ErrorCode.ERR_BadAwaitInStaticVariableInitializer, "await System.Threading.Tasks.Task.FromResult(1)").WithLocation(2, 5));
}
[WorkItem(5787)]
[Fact]
public void AwaitInInteractiveStaticInitializer()
{
var references = new[] { MscorlibRef_v4_0_30316_17626, SystemCoreRef };
var source =
@"static int x = await System.Threading.Tasks.Task.FromResult(1);";
@"static int x = await System.Threading.Tasks.Task.FromResult(1);
int y = await System.Threading.Tasks.Task.FromResult(2);";
var compilation = CSharpCompilation.CreateSubmission("s0.dll", SyntaxFactory.ParseSyntaxTree(source, options: TestOptions.Interactive), references);
compilation.VerifyDiagnostics(
// (1,16): error CS1992: The 'await' operator can only be used when contained within a method or lambda expression marked with the 'async' modifier
// (1,16): error CS8100: The 'await' operator cannot be used in a static script variable initializer.
// static int x = await System.Threading.Tasks.Task.FromResult(1);
Diagnostic(ErrorCode.ERR_BadAwaitWithoutAsync, "await System.Threading.Tasks.Task.FromResult(1)").WithLocation(1, 16));
Diagnostic(ErrorCode.ERR_BadAwaitInStaticVariableInitializer, "await System.Threading.Tasks.Task.FromResult(1)").WithLocation(1, 16));
}
[Fact, WorkItem(4839, "https://github.com/dotnet/roslyn/issues/4839")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册