提交 9d2a2cfe 编写于 作者: Y Yair Halberstadt 提交者: Charles Stoner

Fix Host Object Access in Interactive (#39584)

上级 964de174
......@@ -1537,7 +1537,7 @@ private BoundExpression SynthesizeMethodGroupReceiver(CSharpSyntaxNode syntax, A
}
else
{
return TryBindInteractiveReceiver(syntax, ContainingMember(), currentType, declaringType);
return TryBindInteractiveReceiver(syntax, declaringType);
}
}
......@@ -1795,7 +1795,7 @@ private BoundExpression SynthesizeReceiver(SyntaxNode node, Symbol member, Diagn
}
else
{
return TryBindInteractiveReceiver(node, ContainingMember(), currentType, declaringType);
return TryBindInteractiveReceiver(node, declaringType);
}
}
......@@ -1810,9 +1810,11 @@ internal Symbol ContainingMember()
return containingMember;
}
private BoundExpression TryBindInteractiveReceiver(SyntaxNode syntax, Symbol currentMember, NamedTypeSymbol currentType, NamedTypeSymbol memberDeclaringType)
private BoundExpression TryBindInteractiveReceiver(SyntaxNode syntax, NamedTypeSymbol memberDeclaringType)
{
if (currentType.TypeKind == TypeKind.Submission && currentMember.RequiresInstanceReceiver())
if (this.ContainingType.TypeKind == TypeKind.Submission
// check we have access to `this`
&& isInstanceContext())
{
if (memberDeclaringType.TypeKind == TypeKind.Submission)
{
......@@ -1830,6 +1832,24 @@ private BoundExpression TryBindInteractiveReceiver(SyntaxNode syntax, Symbol cur
}
return null;
bool isInstanceContext()
{
var containingMember = this.ContainingMemberOrLambda;
do
{
if (containingMember.IsStatic)
{
return false;
}
if (containingMember.Kind == SymbolKind.NamedType)
{
break;
}
containingMember = containingMember.ContainingSymbol;
} while ((object)containingMember != null);
return true;
}
}
public BoundExpression BindNamespaceOrTypeOrExpression(ExpressionSyntax node, DiagnosticBag diagnostics)
......
......@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
......@@ -1148,6 +1147,14 @@ public void AwaitChain2()
Assert.Equal(3, state.ReturnValue);
}
[Fact, WorkItem(39548, "https://github.com/dotnet/roslyn/issues/39548")]
public async Task PatternVariableDeclaration()
{
var state = await CSharpScript.RunAsync("var x = (false, 4);");
state = await state.ContinueWithAsync("x is (false, var y)");
Assert.Equal(true, state.ReturnValue);
}
#endregion
#region References
......@@ -1776,6 +1783,95 @@ public void HostObjectAssemblyReference3()
}
}
public class E
{
public bool TryGetValue(out object obj)
{
obj = new object();
return true;
}
}
[Fact]
[WorkItem(39565, "https://github.com/dotnet/roslyn/issues/39565")]
public async Task MethodCallWithImplicitReceiverAndOutVar()
{
var code = @"
if(TryGetValue(out var result)){
_ = result;
}
return true;
";
var result = await CSharpScript.EvaluateAsync<bool>(code, globalsType: typeof(E), globals: new E());
Assert.True(result);
}
public class F
{
public bool Value = true;
}
[Fact]
public void StaticMethodCannotAccessGlobalInstance()
{
var code = @"
static bool M()
{
return Value;
}
return M();
";
var script = CSharpScript.Create<bool>(code, globalsType: typeof(F));
ScriptingTestHelpers.AssertCompilationError(() => script.RunAsync(new F()).Wait(),
// (4,9): error CS0120: An object reference is required for the non-static field, method, or property 'InteractiveSessionTests.F.Value'
// return Value;
Diagnostic(ErrorCode.ERR_ObjectRequired, "Value").WithArguments("Microsoft.CodeAnalysis.CSharp.Scripting.UnitTests.InteractiveSessionTests.F.Value").WithLocation(4, 9));
}
[Fact]
[WorkItem(39581, "https://github.com/dotnet/roslyn/issues/39581")]
public void StaticLocalFunctionCannotAccessGlobalInstance()
{
var code = @"
bool M()
{
return Inner();
static bool Inner()
{
return Value;
}
}
return M();
";
var script = CSharpScript.Create<bool>(code, globalsType: typeof(F));
ScriptingTestHelpers.AssertCompilationError(() => script.RunAsync(new F()).Wait(),
// (7,10): error CS0120: An object reference is required for the non-static field, method, or property 'InteractiveSessionTests.F.Value'
// return Value;
Diagnostic(ErrorCode.ERR_ObjectRequired, "Value").WithArguments("Microsoft.CodeAnalysis.CSharp.Scripting.UnitTests.InteractiveSessionTests.F.Value").WithLocation(7, 10));
}
[Fact]
public async Task LocalFunctionCanAccessGlobalInstance()
{
var code = @"
bool M()
{
return Inner();
bool Inner()
{
return Value;
}
}
return M();
";
var result = await CSharpScript.EvaluateAsync<bool>(code, globalsType: typeof(F), globals: new F());
Assert.True(result);
}
#endregion
#region Exceptions
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册