Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't hoist ref locals #43463

Merged
merged 7 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static bool HoistInDebugBuild(Symbol symbol)
ParameterSymbol parameter =>
// in Debug build hoist all parameters that can be hoisted:
!parameter.Type.IsRestrictedType(),
LocalSymbol { IsConst: false, IsPinned: false } local =>
LocalSymbol { IsConst: false, IsPinned: false, IsRef: false } local =>
// hoist all user-defined locals and long-lived temps that can be hoisted:
local.SynthesizedKind.MustSurviveStateMachineSuspension() &&
!local.Type.IsRestrictedType(),
Expand Down
55 changes: 55 additions & 0 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5606,5 +5606,60 @@ static async Task Main()
var comp = CSharpTestBase.CreateCompilation(source, options: TestOptions.ReleaseExe);
CompileAndVerify(comp, expectedOutput: "StructAwaitable");
}

[Fact, WorkItem(40251, "https://github.com/dotnet/roslyn/issues/40251")]
public void AssignRefAfterAwait()
{
const string source = @"
using System.Threading.Tasks;

class IntCode
{
public async Task Step(int i, Task t)
{
await t;
ReadMemory() = i switch
{
_ => throw null
};
}

private ref long ReadMemory() => throw null;
}
";

var comp = CreateCompilation(source, options: TestOptions.DebugDll);
comp.VerifyEmitDiagnostics();
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comp.VerifyEmitDiagnostics(); [](start = 12, length = 29)

Consider running the scenario and observing expected behavior at runtime. #Closed

comp = CreateCompilation(source, options: TestOptions.ReleaseDll);
comp.VerifyEmitDiagnostics();
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comp.VerifyEmitDiagnostics(); [](start = 12, length = 29)

Here as well. #Closed

}

[Fact, WorkItem(40251, "https://github.com/dotnet/roslyn/issues/40251")]
public void AssignRefWithAwait()
{
const string source = @"
using System.Threading.Tasks;

class IntCode
{
public async Task Step(Task<int> t)
{
ReadMemory() = await t;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test where we have a ref l-value and use a compound operator. Essentially

ReadMemory() += await t;

}

private ref long ReadMemory() => throw null;
}
";
var expected = new[]
{
// (8,9): error CS8178: 'await' cannot be used in an expression containing a call to 'IntCode.ReadMemory()' because it returns by reference
// ReadMemory() = await t;
Diagnostic(ErrorCode.ERR_RefReturningCallAndAwait, "ReadMemory()").WithArguments("IntCode.ReadMemory()").WithLocation(8, 9)
};
var comp = CreateCompilation(source, options: TestOptions.DebugDll);
comp.VerifyEmitDiagnostics(expected);
comp = CreateCompilation(source, options: TestOptions.ReleaseDll);
comp.VerifyEmitDiagnostics(expected);
}
}
}