Skip to content

Commit 66bfc9c

Browse files
WojciechMazuroderskyGedochaosom-snytt
authored
Backport "Tweak type for tryParameterless" to 3.8.0 (#24797)
Backports #24716 to the 3.8.0-RC4. PR submitted by the release tooling. [skip ci] --------- Co-authored-by: odersky <[email protected]> Co-authored-by: Piotr Chabelski <[email protected]> Co-authored-by: Som Snytt <[email protected]>
1 parent 4e8dbfd commit 66bfc9c

File tree

4 files changed

+70
-53
lines changed

4 files changed

+70
-53
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4288,72 +4288,64 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42884288
val allDenots = ref.denot.alternatives
42894289
if pt.isExtensionApplyProto then allDenots.filter(_.symbol.is(ExtensionMethod))
42904290
else allDenots
4291+
def altRef(alt: SingleDenotation) = TermRef(ref.prefix, ref.name, alt)
4292+
val alts = altDenots.map(altRef)
42914293

42924294
typr.println(i"adapt overloaded $ref with alternatives ${altDenots map (_.info)}%\n\n %")
42934295

42944296
/** Search for an alternative that does not take parameters.
42954297
* If there is one, return it, otherwise return the error tree.
42964298
*/
4297-
def tryParameterless(alts: List[TermRef])(error: => tpd.Tree): Tree =
4299+
def tryParameterless(error: => tpd.Tree): Tree =
42984300
alts.filter(_.info.isParameterless) match
42994301
case alt :: Nil => readaptSimplified(tree.withType(alt))
43004302
case _ =>
43014303
altDenots.find(_.info.paramInfoss == ListOfNil) match
4302-
case Some(alt) => readaptSimplified(tree.withType(alt.symbol.denot.termRef))
4304+
case Some(alt) => readaptSimplified(tree.withType(altRef(alt)))
43034305
case _ => error
43044306

4305-
def altRef(alt: SingleDenotation) = TermRef(ref.prefix, ref.name, alt)
4306-
val alts = altDenots.map(altRef)
4307-
43084307
resolveOverloaded(alts, pt) match
4309-
case alt :: Nil =>
4310-
readaptSimplified(tree.withType(alt))
4311-
case Nil =>
4312-
// If no alternative matches, there are still two ways to recover:
4313-
// 1. If context is an application, try to insert an apply or implicit
4314-
// 2. If context is not an application, pick a alternative that does
4315-
// not take parameters.
4316-
4317-
def errorNoMatch = errorTree(tree, NoMatchingOverload(altDenots, pt))
4318-
4319-
pt match
4320-
case pt: FunOrPolyProto if pt.applyKind != ApplyKind.Using =>
4321-
// insert apply or convert qualifier, but only for a regular application
4322-
tryInsertApplyOrImplicit(tree, pt, locked)(errorNoMatch)
4323-
case _ =>
4324-
tryParameterless(alts)(errorNoMatch)
4325-
4326-
case ambiAlts =>
4327-
// If there are ambiguous alternatives, and:
4328-
// 1. the types aren't erroneous
4329-
// 2. the expected type is not a function type
4330-
// 3. there exist a parameterless alternative
4331-
//
4332-
// Then, pick the parameterless alternative.
4333-
// See tests/pos/i10715-scala and tests/pos/i10715-java.
4334-
4335-
/** Constructs an "ambiguous overload" error */
4336-
def errorAmbiguous =
4337-
val remainingDenots = altDenots.filter(denot => ambiAlts.contains(altRef(denot)))
4338-
val addendum =
4339-
if ambiAlts.exists(!_.symbol.exists) then
4340-
i"""|
4341-
|
4342-
|Note: Overloaded definitions introduced by refinements cannot be resolved"""
4343-
else ""
4344-
errorTree(tree, AmbiguousOverload(tree, remainingDenots, pt, addendum))
4345-
end errorAmbiguous
4346-
4347-
if tree.tpe.isErroneous || pt.isErroneous then
4348-
tree.withType(UnspecifiedErrorType)
4349-
else
4350-
pt match
4351-
case _: FunProto =>
4352-
errorAmbiguous
4353-
case _ =>
4354-
tryParameterless(alts)(errorAmbiguous)
4355-
4356-
end match
4308+
case alt :: Nil =>
4309+
readaptSimplified(tree.withType(alt))
4310+
case Nil =>
4311+
// If no alternative matches, there are still two ways to recover:
4312+
// 1. If context is an application, try to insert an apply or implicit
4313+
// 2. If context is not an application, pick an alternative that does
4314+
// not take parameters.
4315+
def errorNoMatch = errorTree(tree, NoMatchingOverload(altDenots, pt))
4316+
4317+
pt match
4318+
case pt: FunOrPolyProto if pt.applyKind != ApplyKind.Using =>
4319+
// insert apply or convert qualifier, but only for a regular application
4320+
tryInsertApplyOrImplicit(tree, pt, locked)(errorNoMatch)
4321+
case _ =>
4322+
tryParameterless(errorNoMatch)
4323+
case ambiAlts =>
4324+
// If there are ambiguous alternatives, and:
4325+
// 1. the types aren't erroneous
4326+
// 2. the expected type is not a function type
4327+
// 3. there exists a parameterless alternative
4328+
//
4329+
// Then, pick the parameterless alternative. See tests/pos/i10715-*
4330+
4331+
/** Constructs an "ambiguous overload" error */
4332+
def errorAmbiguous =
4333+
val remainingDenots = altDenots.filter(denot => ambiAlts.contains(altRef(denot)))
4334+
val addendum =
4335+
if ambiAlts.exists(!_.symbol.exists) then
4336+
i"""|
4337+
|
4338+
|Note: Overloaded definitions introduced by refinements cannot be resolved"""
4339+
else ""
4340+
errorTree(tree, AmbiguousOverload(tree, remainingDenots, pt, addendum))
4341+
4342+
pt match
4343+
case pt if tree.tpe.isErroneous || pt.isErroneous =>
4344+
tree.withType(UnspecifiedErrorType)
4345+
case _: FunProto =>
4346+
errorAmbiguous
4347+
case _ =>
4348+
tryParameterless(errorAmbiguous)
43574349
end adaptOverloaded
43584350

43594351
def adaptToArgs(wtp: Type, pt: FunProto): Tree = wtp match {

tests/pos/i24631/TestJava.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package example;
2+
3+
public abstract class TestJava<T> {
4+
public abstract T create(String foo);
5+
6+
// Note that this is the method that's called from Scala code
7+
public T create() { return create(""); }
8+
9+
public static class Concrete extends TestJava<String> {
10+
@Override public String create(String foo) { return foo; }
11+
}
12+
}

tests/pos/i24631/test.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val s = new example.TestJava.Concrete().create
2+
val s2: String = s

tests/pos/i24631b.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//> using options -source:3.0-migration
2+
3+
abstract class C[A]:
4+
def create(s: String): A
5+
def create(): A = create("")
6+
7+
class D extends C[String]:
8+
def create(s: String): String = s
9+
10+
val s = D().create
11+
val s2: String = s

0 commit comments

Comments
 (0)