-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 check safe-to-escape of receiver arguments to readonly members #35597
Don't check safe-to-escape of receiver arguments to readonly members #35597
Conversation
src/Compilers/CSharp/Test/Semantic/Semantics/RefEscapingTests.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/RefEscapingTests.cs
Outdated
Show resolved
Hide resolved
These tests are a bit messed up--bad names and copy/pastes. I'll fix them up. |
var isReadOnlyInvocation = symbol switch | ||
{ | ||
MethodSymbol m => m.IsEffectivelyReadOnly, | ||
// TODO: val escape checks should be skipped for property accesses when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the case you were telling me about, where we can't easily determine which accessor we are using?
So an indexer that takes a Span<T>
(for multi-dimensional indexing) won't work with this change and will need to be handled in the future, correct (just talking about the getter, since the setter needs to be mutable in this case)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah if it looks like this[Span<T> span] { readonly get; set; }
we will be pessimistic and say the span could escape illegally even though we should be able to tell that only the getter is called.
comp.VerifyDiagnostics( | ||
// (11,15): error CS8352: Cannot use local 'x' in this context because it may expose referenced variables outside of their declaration scope | ||
// b.P = x; | ||
Diagnostic(ErrorCode.ERR_EscapeLocal, "x").WithArguments("x").WithLocation(11, 15)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of this test is more to explicitly illustrate the existing behavior. Strictly speaking we might be able to exclude such properties from escape analysis but it doesn't seem to present much value.
Is there a proposed update to a specification? Or is this something already specified? Is there something over on |
This is functionality that already works for methods of |
Where would I find a spec for the behavior implemented here? Or the related functionality for |
I would expect it to be in either https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/span-safety.md or https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/readonly-ref.md, if anywhere. However, I did not immediately see a section detailing a |
I would prefer a specification for the proposed language change (even if that means also writing spec language for stuff already done, such as readonly structs) to be reviewed before I would feel comfortable reviewing its implementation. |
I agree with @gafter. This isn't bug-fix level, we need a spec proposal first. |
It seems like the currently shipped behavior on escape analysis for arguments to readonly struct methods isn't specified in the span-safety or readonly-ref docs. It seems better to put in span-safety. How about I propose a change to the span safety spec as the next step? Please let me know if there's anything I should look at to onboard to that or if there's some other way this should get done. |
After some closer study of the span safety spec I've identified the following relevant clauses in the Language Constraints section.
It seems like these constraints are what's checked by CheckInvocationArgMixing. The spec language excludes I'm going to update the implementation to quote the spec and ask you to take another look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talked with @RikkiGibson offline. We think there might be a forward compat scenario where you can call one of these members from a C# 7 assembly in VS 2019, but not in 2017, so he's going to add more tests to verify behavior and adjust as necessary. |
Done review pass (commit 5) |
We decided that this is an uncommon enough scenario that we're comfortable asking people to simply update their compiler version to make it work. |
Fixes #35146 but we'll need to create a new issue to track fixing escape analysis on properties where one accessor is readonly and the other is not.