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

Inductive given using Tuple.Tail can't be derived when tuple is of size >3 #17115

Closed
fazzou opened this issue Mar 15, 2023 · 6 comments
Closed

Comments

@fazzou
Copy link

fazzou commented Mar 15, 2023

Compiler version

3.2.2 (also tried on 3.3.0-RC3 & 3.1.3)

Minimized code

trait A[T <: Tuple] { val x: Int }
given empty: A[EmptyTuple] with { val x = 1 }
given inductive[Tup <: NonEmptyTuple](using A[Tuple.Tail[Tup]]): A[Tup] with { val x = summon[A[Tuple.Tail[Tup]]].x + 1 }

println(summon[A[(String, String, String)]].x) //this line is fine
println(summon[A[(String, String, String, String)]].x) //this line gives error

Output

> scala-cli --scala 3 test.sc
Compiling project (Scala 3.2.2, JVM)
[error] ./test.sc:6:52
[error] No given instance of type test.A[(String, String, String, String)] was found for parameter x of method summon in object Predef.
[error] I found:
[error]
[error]     test.inductive[(String, String, String, String)](
[error]       test.inductive[Tuple.Tail[(String, String, String, String)]](
[error]         /* missing */
[error]           summon[test.A[Tuple.Tail[Tuple.Tail[(String, String, String, String)]]]]
[error]       )
[error]     )
[error]
[error] But given instance inductive in object test produces a diverging implicit search when trying to match type test.A[Tuple.Tail[Tuple.Tail[(String, String, String, String)]]].
[error]
[error] Note: given instance inductive in object test was not considered because it was not imported with `import given`.
[error] println(summon[A[(String, String, String, String)]].x)
[error]                                                    ^
Error compiling project (Scala 3.2.2, JVM)
Compilation failed

Expectation

Program compiling successfully and printing

4
5
@fazzou fazzou added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 15, 2023
@nicolasstucki nicolasstucki added area:tuples and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 16, 2023
@nicolasstucki
Copy link
Contributor

I got an error in both cases.

@nicolasstucki
Copy link
Contributor

It seems that the match types are not getting simplified in the recursive case.

@nicolasstucki
Copy link
Contributor

I tried this change

-    inferImplicit(formal, EmptyTree, span) match
+    inferImplicit(formal.simplified, EmptyTree, span) match

here https://github.com/lampepfl/dotty/blob/main/compiler/src/dotty/tools/dotc/typer/Implicits.scala#L882

This fixed the issue but started breaking uses of throws.

@odersky
Copy link
Contributor

odersky commented Mar 16, 2023

One has to be very, very careful with simplified. It is a heavy high-level operation. It can seem to fix many things, but it also forces too much which can break other things. So best not to use it in places other where it is already used today.

@fazzou
Copy link
Author

fazzou commented Mar 16, 2023

I got an error in both cases.

Hmm for me & on scastie it seemed deterministically fail on the 2nd one, though not sure, maybe there is some context that can influence that behavior.

@prolativ
Copy link
Contributor

For me the first one seems to always work too

@odersky odersky removed their assignment Mar 28, 2023
@sjrd sjrd self-assigned this Aug 30, 2023
@sjrd sjrd closed this as completed in ad29ce8 Sep 1, 2023
@sjrd sjrd added this to the 3.4.0 milestone Sep 1, 2023
WojciechMazur pushed a commit that referenced this issue Jun 19, 2024
WojciechMazur added a commit that referenced this issue Jun 20, 2024
…o LTS (#20605)

Backports #18386 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
WojciechMazur pushed a commit that referenced this issue Jun 20, 2024
WojciechMazur added a commit that referenced this issue Jun 20, 2024
…o LTS (#20665)

Backports #18386 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants