-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defer "pure expression .. in statement position" warnings until refch…
…ecks. This allows a macro to transform code that used to warn into something that does not.
- Loading branch information
Showing
11 changed files
with
78 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
names-defaults-neg-213.scala:7: warning: a pure expression does nothing in statement position | ||
f1(x = 1) // named arg in 2.13 (value discard), not ambiguous | ||
^ | ||
names-defaults-neg-213.scala:8: error: unknown parameter name: x | ||
Note that assignments in argument position are no longer allowed since Scala 2.13. | ||
To express the assignment expression, wrap it in brackets, e.g., `{ x = ... }`. | ||
f2(x = 1) // error, no parameter named x. error message mentions change in 2.13 | ||
^ | ||
names-defaults-neg-213.scala:13: warning: a pure expression does nothing in statement position | ||
f1(x = 1) // ok, named arg (value discard) | ||
^ | ||
names-defaults-neg-213.scala:14: error: unknown parameter name: x | ||
f2(x = 1) // error (no such parameter). no mention of new semantics in 2.13 | ||
^ | ||
two warnings found | ||
two errors found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox.Context | ||
|
||
object Macro { | ||
def blockToList[T](block: T): List[T] = macro impl[T] | ||
def impl[T: c.WeakTypeTag](c: Context)(block: c.Tree): c.Tree = { | ||
import c.universe._ | ||
block match { | ||
case Block(stats, expr) => | ||
q"_root_.scala.List.apply[${weakTypeOf[T]}](..${stats :+ expr})" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// scalac: -Xfatal-errors | ||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
// We don't want a "pure expression discarded" warning here as the macro will | ||
// eliminate the block | ||
val is = Macro.blockToList[Int] { | ||
1 | ||
2 | ||
3 | ||
} | ||
assert(is == List(1, 2, 3)) | ||
} | ||
} |