Skip to content

Update for new tiebreaker rules - #4677

Merged
BillWagner merged 8 commits into
dotnet:masterfrom
BillWagner:overload-in-value
Mar 23, 2018
Merged

BillWagner merged 8 commits into
dotnet:masterfrom
BillWagner:overload-in-value

Conversation

@BillWagner

@BillWagner BillWagner commented Mar 15, 2018 •

Copy link
Copy Markdown
Member

Fixes #3949
Fixes #3907

Update the locations where in and by value tiebreaker rules have changed.

The keys to the rules are that in is optional at call sites. However, when it is used at call sites, the developer forces the compiler to pick the in method overload, and declares that the compiler must pass a reference to the argument, not a temporary created from the argument.

/cc @VSadov @jcouv

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

with a matching in parameter ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"expression" duplicated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

technically properties can be ref-returning and then they are lvalues and can be used as "in" arguments.
Perhaps says something like " . . . constants, ordinary properties or other expressions that produce values."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

to any field of an in parameter when it has struct type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This restriction also applies to fields if parameter is a struct. Basically if a struct variable is readonly its fields are readonly, recursively. Not sure how to say it better.
Basically inParam.field1.field2.field3 cannot be assigned to, passed by ref, etc... , just the same as inParam itself if inParsm, field1, field2 are all structs and inParam is an in parameter.

@VSadov

VSadov commented Mar 15, 2018

Copy link
Copy Markdown
Member

Had some comments, but otherwise looks very good!

@BillWagner BillWagner changed the title [WIP] update for new tiebreaker rules Update for new tiebreaker rules Mar 16, 2018
@BillWagner
BillWagner requested a review from rpetrusha March 16, 2018 20:16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

NIT: most understand or most easily understand ? - not sure about english.

@VSadov VSadov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@BillWagner

Copy link
Copy Markdown
Member Author

closing and reopening to force a new build

@BillWagner BillWagner closed this Mar 16, 2018
@BillWagner BillWagner reopened this Mar 16, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it necessary to say that a temporary int is created here? Especially considering that it does not actually create a temporary local variable in IL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo: specifiese → specifies

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wasn't "4-byte" correct? If not, shouldn't "8-byte" below be fixed too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

4-byte is correct.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The compiler enforces the in argument and its struct members are readonly variables.

This implies that a struct field of a class in argument should be readonly, but that's not true.

I think the explanation before that is correct, it's just the summary in the last sentence that is problematic.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo: follow → following?

@rpetrusha rpetrusha left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've left a number of suggestions, comments, and questions, @BillWagner.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the "because" doesn't seem to fully explain why it's optional; I suspect because this is the default
would explicitly --> explicitly
callsite --> call site (there are some other instances later where callsite is used as well)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there should be a comma before "which"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since in is an advanced feature, this may not be an issue at all. But many developers find overload resolution confusing (or completely mysterious), so I'm wondering if it's worthwhile to add a brief explanation maybe with some simple code that asks how the compiler can choose the correct overload.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did some searching, and we don't have an article that explains method overloads and the basic rules in the C# docset. (I was surprised by that, and I may have missed it).

I'd rather not bury the introduction to method overloads here. I can add an issue to create that article, and make sure it's referenced from here. Do you agree?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I definitely agree. I'll add the issue -- we really need articles for both the C# and the VB compilers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

forces selection of --> forces the compiler to select

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

temporary has --> temporary variable has

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

of copies --> of making copies?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"that" is a vague reference here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

built in --> built-in
what are built-in numeric values? constants? numeric literals?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I deleted "built in". (It was taken from the draft spec) It refers to the numeric types: byte, short, int, long, double and so on)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This contradicts line 90 and following.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

PR #4710 address this (based on my review comments).

Either way, I'll update this paragraph after merging that PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the original differently is correct

BillWagner and others added 7 commits March 22, 2018 10:56
Fixes #3949

Update the locations where `in` and by value tiebreaker rules have changed.
I intended that on the previous commit, but missed it.
Revisit after merging #4770 for one final comment.
@BillWagner

BillWagner commented Mar 22, 2018 •

Copy link
Copy Markdown
Member Author

@rpetrusha I've addressed all the feedback from the earlier review, except for one comment where I had a question. Do you have recommendations as to the right guidance there?

@rpetrusha rpetrusha left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks really good, @BillWagner. I've left one comment unrelated to your PR, one suggested change, and also noted cases where "read only" should be "read-only". You can merge when you're ready.


> [!NOTE]
> The `in` keyword can also be used with a generic type parameter to specify that the type parameter is contravariant, as part of a `foreach` statement, or as part of a `join` clause in a LINQ query. For more information on the use of the `in` keyword in these contexts, see [in](in.md) which provides links to all those uses.
> The `in` keyword can also be used with a generic type parameter to specify that the type parameter is contravariant, as part of a `foreach` statement, or as part of a `join` clause in a LINQ query. For more information on the use of the `in` keyword in these contexts, see [in](in.md), which provides links to all those uses.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is unrelated to your PR, but the in disambiguation topic is in the Query Keywords section in the TOC, which seems like the wrong place.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm going to create a new issue for this: #4757
That way, this PR stays focused on this single topic.


First, specifying `in` at the call site forces the compiler to select a method defined with a matching `in` parameter. Otherwise, when two methods differ only in the presence of `in`, the by value overload is a better match.

Second, specifying `in` declares your intent to pass an argument by reference. The argument used with `in` must represent a location that can be directly referred to. The same general rules for `out` and `ref` arguments apply: You cannot use constants, ordinary properties, or other expressions that produce values. Omitting `in` at the call site informs the compiler that you will allow the compiler to create a temporary variable to pass by read-only reference to the method. The compiler creates a temporary variable to overcome several restrictions with `in` arguments:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This remains confusing -- it was only on the fourth reading that I noticed the "Omitting...". I think that the issue can be addressed by adding "Otherwise, omitting...", which also makes it parallel to the previous paragraph.

the value passed to it.
and `out` keywords to pass arguments
by reference. The `in` keyword specifies passing
the argument by reference but, the called method does not modify

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the comma should precede the but, not follow it

apply for multiple layers of member access provided the types at all levels
of member access are `structs`.
The compiler enforces that `struct` types passed as `in` arguments and their
`struct` members are read only variables when used as arguments to other methods.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

read only --> read-only

- The argument is an expression but does not have a known storage variable.
- An overload exists that differs by the presence or absence of `in`. In that case, the by value overload is a better match.

These rules are useful as you update existing code to use read only

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

read only --> read-only


When you assign a variable to a `ref readonly return`, you can specify either a `ref readonly`
variable, or a by-value copy of the readonly reference:
variable, or a by-value copy of the read only reference:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

read only --> read-only

Applying `ref readonly` to high-traffic uses of a struct may be sufficient.
Other times, you may want to create an immutable struct. Then you can
always pass by readonly reference. That practice
always pass by read only reference. That practice

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

read-only

@BillWagner
BillWagner merged commit a11be50 into dotnet:master Mar 23, 2018
@BillWagner
BillWagner deleted the overload-in-value branch March 26, 2018 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants