提交 27293ac4 编写于 作者: E Evan Hauck

Merge pull request #11219 from khyperia/fix9945

Fix #9945: Check if dynamic property is set-only when dotting off it
......@@ -4510,6 +4510,12 @@ private bool IsUsingAliasInScope(string name)
if ((object)leftType != null && leftType.IsDynamic())
{
// There are some sources of a `dynamic` typed value that can be known before runtime
// to be invalid. For example, accessing a set-only property whose type is dynamic:
// dynamic Foo { set; }
// If Foo itself is a dynamic thing (e.g. in `x.Foo.Bar`, `x` is dynamic, and we're
// currently checking Bar), then CheckValue will do nothing.
boundLeft = CheckValue(boundLeft, BindValueKind.RValue, diagnostics);
return BindDynamicMemberAccess(node, boundLeft, right, invoked, indexed, diagnostics);
}
......
......@@ -3434,6 +3434,33 @@ static void Main()
CompileAndVerify(compilation2, expectedOutput: @"4");
}
[Fact, WorkItem(9945, "https://github.com/dotnet/roslyn/issues/9945")]
public void DynamicGetOnlyProperty()
{
string source = @"
class Program
{
static void Main()
{
I i = null;
System.Type t = i.d.GetType();
}
interface I
{
dynamic d { set; }
}
}
";
var compilation = CreateCompilationWithMscorlib(source, new[] { CSharpRef, SystemCoreRef }, options: TestOptions.DebugDll);
// crash happens during emit if not detected, so VerifyDiagnostics (no Emit) doesn't catch the crash.
compilation.VerifyEmitDiagnostics(
// (7,25): error CS0154: The property or indexer 'Program.I.d' cannot be used in this context because it lacks the get accessor
// System.Type t = i.d.GetType();
Diagnostic(ErrorCode.ERR_PropertyLacksGet, "i.d").WithArguments("Program.I.d").WithLocation(7, 25)
);
}
[ClrOnlyFact(ClrOnlyReason.Ilasm)]
public void IncorrectArrayLength()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册