Skip to content

Commit

Permalink
Merge pull request #11219 from khyperia/fix9945
Browse files Browse the repository at this point in the history
Fix #9945: Check if dynamic property is set-only when dotting off it
  • Loading branch information
khyperia committed May 12, 2016
2 parents 9335649 + 1885b7d commit 27293ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
27 changes: 27 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/DynamicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 27293ac

Please sign in to comment.