Skip to content

inline match over Option[String] not accept None as scrutinee #14245

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
rssh opened this issue Jan 10, 2022 · 11 comments
Closed

inline match over Option[String] not accept None as scrutinee #14245

rssh opened this issue Jan 10, 2022 · 11 comments

Comments

@rssh
Copy link
Contributor

rssh commented Jan 10, 2022

Compiler version

Master on state of Tue Jan 11 00:12:54 EET 2022

Minimized code

object Test {

  inline def get: Option[String] = None

  private transparent inline def fun: Unit =
    inline get match
      case Some(x) => println("value = $x")
      case None    => println("value not set")

  fun

}

Output

[info] compiling 1 Scala source to /Users/rssh/tests/dotty/compileTimeNoCast/target/scala-3.1.2-RC1-bin-SNAPSHOT/classes ...
[error] -- [E007] Type Mismatch Error: /Users/rssh/tests/dotty/compileTimeNoCast/src/main/scala/x/Test.scala:12:2 
[error] 12 |  fun
[error]    |  ^^^
[error]    |  Found:    Option[String]
[error]    |  Required: None.type
[error]    | This location contains code that was inlined from Test.scala:5
[error]    | This location contains code that was inlined from Test.scala:8
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 3 s, completed 11 Jan 2022, 00:14:05

Expectation

should be compiled.

@rssh rssh added the itype:bug label Jan 10, 2022
@rssh
Copy link
Contributor Author

rssh commented Jan 10, 2022

// discovered during porting example from #12039. into #14234

@som-snytt
Copy link
Contributor

Where we used to say, "full disclosure", we now say "transparent inline." Without much experience, this seems expected to me.

Also, the elimination of closures ought to be called "disclosure".

@rssh
Copy link
Contributor Author

rssh commented Jan 11, 2022

slightly minimized:

object Test {

  inline def get: Option[String] = None

  inline def fun: Unit =
    inline get match
      case Some(x) => println("value = $x")
      case None    => println("value not set")

  fun

}

@rssh
Copy link
Contributor Author

rssh commented Jan 11, 2022

It has supposed to work in April 2021. But yes, expectations can change over time.

rssh added a commit to rssh/dotty that referenced this issue Jan 11, 2022
rssh added a commit to rssh/dotty that referenced this issue Jan 11, 2022
…Scala2

 added @Experimenta annotation to new API method

 modified message, according to review

Update library/src/scala/quoted/Quotes.scala

Co-authored-by: Nicolas Stucki <[email protected]>

 settings -> XmacroSettings

 added CompilationInfo.XmacroSettings to MiMaFilters

 corrected MiMaFilters

 added examples from scala#12039 with workarround against scala#14245

Co-authored-by:  David Barri <[email protected]>
@dwijnand
Copy link
Member

Without much experience, this seems expected to me.

I likely have the same or less experience with inline semantics, but this doesn't seem expected to me, given the explicit type annotation on get. Now, had get been transparent or declared : None.type or inferred type, then sure, but as it stands I expect it to inline presenting itself as an Option[String].

Is that not how inlining works in general? I.e. is this a match-specific bug?

rssh added a commit to rssh/dotty that referenced this issue Jan 11, 2022
…Scala2

 added @Experimenta annotation to new API method

 modified message, according to review

Update library/src/scala/quoted/Quotes.scala

Co-authored-by: Nicolas Stucki <[email protected]>

 settings -> XmacroSettings

 added CompilationInfo.XmacroSettings to MiMaFilters

 corrected MiMaFilters

 added examples from scala#12039 with workarround against scala#14245

Co-authored-by:  David Barri <[email protected]>

 remove trailing '\n' from file
rssh added a commit to rssh/dotty that referenced this issue Jan 11, 2022
…Scala2

 added @Experimenta annotation to new API method

 modified message, according to review

Update library/src/scala/quoted/Quotes.scala

 settings -> XmacroSettings

 added CompilationInfo.XmacroSettings to MiMaFilters

 corrected MiMaFilters

 added examples from scala#12039 with workarround against scala#14245

 remove trailing '\n' from file

Co-authored-by: David Barri <[email protected]>
Co-authored-by: Nicolas Stucki <[email protected]>
@bishabosha
Copy link
Member

bishabosha commented Jan 11, 2022

Inline match can only work on the static type of an expression, not its actual "code", so an Option[String] can not be reduced to Some or None. We have discussed several times about looking at the expression instead, but it never went far - again it would be cool to maybe hook into the knowledge that we have FromExpr which could potentially save some work implementing this (apart from not preserving positions).

@SethTisue
Copy link
Member

We have discussed several times about looking at the expression instead

I'm interested what that would even mean and what the design implications are.

@dwijnand
Copy link
Member

Inline match can only work on the static type of an expression, not its actual "code", so an Option[String] can not be reduced to Some or None.

Ah ok, so this is right to not compile, it just doesn't compile for an accidental reason (the scrutinee binding type mismatches) rather than the real reason (you can't statically reduce an Option in that inline match).

@rssh
Copy link
Contributor Author

rssh commented Jan 11, 2022

One question left -- why did the original pull request (#12039) passes all tests. So, the inline match was able to statically reduce an Option a few months ago?

@dwijnand
Copy link
Member

Perhaps because there envGet is transparent, so using the RHS static type rather than the get static type is expected.

@rssh
Copy link
Contributor Author

rssh commented Jan 11, 2022

yes, that was it... (Hmm, but port without macros still not possible, because compiletime.error now want constant literal).

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

6 participants