-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Only allow sound inline parameters in overrides #8785
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
Only allow sound inline parameters in overrides #8785
Conversation
43e195b
to
839ce05
Compare
Ping @sjrd |
46cba9e
to
18eae39
Compare
Inline overrides can only use inline parameters if the overriden parameter is inline.
18eae39
to
89672bf
Compare
Co-Authored-By: Jamie Thompson <[email protected]>
for | ||
sym2 <- sym.allOverriddenSymbols | ||
(p1, p2) <- params.lazyZip(sym2.paramSymss.flatten) | ||
if p1.is(Inline) && !p2.is(Inline) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the reverse? Is it ok for a non-inline parameter to override an inline parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's debatable whether it's OK or desirable. But at least it is sound, because, as @nicolasstucki observed, you can always emulate the semantics of a non-inline
param with an inline
param that you immediately assign to a val
in the body of the method.
However, personally I think we should not allow it either. In Scala, typically overrides must have exactly the same parameters. I think we should also apply that to the quality of being inline
. And if someone really needs that capability, well they can always do the emulation themselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, actually I think they're unsound. If we have a method with two parameters, the first one being either inline
+val
or by-value, we can end up with different evaluation order for the actual arguments:
def foo(inline x: Int, y: Int): Int = {
val x2 = x
x2 * x2 + y
}
foo({ println(1); 3 }, { println(2); 4 })
// prints 2
// prints 1
// returns 13
but
def foo(x: Int, y: Int): Int = {
x * x + y
}
foo({ println(1); 3 }, { println(2); 4 })
// prints 1
// prints 2
// returns 13
So overriding an inline parameter with a by-value parameter is unsound.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already added a commit disallowing them.
Co-Authored-By: Guillaume Martres <[email protected]>
Inline overrides can only use inline parameters if the overriden parameter is inline.