Skip to content

[generator] Be smarter about when members with same names need "new". #567

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

Merged
merged 1 commit into from
Feb 10, 2020

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Feb 7, 2020

Today when a generated member has the same name as a method on System.Object we generate the method with a new in order to hide the base member. However we only compare via the method name and not the method signature, meaning we add more new than is necessary.

For example this creates a warning because new is not needed:

// Org.Json.JSONArray.cs
public new virtual unsafe string ToString (int indentSpaces) { ... }
warning CS0109: The member 'JSONArray.ToString(int)' does not hide an accessible member. The new keyword is not required.

These are the methods from System.Object that we currently check against:

"Boolean Equals(System.Object)"
"Boolean Equals(System.Object, System.Object)"    // static
"Boolean ReferenceEquals(System.Object, System.Object)"    //static
"Int32 GetHashCode()"
"System.Type GetType()"
"System.String ToString()"
plus "Handle"

With this change we now are more explicit about what methods we are checking against so we can ensure that parameters and static-ness match as well.

This eliminates 87 warnings from the API-29 Mono.Android.dll build.

@jpobst jpobst force-pushed the smarter-new branch 2 times, most recently from b7312e8 to 5ee0987 Compare February 10, 2020 17:13
c.Properties.First ().Name = "ReferenceEquals";
Assert.True (c.RequiresNew (c.Properties.First ()));

c.Properties.First ().Name = "Handle2";
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why Handle2 should require a new.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's Asserting that it doesn't require a new. :)

@jonpryor jonpryor merged commit c5815fb into master Feb 10, 2020
@jonpryor jonpryor deleted the smarter-new branch February 10, 2020 21:55
jonpryor pushed a commit that referenced this pull request Feb 19, 2020
…567)

Today when a generated member has the same name as a method on
`System.Object` we generate the method with a `new` in order to hide
the base member.  However, we only compare via the method name and
not the method signature, meaning we add more `new` than is necessary.

For example this creates a warning because `new` is not needed:

	// Org.Json.JSONArray.cs
	public new virtual unsafe string ToString (int indentSpaces) { … }

	warning CS0109: The member 'JSONArray.ToString(int)' does not hide an accessible member. The new keyword is not required.

These are the methods from `System.Object` that we currently check against:

  * `bool Equals (object)`
  * `static bool Equals (object, object)`
  * `static bool ReferenceEquals (object, object)`
  * `int GetHashCode ()`
  * `Type GetType ()`
  * `string ToString ()`

Additionally, all properties named `Handle` must get `new`.

With this change we now are more explicit about what methods we are
checking against so we can ensure that parameters and `static`-ness
match as well.

This eliminates 87 warnings from the API-29 `Mono.Android.dll` build.
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants