Skip to content

match expression ignores inline command #10919

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

Closed
antognini opened this issue Dec 26, 2020 · 2 comments
Closed

match expression ignores inline command #10919

antognini opened this issue Dec 26, 2020 · 2 comments

Comments

@antognini
Copy link

antognini commented Dec 26, 2020

Minimized code

3.0.0-M3

object Inline1:
  inline def foo( i:Int): Int =
    inline if i == 0 then -1 
    else Math.abs(i)

  def bar(i:Int): Int = foo(i) // as expected, fails to compile
  //                    ^^^^^^
  // Cannot reduce `inline if` because its condition is not a constant value: i.==(0)


object Inline2:
  inline def foo( i:Int): Int =
    inline i match
      case 0     => -1
      case x:Int => Math.abs(x)
  
  def bar(i:Int): Int = foo(i) // a compilation error was expected, but none is reported

Expectation

inline is a command in Scala 3.0.0, not an implementation hint:
=> def bar() should fail in conjunction with inline match, as it does with inline if.

@odersky
Copy link
Contributor

odersky commented Dec 26, 2020

That's not how it is specified, and a lot of code relies on this behavior.

@odersky odersky closed this as completed Dec 26, 2020
@SethTisue
Copy link
Member

SethTisue commented Sep 21, 2022

I think the current behavior is comprehensible, as follows. At compile time, case 0 isn't known to match, so the compiler proceeds to the next case, which does match. It may help to understand that here case 0 is a type test, not a runtime equality test. If it's the other semantics you wanted, inline if is available.

related: #13774 — there is some discussion there. It's probably better to discuss further on 13774 unless someone wants to make a case that this ticket is definitely different.

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

3 participants