-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Consider RefSafetyRules with UnscopedRef #75930
base: main
Are you sure you want to change the base?
Conversation
The issue that this PR is claiming to fix sounds as though the problem is related to using a specific language version, but the code changes and the tests look like the language version makes no difference on its own. If that is the case, consider clarifying the issue, clearly describing all the factors involved. And, perhaps, adjusting its title. |
I've updated the issue's title and description. |
"""; | ||
CreateCompilation([source, UnscopedRefAttributeDefinition], | ||
parseOptions: TestOptions.Regular10).VerifyDiagnostics( | ||
// (6,47): error CS8170: Struct members cannot return 'this' or other instance members by reference |
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.
error CS8170: Struct members cannot return 'this' or other instance members by reference
This looks like a breaking change, although it also looks like users of this library would ignore the [UnscopedRef]
attribute on the method since the assembly is not marked with [RefSafetyRules]
, so perhaps the breaking change when compiling this library is appropriate.
Consider adding a note to the Breaking Change document.
Is this the only breaking change in the PR? (From the tests below, it looks like in other cases we're reporting other errors where [UnscopedRef]
is used (and ignored) with earlier ref safety rules.)
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.
Should the compiler report a diagnostic when [UnscopedRef]
is applied to a source method that is compiled with earlier ref safety rules? Otherwise, the library author may not realize the annotation will be ignored by callers. For instance, would the following still compile with earlier ref safety rules without warnings or errors?
struct S
{
int F;
[UnscopedRef] public ref int Ref() => ref Unsafe.AsRef(in F);
}
static class C
{
static ref int M()
{
S s = default;
return ref s.Ref();
}
}
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.
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.
It feels like an oversight that we did not report an error when UnscopedRef was used in presence of the old ref safety rules.
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.
For instance, would the following still compile with earlier ref safety rules without warnings or errors?
No, it wouldn't compile. There will be errors because you are still using this
by reference inside S.Ref
which is disallowed there.
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.
But I've changed the implementation to report an error for [UnscopedRef]
on a member in the old rules similarly as we report it for [UnscopedRef]
on a parameter.
Fixes #75828