Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {

/** The current tree applied to given type argument list: `tree[targs(0), ..., targs(targs.length - 1)]` */
def appliedToTypeTrees(targs: List[Tree])(using Context): Tree =
if targs.isEmpty then tree else TypeApply(tree, targs)
if targs.isEmpty then tree else tree match
case Block(stmts, expr) if stmts.nonEmpty => Block(stmts, TypeApply(expr, targs))
case _ => TypeApply(tree, targs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is suspicious. What if it's an If where the two branches need a type application? What is it about Blocks that deserves a special-case here, but not other constructs? Also why do this for type arguments but not term arguments?

If there is a specific call site that for some reason creates a Block, then that call site is probably the correct place to take care of it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's not an amazing fix, but the bit where a Block is created seems quite far from this, and I don't see a way to not create a Block there given the need for a temp local to use the argument twice (once as an arg, once as an arg of the default value function of the using)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying the call site should not create a Block. But when it creates a Block, it should make sure to push any type application inside the block itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Block is created in typedUnadapted, and this method here is called in adapt, both from the bit of typed that does adapt(typedUnadapted(...), ...).

I agree in principle we shouldn't have to add a special case here but I don't see how to avoid it without changing a lot of logic.


/** Apply to `()` unless tree's widened type is parameterless */
def ensureApplied(using Context): Tree =
Expand Down
20 changes: 20 additions & 0 deletions tests/pos/26238-min.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
trait Thing[T]
trait G

object Outer:

def thing: Thing[Int] = ???

extension[T1](t: Thing[T1])(using g: G = new G { })
def map[T2](m: T1 => T2): Thing[T2] = ???
def flatMap[T2](m: T1 => Thing[T2]): Thing[T2] = ???

// `y` requires a `flatMap`, and that one requires getting the default value for the `g` argument,
// and getting that default value requires the `t` argument (since default values can depend on previous args),
// which means we end up with a block;
// and since the whole thing is generic, we must apply a generic type argument to that block
for
x <- thing
y <- thing
yield
1
65 changes: 65 additions & 0 deletions tests/pos/26238.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
object toplevel {

@deprecated("pls work this out")
def ??? = scala.Predef.`???`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we at least remove dead code from the non-minimized test?


import util.ChainingOps.{*, given}

object S {

trait AllocatedNodeTypeFamily:
type Node

trait Parser[+A] private[S] () :
type SpcNd

type OutliningParser[+A, +Node]
= Parser[A] { type SpcNd <: Node }

extension [R1] (g1: Parser[R1] )
(using pfmoIspcndi: ParserFlatMapOp.ISpcNdInvariant = new ParserFlatMapOp.ISpcNdInvariant {}.asInstanceOf[ParserFlatMapOp.ISpcNdInvariant { type SpcNdObj = g1.SpcNd } ] )

def map[R2] (m: R1 => ( R2 ) ): OutliningParser[R2, g1.SpcNd] = new Parser[Any] {}.asInstanceOf
def flatMap[R2] (m: R1 => (Parser[R2] ) ): OutliningParser[R2, g1.SpcNd] = new Parser[Any] {}.asInstanceOf

object ParserFlatMapOp {

given given_ISpcNdInvariant_PModAllocatedNodeTypeFamily
: (x: AllocatedNodeTypeFamily ) => (ISpcNdInvariant { type SpcNdObj = x.Node })
= new ISpcNdInvariant {}.asInstanceOf

trait ISpcNdInvariant :
type SpcNdObj

} // ParserFlatMapOp.

def defaultIntParser
: Parser[Int]
= new Parser[Any] {}.asInstanceOf

} // S.

System.err.println(Math.random() )

System.err.println(Math.random() )

locally :
System.err.println(Math.random() )

// given S.ParserFlatMapOp.ISpcNdInvariant
// = (new S.ParserFlatMapOp.ISpcNdInvariant {}).asInstanceOf

/* run locally you might see something like say "unexpected tree for type application" or the like.. 😒 */
/* commented out, the whole code would compile and run like it should.. */
locally :
for
r1 <- S.defaultIntParser
r2 <- S.defaultIntParser
yield { 5 }
.toString()

System.err.println(s"done.")

System.err.println(Math.random() )

}
Loading