Fix issue #22310: Override checking incorrectly accepts incompatible generic types #23839
+68
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes a bug where the compiler incorrectly allowed
override
declarations that don't actually override anything when generic types with incompatible type arguments were involved.Problem
The following code should produce an "overrides nothing" error but was incorrectly accepted:
When
A1
extendsA0[String]
, the inherited method becomesfunc(arg0: A0[String], arg1: String): Unit
. The override attempt withA0[Object]
should fail becauseA0[Object]
andA0[String]
are incompatible types (generic classes are invariant by default).Root Cause
The issue was in the override checking logic in
RefChecks.scala
. TheisSignatureMatch
function used signature-based matching that erased type arguments for user-defined generic classes. This causedA0[String]
andA0[Object]
to be treated as having the same signature, leading to false positive matches.The signature matching logic in
TypeErasure.sigName()
only preserves type arguments for built-in types likeArray
andPair
, but erases them for user-defined classes through theerasureDependsOnArgs
check.Solution
Modified the override checking logic to use strict type comparison by calling
matchesLoosely(other, alwaysCompareTypes = true)
instead ofmatches()
. This ensures that even when signatures match after type erasure, the system performs a full type comparison usinginfo.matches(other.info)
to validate that the parameter types are actually compatible.Changes
compiler/src/dotty/tools/dotc/typer/RefChecks.scala
: UpdatedisSignatureMatch
to usematchesLoosely
withalwaysCompareTypes = true
for strict type checking in override validationtests/neg/i22310.scala
: Added comprehensive test cases covering the reported issue and similar scenariostests/neg/i22310.check
: Expected compilation errors for invalid overridestests/pos/i22310-valid.scala
: Test cases ensuring valid overrides continue to work correctlyThe fix is minimal and targeted, only affecting override checking behavior while preserving all existing valid override scenarios including proper variance handling.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
esm.ubuntu.com
/usr/lib/apt/methods/https
(dns block)git.io
curl -Ls REDACTED
(dns block)repo.scala-sbt.org
/usr/lib/apt/methods/https
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.