Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert SAM result types to function types #17740

Merged
merged 3 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case RefinedType(parent, nme.apply, mt @ MethodTpe(_, formals, restpe))
if (defn.isNonRefinedFunction(parent) || defn.isErasedFunctionType(parent)) && formals.length == defaultArity =>
(formals, untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef))))
case SAMType(mt @ MethodTpe(_, formals, restpe)) =>
case pt1 @ SAMType(mt @ MethodTpe(_, formals, methResType)) =>
val restpe = methResType match
case mt: MethodType if !mt.isParamDependent => mt.toFunctionType(isJava = pt1.classSymbol.is(JavaDefined))
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if mt is ParamDependent? Can we still get a crash then?

Maybe we should in that case fall back to the default composition with wildcard types?

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried and I think if you make it param dependent it doesn't qualify as a SAM type? I guess it'll be good to have a case in, just to prove it doesn't crash the compiler - and that way you can review that I've written the test case correctly.

Copy link
Contributor

Choose a reason for hiding this comment

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

I tried and I think if you make it param dependent it doesn't qualify as a SAM type?

I doubt that. Types with ParamDependent method types cannot be SAM types. But I don't see that ParamDependent result types are excluded.

case tp => tp
(formals,
if (mt.isResultDependent)
untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef)))
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i17183.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Dependency

trait MyFunc {
def apply(a: Int, b: String)(using Dependency): String
}

case class Context(f: MyFunc)

def test = Context(f = (_, _) => ???)