Skip to content

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

Conversation

nicolasstucki
Copy link
Contributor

Inline overrides can only use inline parameters if the overriden parameter is inline.

@nicolasstucki nicolasstucki self-assigned this Apr 23, 2020
@nicolasstucki nicolasstucki force-pushed the only-allow-sound-inline-parameters-in-overrides branch 2 times, most recently from 43e195b to 839ce05 Compare April 23, 2020 17:41
@nicolasstucki
Copy link
Contributor Author

Ping @sjrd

@nicolasstucki nicolasstucki requested a review from odersky April 23, 2020 17:43
@smarter smarter added this to the 0.24.0-RC1 milestone Apr 23, 2020
@nicolasstucki nicolasstucki force-pushed the only-allow-sound-inline-parameters-in-overrides branch 2 times, most recently from 46cba9e to 18eae39 Compare April 24, 2020 06:29
Inline overrides can only use inline parameters if the overriden parameter is inline.
@nicolasstucki nicolasstucki force-pushed the only-allow-sound-inline-parameters-in-overrides branch from 18eae39 to 89672bf Compare April 24, 2020 07:30
@nicolasstucki nicolasstucki marked this pull request as ready for review April 24, 2020 09:32
for
sym2 <- sym.allOverriddenSymbols
(p1, p2) <- params.lazyZip(sym2.paramSymss.flatten)
if p1.is(Inline) && !p2.is(Inline)
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

@nicolasstucki nicolasstucki requested review from sjrd and removed request for odersky April 24, 2020 17:13
@nicolasstucki nicolasstucki merged commit 17d1633 into scala:master Apr 24, 2020
@nicolasstucki nicolasstucki deleted the only-allow-sound-inline-parameters-in-overrides branch April 24, 2020 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants