Skip to content

Commit

Permalink
test more variants
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Mar 8, 2023
1 parent 132001e commit 2195be1
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
85 changes: 85 additions & 0 deletions tests/pos/i16920-boxed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import language.experimental.relaxedExtensionImports

import scala.annotation.targetName
trait Foo[+T]

def foo[T](t: T): Foo[T] = new Foo[T] {}

object One:
extension (s: Foo[String])
def wow: Unit = println(s)

object Two:
extension (i: Foo[Int])
def wow: Unit = println(i)

object Three:
extension (s: Foo[String])
@targetName("wowString")
def wow: Unit = println(s)
extension (i: Foo[Int])
@targetName("wowInt")
def wow: Unit = println(i)

object Four:
implicit class WowString(s: Foo[String]):
def wow: Unit = println(s)

object Five:
implicit class WowInt(i: Foo[Int]):
def wow: Unit = println(i)

object Compiles:
import Three._
def test: Unit =
foo(5).wow
foo("five").wow

object AlsoCompiles:
import Four._
import Five._
def test: Unit =
foo(5).wow
foo("five").wow

object UsedToFail:
import One._
import Compiles.*
import Two._
def test: Unit =
foo(5).wow
foo("five").wow

object Conflicting:
extension (i: Foo[Int])
def wow: Unit = println(i)

object Named:
import One.wow
import Two.wow
import Conflicting._
def test: Unit =
foo(5).wow // ok
foo("five").wow // ok

object Named2:
import Conflicting._
import One.wow
import Two.wow
def test: Unit =
foo(5).wow // ok
foo("five").wow // ok

val Alias = Two

object Named3:
import Alias._
import Two._
def test: Unit =
foo(5).wow // ok

object Named4:
import Two._
import Alias._
def test: Unit =
foo(5).wow // ok
88 changes: 88 additions & 0 deletions tests/pos/i16920-typeclass.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import language.experimental.relaxedExtensionImports
// evolve tests/pos/i16920.scala to abstract out Wow to a typeclass, more like the typical library use case

trait Wow[T]:
extension (t: T)
def wow: Unit

object One:
given wowStr: Wow[String] with
extension (s: String)
def wow: Unit = println(s)

object Two:
given wowInt: Wow[Int] with
extension (i: Int)
def wow: Unit = println(i)

object Three:
given wowStr: Wow[String] with
extension (s: String)
def wow: Unit = println(s)
given wowInt: Wow[Int] with
extension (i: Int)
def wow: Unit = println(i)

object Four:
implicit class WowString(s: String):
def wow: Unit = println(s)

object Five:
implicit class WowInt(i: Int):
def wow: Unit = println(i)

object Compiles:
import Three.given
def test: Unit =
5.wow
"five".wow

object AlsoCompiles:
import Four.given
import Five.given
def test: Unit =
5.wow
"five".wow

object UsedToFail:
import One.given
import Compiles.*
import Two.given
def test: Unit =
5.wow
"five".wow

object Conflicting:
given wowInt: Wow[Int] with
extension (i: Int)
def wow: Unit = println(i)

object Named:
import One.wowStr
import Two.wowInt
import Conflicting.given
def test: Unit =
5.wow // ok
"five".wow // ok

object Named2:
import Conflicting.given
import One.wowStr
import Two.wowInt
def test: Unit =
5.wow // ok
"five".wow // ok

val Alias = Two

object Named3:
import Alias.given
import Two.given
def test: Unit =
5.wow // ok

object Named4:
import Two.given
import Alias.given
def test: Unit =
5.wow // ok
21 changes: 20 additions & 1 deletion tests/pos/reference/extension-methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,23 @@ object ExtMethods:
import DoubleOps.**
assert(2.0 ** 3 == **(2.0)(3))

end ExtMethods
// ***** changes to shadowing rules with named imports 3.3+ *****
import scala.annotation.targetName
trait Foo[+T]

object Ops:
extension (foo : Foo[Int])
@targetName("bazInt")
def baz : Unit = {}

import Ops.*

extension (foo : Foo[Double])
@targetName("bazDouble")
def baz : Unit = {}


val f = new Foo[Int] {}
f.baz //error

end ExtMethods

0 comments on commit 2195be1

Please sign in to comment.