Skip to content

Commit

Permalink
Fix i14451 (#16010)
Browse files Browse the repository at this point in the history
Fixes #14451

Implicitly assumed type clauses could only be at the beginning, which is
wrong since:
```scala
extension (x: Int) def foo[T](y: T) = ???
```
de-sugars to something like:
```scala
def foo(x: Int)[T](y: T) = ???
```

To fix it, I implement `stripInferrable`, a variant of `stripImplicit`
which also drops type clauses, and use it in `resultIsMethod`
I suspect the other uses of `stripImplicit` could be simplified, or even
fixed (assuming they make the same mistake as `resultIsMethod`), by
using `stripInferrable`
  • Loading branch information
bishabosha authored Sep 15, 2022
2 parents 684ae79 + c87e56b commit 9cbc48b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,17 @@ trait Applications extends Compatibility {
}
}

/** Drop any implicit parameter section */
/** Drop any leading type or implicit parameter sections */
def stripInferrable(tp: Type)(using Context): Type = tp match {
case mt: MethodType if mt.isImplicitMethod =>
stripInferrable(resultTypeApprox(mt))
case pt: PolyType =>
stripInferrable(pt.resType)
case _ =>
tp
}

/** Drop any leading implicit parameter sections */
def stripImplicit(tp: Type)(using Context): Type = tp match {
case mt: MethodType if mt.isImplicitMethod =>
stripImplicit(resultTypeApprox(mt))
Expand Down Expand Up @@ -2042,7 +2052,7 @@ trait Applications extends Compatibility {
skip(alt.widen)

def resultIsMethod(tp: Type): Boolean = tp.widen.stripPoly match
case tp: MethodType => stripImplicit(tp.resultType).isInstanceOf[MethodType]
case tp: MethodType => stripInferrable(tp.resultType).isInstanceOf[MethodType]
case _ => false

record("resolveOverloaded.narrowedApplicable", candidates.length)
Expand Down
27 changes: 27 additions & 0 deletions tests/pos/i14451.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

class Foo

extension (dfVal: Foo)
def f0(step: Int): Foo = ???
def f0: Foo = ???
val v0 = (new Foo).f0

extension (dfVal: Foo)
def f1[T](step: Int): Foo = ???
def f1: Foo = ???
val v1 = (new Foo).f1

extension (dfVal: Foo)
def f2[T](step: Int): Foo = ???
def f2[T]: Foo = ???
val v2 = (new Foo).f2

extension [A](dfVal: Foo)
def f3[T](step: Int): Foo = ???
def f3: Foo = ???
val v3 = (new Foo).f3

extension [A](dfVal: Foo)
def f4[T](step: Int): Foo = ???
def f4[T]: Foo = ???
val v4 = (new Foo).f4

0 comments on commit 9cbc48b

Please sign in to comment.