Skip to content
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

Handle nullability info in by-name arguments #7727

Merged
merged 4 commits into from
Dec 14, 2019

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Dec 12, 2019

Implements the scheme described in my comment to #7546.

  • We type check an argument without or with flow info,
    depending on whether the function part of the application
    is known to take a call-by-name parameter. If we know nothing
    about the function part, we assume call-by-value.
  • At the end of type checking an application, if the argument is
    known to be by-name, remove all x.$asInstanceOf$[T] casts in the
    argument where x is a mutable variable, and run the (re-)typer again
    on the result. If this succeeds and gives a type that is still compatible
    with the formal parameter type, we are done.
  • Otherwise, issue an error saying that the argument cannot be treated
    as call-by-name since it contains flow-assumptions about mutable variables.
    As a remedy, suggest to wrap the argument in a scala.compiletime.byName(...)
    call. Here, byName is defined as follws:
    inline def byName[T](x: => T): T
    
    Wrapping an argument with byName means that we know statically that
    it is passed to a by-name parameter, so it will be typechecked without
    flow info.

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala Outdated Show resolved Hide resolved
compiler/src/dotty/tools/dotc/typer/Applications.scala Outdated Show resolved Hide resolved
compiler/src/dotty/tools/dotc/typer/Applications.scala Outdated Show resolved Hide resolved
Implements the scheme described in my comment to scala#7546.

 - We type check an argument without or with flow info,
   depending on whether the function part of the application
   is known to take a call-by-name parameter. If we know nothing
   about the function part, we assume call-by-value.
 - At the end of type checking an application, if the argument is
   known to be by-name, remove all x.$asInstanceOf$[T] casts in the
   argument where x is a mutable variable, and run the type assigner
   (not the type checker!) again on the result. If this succeeds and
   gives a type that is still compatible with the formal parameter type,
   we are done.
 - Otherwise, issue an error saying that the argument cannot be treated
   as call-by-name since it contains flow-assumptions about mutable variables.
   As a remedy, suggest to wrap the argument in a `scala.compiletime.byName(...)`
   call. Here, `byName` is defined as follws:
   ```
   inline def byName[T](x: => T): T
   ```
   Wrapping an argument with byName means that we know statically that
   it is passed to a by-name parameter, so it will be typechecked without
   flow info.
@odersky odersky force-pushed the change-byname-nullable branch from a0e3525 to df71c9a Compare December 14, 2019 09:53
@odersky odersky merged commit 0d950fb into scala:master Dec 14, 2019
@odersky odersky deleted the change-byname-nullable branch December 14, 2019 14:06
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.

2 participants