-
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
Eliminate Warning CS1957 (runtime ambiguous override) by generating correct code #44067
Comments
The simplest fix is to modify (and rename) |
At the moment the removal of the warning doesn't look like the right thing to do due to dotnet/runtime#38119. |
Not sure if we intend to have the C# compiler produce warnings for runtime-version-specific bugs. If so, the warning should be reworded to identify the bug and suggest upgrading the runtime. |
The wording of the warning is correct no matter what version of the runtime is used: "Member '{1}' overrides '{0}'. There are multiple override candidates at run-time. It is implementation dependent which method will be called." What method is called is indeed implementation dependent, even when the "right" one is called. |
The CLI specification is not ambiguous about which method will be called. There is only one candidate at runtime. The only reason it is implementation-dependent is that some implementations are incorrect. |
The actual reason makes absolutely no difference in my opinion. We are not trying to state it in the warning. |
Sure, if we have a policy of producing warnings when we generate code that might be incorrect on some implementation because of runtime bugs. |
We could emit explicit override long time ago, we already did in some scenarios. Why do you think we didn't do that and instead kept reporting the warning? |
I think we didn't do that because that portion of the Roslyn compiler was transcribed from a similar part of the native compiler, which only added the use of a class |
That is definitely not the case. We, even in the native compiler time, knew about that and even used it in some scenarios in classes. But not tried to use it for these scenarios. My guess, we new it wouldn't help. |
@AlekseyTs I'm working from evidence rather than guessing. The native and Roslyn compilers never used methodimpl entries for methods declared In any case, we do not have a policy of reporting warnings on source that might encounter bugs specific to some version of the runtime. |
The warning does state a reason. It says that there are multiple candidates at runtime. Since there is one candidate at runtime, I prefer to reword of remove the diagnostic. |
That is definitely not the case. Please look at the implementation and usage of
Similar method exists in VB compiler and it uses "methodimpl entries for methods declared override" even in more scenarios. |
I stand corrected. More precisely, the compilers did not produce a methodimpl in order to disambiguate multiple candidate overridden methods. |
So, do you still think we we didn't know that methidimpl can be used with overrides in classes? |
Yes, I still think the compiler team documented that there was no mechanism to disambiguate runtime ambiguous overrides (unlike the situation with the code quoted in the compiler that uses methodimpl to correct runtime unambiguous but incorrect overrides). |
You are answering the question that wasn't asked and do not answer the question that was asked. I wonder why. |
This, by the way, is still correct. There is no mechanism to disambiguate runtime ambiguous overrides |
Perhaps because I assumed you had read my previous comment which answered your question and clarified what I think.
That wasn't what I claimed. I claimed that the compiler team thought (incorrectly, as it turns out) that there was no mechanism to communicate a disambiguation to the runtime. |
The belief by the compiler team that there was no such mechanism to express it was incorrect. The mechanism is documented in the ECMA specification, though some runtimes do not implement it correctly yet. I am aware of no evidence anybody was aware of that bug before this week. We expect this to be implemented correctly in .net 5. |
I am that evidence. I was aware of the way runtime behaves since the native compiler times. I new that emitting explicit override doesn't help, at least in some scenarios. That is why I made the comment on the PR that tried to eliminate the warning and asked you to properly test your assumptions and see for yourself.
I definitely support fixing this long standing bug in runtime. And will be happy to return to discussion about the warning once that happened. Until then, I think, doing anything is premature. |
* Implement and test retargeting symbols for covariant returns. * A PE symbol with multiple methodimpl entries is ignored as an explicit override for language covariant returns. * Test binary compatibility scenarios with overrides inserted into the hierarchy. * Test a scenario with duplicate requirements for a methodimpl entry * A methodimpl may be required anytime the runtime and language disagree about the overridden method. (As a side-effect, this fixes a number of previously-believed-unfixable bugs regarding the mismatch between C# and CIL) * Emit a call to the least derived override in the hierarchy with the correct return type. * Test covariant returns in expression trees. * Test capturing a covariant method in a delegate creation. * Test consumption and override of covariantly overridden methods, properties, and indexers from VB. * Require that the target runtime supports covariant returns when the feature is used. * Adjust tests for new special member. * Test compile-time behavior of nullable variance in covariant returns. * Produce RequireMethodImplToRemainInEffectAttribute on covariant overrides. * Eliminate CS1957 by generating correct code. Fixes #44067 * Cache `PEMethodSymbol.ExplicitlyOverriddenClassMethod` Closes #44068 * Move some PROTOTYPE comments to issues. Relates to #44206, #44207, #44208, #44209 * Remove virtual `ExplicitlyOverriddenClassMethod` from MethodSymbol. Instead provide (and test) leaf APIs to be used directly by `OverriddenOrHiddenMembersHelpers`. * Enhance the covariant return tests per PR review. * Rename `RequireMethodImplToRemainInEffectAttribute` to `PreserveBaseOverridesAttribute` per API review * Document breaking change * Changes per review comments - Revert behavior of PEMethodSymbol.IsOverride (true even if we cannot report a unique overridden method) - Add runtime test for behavior of methodimpl in ambiguous scenarios (reported dotnet/runtime#38119) * Modify how overrides are computed for PE symbols as requested in code review. * Condition WRN_MultipleRuntimeOverrideMatches on whether runtime has working methodimpl Relates to dotnet/runtime#38119 Fixes #45453 * Test `SourceMethodSymbol.RequiresExplicitOverride` in ambiguous scenarios. * Preserve historical test for methodimpl generation as a fallback.
Fixed in #44025 |
The compiler sometimes reports CS1957:
warning CS1957: Member 'Derived.Method(int, ref int)' overrides 'Base<int>.Method(int, ref int)'. There are multiple override candidates at run-time. It is implementation dependent which method will be called.
The compiler is capable of emitting a methodimpl record which will disambiguate the situation for the runtime. If we do that, there will never be any need to produce this warning. Perhaps more importantly, the compile-time behavior and the runtime behavior will agree.
The text was updated successfully, but these errors were encountered: