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

Honor annotations for user defined operators and conversions: #34556

Closed

Conversation

chsienki
Copy link
Contributor

  • Add a VisitOperandsHonoringAnnotations that will split the state if needed based on annotations
  • Call it from user defined conversions and binary operators
  • Add tests

Fixes #32671

- Add a VisitOperandsHonoringAnnotations that will split the state if needed based on annotations
- Call it from user defined conversions and binary operators
- Add tests
{
c2.ToString();
}
}
Copy link
Member

Choose a reason for hiding this comment

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

} [](start = 4, length = 1)

For completeness, consider calling c.ToString(); c2.ToString(); in each branch. (At that point, it would be better to have two methods, one testing == and the other testing !=, so there's no side-effects from earlier calls to ToString().)

{
bool saveDisableDiagnostics = _disableDiagnostics;
_disableDiagnostics = true;
VisitArgumentsEvaluateHonoringAnnotations(operands, method.ParameterRefKinds, annotations);
Copy link
Member

@jcouv jcouv Mar 28, 2019

Choose a reason for hiding this comment

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

VisitArgumentsEvaluateHonoringAnnotations [](start = 16, length = 41)

looking at the other place where we use this method, we do some gymnastics to save/restore the state to avoid side-effects from double-visit. It feels likely that a similar scenario could affect user-defined operators.

A scenario that may hit that (should have no warnings on s.ToString():

string? s= "";
_ = s.ToString() + (s= null); // with a custom `+`

Update: On second thought, that's probably not a good scenario (where saving/restoring state matters). VisitArgumentsEvaluateHonoringAnnotations doesn't produce more diagnostics, it only updates the state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe the state saving in the other case is because we do some other visits which can affect it, not due to VisitArgumentsEvaluateHonoringAnnotations


In reply to: 270161114 [](ancestors = 270161114)

node.ConversionKind == ConversionKind.ImplicitUserDefined) &&
!(conversion.Method is null))
{
VisitOperandsHonoringAnnotations(conversion.Method, ImmutableArray.Create(operand));
Copy link
Member

Choose a reason for hiding this comment

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

VisitOperandsHonoringAnnotations [](start = 16, length = 32)

Was there a reason this could not be pushed into ApplyConversion?
Also, there are tuple scenarios worth testing: A converts to B with user-defined conversion, then (B, B) t = (a, a); and variants.

I'm wondering whether we should ignore annotations on such user-defined conversions though. It's not obvious what is the benefit...

Copy link
Member

@jcouv jcouv Mar 28, 2019

Choose a reason for hiding this comment

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

There may also be a problem with not saving/restoring the state here, given that we visit the operand twice. Maybe a scenario like (x.String() + (x = null))-- with a user-defined -.


In reply to: 270163123 [](ancestors = 270163123)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally prototyped it in ApplyConversion, but was trying to make the change smaller: we'd need to handle a potentially split state in other places if we have it in apply conversion.

I'm not sure how the tuple conversions would be used? you could obviously annotate them, but are there scenarios where they would actually have any effect?


In reply to: 270163123 [](ancestors = 270163123)

@jcouv jcouv added this to the 16.2 milestone Apr 23, 2019
@jcouv jcouv added the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Jun 6, 2019
@jinujoseph jinujoseph modified the milestones: 16.2, Backlog Aug 5, 2019
@jahmai-ca
Copy link

@chsienki The lack of annotations on operators (cast and others) is inhibiting the adoption of nullable in my project (we use operators a lot). Will this be merged any time soon?

Base automatically changed from master to main March 3, 2021 23:51
@jahmai-ca
Copy link

Bi-yearly check up :) Will this be merged or closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers New Language Feature - Nullable Reference Types Nullable Reference Types PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotations do not work for user defined conversion operators
5 participants