提交 7d2c408a 编写于 作者: T Ty Overby 提交者: GitHub

harden CompileMethod (#14680)

* harden CompileMethod

* add test

* reword comment
上级 902766d1
......@@ -1048,7 +1048,7 @@ public override object VisitField(FieldSymbol symbol, TypeCompilationState argum
Debug.Assert(!instrumentForDynamicAnalysis);
StateMachineTypeSymbol initializerStateMachineTypeOpt;
processedInitializers.LoweredInitializers = (BoundStatementList)LowerBodyOrInitializer(
var lowered = LowerBodyOrInitializer(
methodSymbol,
methodOrdinal,
analyzedInitializers,
......@@ -1065,10 +1065,8 @@ public override object VisitField(FieldSymbol symbol, TypeCompilationState argum
// initializers can't produce state machines
Debug.Assert((object)initializerStateMachineTypeOpt == null);
Debug.Assert(processedInitializers.LoweredInitializers.Kind == BoundKind.StatementList);
Debug.Assert(!hasErrors);
hasErrors = processedInitializers.LoweredInitializers.HasAnyErrors || diagsForCurrentMethod.HasAnyErrors();
hasErrors = lowered.HasAnyErrors || diagsForCurrentMethod.HasAnyErrors();
SetGlobalErrorIfTrue(hasErrors);
if (hasErrors)
......@@ -1076,6 +1074,10 @@ public override object VisitField(FieldSymbol symbol, TypeCompilationState argum
_diagnostics.AddRange(diagsForCurrentMethod);
return;
}
// Only do the cast if we haven't returned with some error diagnostics.
// Otherwise, `lowered` might have been a BoundBadStatement.
processedInitializers.LoweredInitializers = (BoundStatementList)lowered;
}
// initializers for global code have already been included in the body
......
......@@ -589,5 +589,21 @@ .maxstack 4
}
");
}
[WorkItem(217748, "https://devdiv.visualstudio.com/DevDiv/_workitems?_a=edit&id=21774")]
[Fact]
public void BadExpressionConstructor()
{
string source =
@"class C
{
static dynamic F() => 0;
dynamic d = F() * 2;
}";
CreateCompilationWithMscorlibAndSystemCore(source).VerifyEmitDiagnostics(
// (4,17): error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
// dynamic d = F() * 2;
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "F()").WithArguments("Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo", "Create").WithLocation(4, 17));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册