-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Inline macros dealias function parameter types too eagerly #15304
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
Comments
This is a limitation in type inference. It can be reproduced with the same inference with inline def typeNameOf[A]: String = ???
inline def typeNameOfF1[A](f: A => Unit): String = ???
def test =
type MyString = String
println(typeNameOf[MyString])
println(typeNameOfF1/*[String]*/ { (x: MyString) => })
println(typeNameOfF1[MyString] { (x: MyString) => })
println(typeNameOfF1/*[Seq[MyString]]*/ { (x: Seq[MyString]) => })
println(typeNameOfF1/*[(MyString, Int)]*/ { (x: (MyString, Int)) => }) As far as I know, there are no guarantees that aliases are kept when inferring a type. Not sure if that is even possible in general. |
As pointed out, this is a type checking/inference limitation. Usually whether a type alias is used or not to denote a type should be transparent. Expect when it is not. In particular, it is not when inspecting types in metaprogramming (as you did). The other problematic area is type unification. Overall, I think it is a good idea to dealias types less eagerly. PR #14586 is my attempt to keep type aliases in more cases if possible. It also fixes this issue. |
Scala 3's inline macros resolve type aliases in function type parameters to the original types eagerly.
This issue didn't happen in Scala 2 macros.
For example, if an inline macro receives Function1[A, B] (type A = OriginalType), there is no way inside the macro to know the detailed information of type A as A is already resolved to the original type.
Compiler version
Scala 3.1.2
Minimized code
Macros.scala
main.sc
Output
Expectation
Type aliases (e.g., type MyStirng = String) in the function type parameters should be preserved inside the macros:
The text was updated successfully, but these errors were encountered: