-
Notifications
You must be signed in to change notification settings - Fork 1.1k
inline match breaks simple example, documentation? #13774
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
I suspect the following issue is related to this: trait X[T]:
val value = "X"
given X[Int]()
transparent inline def f[T](x: T) = summonFrom {
case xt: X[T] => xt.value
case _ => "None"
}
transparent inline def g[Tup <: NonEmptyTuple](t: Tup) = f(t.head)
transparent inline def h[Tup <: NonEmptyTuple](t: Tup) = t match
case (head *: _) => f(head)
@main def m =
val t = (1, "s")
println(g(t))
println(h(t)) returns different values for cannot reduce inline match with
scrutinee: t : (t : (Int, String))
patterns : case *:.unapply[Any, Tuple](head @ _, _):Any *: Tuple
transparent inline def h[Tup <: NonEmptyTuple](t: Tup) = inline t match |
I found this single line in the "Semantics-Preserving Inlining for Metaprogramming" paper on the topic:
which is about match not being semantics-preserving. I guess this reduces this issue to updating the docs? |
We can mention that inline match works on the static type of an expression, not its runtime type, hence matching the wildcard case when |
That makes it so a chain of |
The documentation says |
This came up last year at #10919, where @odersky said the behavior is not just intended, but "specified". But I really don't see any wording at https://docs.scala-lang.org/scala3/reference/metaprogramming/inline.html that specifies it. In fact, as Nicolas observes, "If there is enough static information to unambiguously take a branch" (wording from that page) can be read as requiring that compilation fail, since the cases overlap. |
I agree with you @SethTisue. "A match expression in the body of an inline method definition may be prefixed by the inline modifier. If there is enough static information to unambiguously take a branch, the expression is reduced to that branch and the type of the result is taken. If not, a compile-time error is raised that reports that the match cannot be reduced." My conclusion (please challenge this) is that inline match is supposed to preserve semantics (like inline if does), but it doesn't. Is it safe to assume we need a fix to reflect this semantic preserving behaviour? In case this assumption is wrong, @odersky can you please clarify what you mean in very old #10919, what you say the behaviour is not just intended, but specified? |
Speaking as a non-expert, which makes me an audience for the simple example, I think the doc is quite clear. However, I would remove the weaselly adverb, "unambiguously". I would also prefer "select" or "pick" to "take". I'd also prefer not to split an infinitive. (I also double-checked, the character's name is Ron Weasley.)
It already says "static type":
But I sympathize with the OP, where there was not enough inlining to have the salutary effect. 🖖 Maybe a lint could warn about non-unambiguously specified inline matches. In the example, if a branch matches on a narrow type ( Also, on the semantics of "semantics", to speak of "preserving" them, one must distinguish "compiletime" and "runtime", a difference encapsulated by the word "static". Unfortunately, that word is overloaded: connotations accrue to it as by "static cling".
That may be usefully redundant. |
Surprisingly warning-free
|
I "committed" my formulation in the proposed PR, but I'm open to suggestions and amendments or emendations that are not mendacious. |
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.0.2
Minimized code
Output
0
Expectation
Give the same output as (namely 1)
and if not, be documented in the "Inline Matches" section of https://docs.scala-lang.org/scala3/reference/metaprogramming/inline.html.
(Note that this does work when
v
has singleton type1
)The text was updated successfully, but these errors were encountered: