{"action":"created","comment":{"url":"https://api.github.com/repos/lampepfl/dotty/pulls/comments/55927635","id":55927635,"diff_hunk":"@@ -0,0 +1,99 @@\n+package dotty.tools.dotc\n+package rewrite\n+\n+import util.{SourceFile, Positions}\n+import Positions.Position\n+import core.Contexts.{Context, FreshContext}\n+import collection.mutable\n+\n+/** Handles rewriting of Scala2 files to Dotty */\n+object Rewrites {\n+  private class PatchedFiles extends mutable.HashMap[SourceFile, Patches]\n+\n+  private case class Patch(pos: Position, replacement: String) {\n+    def delta = replacement.length - (pos.end - pos.start)\n+  }\n+\n+  private class Patches(source: SourceFile) {\n+    private val pbuf = new mutable.ListBuffer[Patch]()\n+\n+    def addPatch(pos: Position, replacement: String): Unit =\n+      pbuf += Patch(pos, replacement)\n+\n+    def apply(cs: Array[Char]): Array[Char] = {\n+      val delta = pbuf.map(_.delta).sum\n+      val patches = pbuf.toList.sortBy(_.pos.start)\n+      if (patches.nonEmpty)\n+        patches reduceLeft {(p1, p2) =>\n+          assert(p1.pos.end <= p2.pos.start, s\"overlapping patches: $p1 and $p2\")\n+          p2\n+        }\n+      val ds = new Array[Char](cs.length + delta)\n+      def loop(ps: List[Patch], inIdx: Int, outIdx: Int): Unit = {\n+        def copy(upTo: Int): Int = {\n+          val untouched = upTo - inIdx\n+          Array.copy(cs, inIdx, ds, outIdx, untouched)\n+          outIdx + untouched\n+        }\n+        ps match {\n+          case patch @ Patch(pos, replacement) :: ps1 =>\n+            val outNew = copy(pos.start)\n+            replacement.copyToArray(ds, outNew)\n+            loop(ps1, pos.end, outNew + replacement.length)\n+          case Nil =>\n+            val outNew = copy(cs.length)\n+            assert(outNew == ds.length, s\"$outNew != ${ds.length}\")\n+        }\n+      }\n+      loop(patches, 0, 0)\n+      ds\n+    }\n+\n+    def writeBack(): Unit = {\n+      val out = source.file.output\n+      val chars = apply(source.content)","path":"src/dotty/tools/dotc/rewrite/Rewrites.scala","position":54,"original_position":54,"commit_id":"d30e213f85629f4767b1d3a3e9dd013f61d715c2","original_commit_id":"d30e213f85629f4767b1d3a3e9dd013f61d715c2","user":{"login":"smarter","id":63430,"avatar_url":"https://avatars.githubusercontent.com/u/63430?v=3","gravatar_id":"","url":"https://api.github.com/users/smarter","html_url":"https://github.com/smarter","followers_url":"https://api.github.com/users/smarter/followers","following_url":"https://api.github.com/users/smarter/following{/other_user}","gists_url":"https://api.github.com/users/smarter/gists{/gist_id}","starred_url":"https://api.github.com/users/smarter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarter/subscriptions","organizations_url":"https://api.github.com/users/smarter/orgs","repos_url":"https://api.github.com/users/smarter/repos","events_url":"https://api.github.com/users/smarter/events{/privacy}","received_events_url":"https://api.github.com/users/smarter/received_events","type":"User","site_admin":false},"body":"Should probably be `apply(source.underlying.content)` to account for `ScriptSourceFile` although I'd be happier if we could get rid of the script source files suppot and let some external tool handle them.","created_at":"2016-03-12T21:04:39Z","updated_at":"2016-03-12T21:04:39Z","html_url":"https://github.com/lampepfl/dotty/pull/1154#discussion_r55927635","pull_request_url":"https://api.github.com/repos/lampepfl/dotty/pulls/1154","_links":{"self":{"href":"https://api.github.com/repos/lampepfl/dotty/pulls/comments/55927635"},"html":{"href":"https://github.com/lampepfl/dotty/pull/1154#discussion_r55927635"},"pull_request":{"href":"https://api.github.com/repos/lampepfl/dotty/pulls/1154"}}},"pull_request":{"url":"https://api.github.com/repos/lampepfl/dotty/pulls/1154","id":61940774,"html_url":"https://github.com/lampepfl/dotty/pull/1154","diff_url":"https://github.com/lampepfl/dotty/pull/1154.diff","patch_url":"https://github.com/lampepfl/dotty/pull/1154.patch","issue_url":"https://api.github.com/repos/lampepfl/dotty/issues/1154","number":1154,"state":"open","locked":false,"title":"First steps towards rewriting from Scala2 in dotty","user":{"login":"odersky","id":795990,"avatar_url":"https://avatars.githubusercontent.com/u/795990?v=3","gravatar_id":"","url":"https://api.github.com/users/odersky","html_url":"https://github.com/odersky","followers_url":"https://api.github.com/users/odersky/followers","following_url":"https://api.github.com/users/odersky/following{/other_user}","gists_url":"https://api.github.com/users/odersky/gists{/gist_id}","starred_url":"https://api.github.com/users/odersky/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/odersky/subscriptions","organizations_url":"https://api.github.com/users/odersky/orgs","repos_url":"https://api.github.com/users/odersky/repos","events_url":"https://api.github.com/users/odersky/events{/privacy}","received_events_url":"https://api.github.com/users/odersky/received_events","type":"User","site_admin":false},"body":"This is an attempt to do the rewriting right in the dotty compiler. The logic is, if dotty can detect\r\nScala2 incompatibilities it should be able to apply fixes at the same time. That way there is no error-prone syncing between different tools.\r\n\r\nThis PR\r\n\r\n - adds rewrite logic\r\n - adds logic to navigate from typed to untyped trees\r\n - performs specific rewritings: \r\n    - rewrite procedure syntax, \r\n    - add @volatile for lazy vals\r\n    - drop redundant trailing `_'.\r\n    - mask variance errors with @uncheckedVariance\r\n\r\nThere is also a change the way annotations are propagated to derived symbols.\r\n\r\nWith this PR, stdlib can be patched and recompiled without -language:Scala2 (pending fix of\r\n@volatile implicit issues).","created_at":"2016-03-07T16:22:20Z","updated_at":"2016-03-12T21:04:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"62232af61c9269297df9a1ec5dc4b9c1781c04bd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/lampepfl/dotty/pulls/1154/commits","review_comments_url":"https://api.github.com/repos/lampepfl/dotty/pulls/1154/comments","review_comment_url":"https://api.github.com/repos/lampepfl/dotty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lampepfl/dotty/issues/1154/comments","statuses_url":"https://api.github.com/repos/lampepfl/dotty/statuses/d30e213f85629f4767b1d3a3e9dd013f61d715c2","head":{"label":"dotty-staging:add-rewrite","ref":"add-rewrite","sha":"d30e213f85629f4767b1d3a3e9dd013f61d715c2","user":{"login":"dotty-staging","id":6998674,"avatar_url":"https://avatars.githubusercontent.com/u/6998674?v=3","gravatar_id":"","url":"https://api.github.com/users/dotty-staging","html_url":"https://github.com/dotty-staging","followers_url":"https://api.github.com/users/dotty-staging/followers","following_url":"https://api.github.com/users/dotty-staging/following{/other_user}","gists_url":"https://api.github.com/users/dotty-staging/gists{/gist_id}","starred_url":"https://api.github.com/users/dotty-staging/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotty-staging/subscriptions","organizations_url":"https://api.github.com/users/dotty-staging/orgs","repos_url":"https://api.github.com/users/dotty-staging/repos","events_url":"https://api.github.com/users/dotty-staging/events{/privacy}","received_events_url":"https://api.github.com/users/dotty-staging/received_events","type":"Organization","site_admin":false},"repo":{"id":17904384,"name":"dotty","full_name":"dotty-staging/dotty","owner":{"login":"dotty-staging","id":6998674,"avatar_url":"https://avatars.githubusercontent.com/u/6998674?v=3","gravatar_id":"","url":"https://api.github.com/users/dotty-staging","html_url":"https://github.com/dotty-staging","followers_url":"https://api.github.com/users/dotty-staging/followers","following_url":"https://api.github.com/users/dotty-staging/following{/other_user}","gists_url":"https://api.github.com/users/dotty-staging/gists{/gist_id}","starred_url":"https://api.github.com/users/dotty-staging/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotty-staging/subscriptions","organizations_url":"https://api.github.com/users/dotty-staging/orgs","repos_url":"https://api.github.com/users/dotty-staging/repos","events_url":"https://api.github.com/users/dotty-staging/events{/privacy}","received_events_url":"https://api.github.com/users/dotty-staging/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotty-staging/dotty","description":"Research platform for new language concepts and compiler technologies for Scala.","fork":true,"url":"https://api.github.com/repos/dotty-staging/dotty","forks_url":"https://api.github.com/repos/dotty-staging/dotty/forks","keys_url":"https://api.github.com/repos/dotty-staging/dotty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotty-staging/dotty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotty-staging/dotty/teams","hooks_url":"https://api.github.com/repos/dotty-staging/dotty/hooks","issue_events_url":"https://api.github.com/repos/dotty-staging/dotty/issues/events{/number}","events_url":"https://api.github.com/repos/dotty-staging/dotty/events","assignees_url":"https://api.github.com/repos/dotty-staging/dotty/assignees{/user}","branches_url":"https://api.github.com/repos/dotty-staging/dotty/branches{/branch}","tags_url":"https://api.github.com/repos/dotty-staging/dotty/tags","blobs_url":"https://api.github.com/repos/dotty-staging/dotty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotty-staging/dotty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotty-staging/dotty/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotty-staging/dotty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotty-staging/dotty/statuses/{sha}","languages_url":"https://api.github.com/repos/dotty-staging/dotty/languages","stargazers_url":"https://api.github.com/repos/dotty-staging/dotty/stargazers","contributors_url":"https://api.github.com/repos/dotty-staging/dotty/contributors","subscribers_url":"https://api.github.com/repos/dotty-staging/dotty/subscribers","subscription_url":"https://api.github.com/repos/dotty-staging/dotty/subscription","commits_url":"https://api.github.com/repos/dotty-staging/dotty/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotty-staging/dotty/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotty-staging/dotty/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotty-staging/dotty/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotty-staging/dotty/contents/{+path}","compare_url":"https://api.github.com/repos/dotty-staging/dotty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotty-staging/dotty/merges","archive_url":"https://api.github.com/repos/dotty-staging/dotty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotty-staging/dotty/downloads","issues_url":"https://api.github.com/repos/dotty-staging/dotty/issues{/number}","pulls_url":"https://api.github.com/repos/dotty-staging/dotty/pulls{/number}","milestones_url":"https://api.github.com/repos/dotty-staging/dotty/milestones{/number}","notifications_url":"https://api.github.com/repos/dotty-staging/dotty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotty-staging/dotty/labels{/name}","releases_url":"https://api.github.com/repos/dotty-staging/dotty/releases{/id}","deployments_url":"https://api.github.com/repos/dotty-staging/dotty/deployments","created_at":"2014-03-19T13:06:15Z","updated_at":"2015-11-30T13:03:19Z","pushed_at":"2016-03-12T13:32:17Z","git_url":"git://github.com/dotty-staging/dotty.git","ssh_url":"git@github.com:dotty-staging/dotty.git","clone_url":"https://github.com/dotty-staging/dotty.git","svn_url":"https://github.com/dotty-staging/dotty","homepage":"","size":17476,"stargazers_count":3,"watchers_count":3,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":3,"default_branch":"master"}},"base":{"label":"lampepfl:master","ref":"master","sha":"560810e25c77a5efd6212b71f29d2626aa21e9af","user":{"login":"lampepfl","id":2684793,"avatar_url":"https://avatars.githubusercontent.com/u/2684793?v=3","gravatar_id":"","url":"https://api.github.com/users/lampepfl","html_url":"https://github.com/lampepfl","followers_url":"https://api.github.com/users/lampepfl/followers","following_url":"https://api.github.com/users/lampepfl/following{/other_user}","gists_url":"https://api.github.com/users/lampepfl/gists{/gist_id}","starred_url":"https://api.github.com/users/lampepfl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lampepfl/subscriptions","organizations_url":"https://api.github.com/users/lampepfl/orgs","repos_url":"https://api.github.com/users/lampepfl/repos","events_url":"https://api.github.com/users/lampepfl/events{/privacy}","received_events_url":"https://api.github.com/users/lampepfl/received_events","type":"Organization","site_admin":false},"repo":{"id":7035651,"name":"dotty","full_name":"lampepfl/dotty","owner":{"login":"lampepfl","id":2684793,"avatar_url":"https://avatars.githubusercontent.com/u/2684793?v=3","gravatar_id":"","url":"https://api.github.com/users/lampepfl","html_url":"https://github.com/lampepfl","followers_url":"https://api.github.com/users/lampepfl/followers","following_url":"https://api.github.com/users/lampepfl/following{/other_user}","gists_url":"https://api.github.com/users/lampepfl/gists{/gist_id}","starred_url":"https://api.github.com/users/lampepfl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lampepfl/subscriptions","organizations_url":"https://api.github.com/users/lampepfl/orgs","repos_url":"https://api.github.com/users/lampepfl/repos","events_url":"https://api.github.com/users/lampepfl/events{/privacy}","received_events_url":"https://api.github.com/users/lampepfl/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lampepfl/dotty","description":"Research platform for new language concepts and compiler technologies for Scala.","fork":false,"url":"https://api.github.com/repos/lampepfl/dotty","forks_url":"https://api.github.com/repos/lampepfl/dotty/forks","keys_url":"https://api.github.com/repos/lampepfl/dotty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lampepfl/dotty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lampepfl/dotty/teams","hooks_url":"https://api.github.com/repos/lampepfl/dotty/hooks","issue_events_url":"https://api.github.com/repos/lampepfl/dotty/issues/events{/number}","events_url":"https://api.github.com/repos/lampepfl/dotty/events","assignees_url":"https://api.github.com/repos/lampepfl/dotty/assignees{/user}","branches_url":"https://api.github.com/repos/lampepfl/dotty/branches{/branch}","tags_url":"https://api.github.com/repos/lampepfl/dotty/tags","blobs_url":"https://api.github.com/repos/lampepfl/dotty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lampepfl/dotty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lampepfl/dotty/git/refs{/sha}","trees_url":"https://api.github.com/repos/lampepfl/dotty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lampepfl/dotty/statuses/{sha}","languages_url":"https://api.github.com/repos/lampepfl/dotty/languages","stargazers_url":"https://api.github.com/repos/lampepfl/dotty/stargazers","contributors_url":"https://api.github.com/repos/lampepfl/dotty/contributors","subscribers_url":"https://api.github.com/repos/lampepfl/dotty/subscribers","subscription_url":"https://api.github.com/repos/lampepfl/dotty/subscription","commits_url":"https://api.github.com/repos/lampepfl/dotty/commits{/sha}","git_commits_url":"https://api.github.com/repos/lampepfl/dotty/git/commits{/sha}","comments_url":"https://api.github.com/repos/lampepfl/dotty/comments{/number}","issue_comment_url":"https://api.github.com/repos/lampepfl/dotty/issues/comments{/number}","contents_url":"https://api.github.com/repos/lampepfl/dotty/contents/{+path}","compare_url":"https://api.github.com/repos/lampepfl/dotty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lampepfl/dotty/merges","archive_url":"https://api.github.com/repos/lampepfl/dotty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lampepfl/dotty/downloads","issues_url":"https://api.github.com/repos/lampepfl/dotty/issues{/number}","pulls_url":"https://api.github.com/repos/lampepfl/dotty/pulls{/number}","milestones_url":"https://api.github.com/repos/lampepfl/dotty/milestones{/number}","notifications_url":"https://api.github.com/repos/lampepfl/dotty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lampepfl/dotty/labels{/name}","releases_url":"https://api.github.com/repos/lampepfl/dotty/releases{/id}","deployments_url":"https://api.github.com/repos/lampepfl/dotty/deployments","created_at":"2012-12-06T12:57:33Z","updated_at":"2016-03-12T16:14:05Z","pushed_at":"2016-03-12T13:32:17Z","git_url":"git://github.com/lampepfl/dotty.git","ssh_url":"git@github.com:lampepfl/dotty.git","clone_url":"https://github.com/lampepfl/dotty.git","svn_url":"https://github.com/lampepfl/dotty","homepage":"","size":23422,"stargazers_count":809,"watchers_count":809,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":108,"mirror_url":null,"open_issues_count":139,"forks":108,"open_issues":139,"watchers":809,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lampepfl/dotty/pulls/1154"},"html":{"href":"https://github.com/lampepfl/dotty/pull/1154"},"issue":{"href":"https://api.github.com/repos/lampepfl/dotty/issues/1154"},"comments":{"href":"https://api.github.com/repos/lampepfl/dotty/issues/1154/comments"},"review_comments":{"href":"https://api.github.com/repos/lampepfl/dotty/pulls/1154/comments"},"review_comment":{"href":"https://api.github.com/repos/lampepfl/dotty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lampepfl/dotty/pulls/1154/commits"},"statuses":{"href":"https://api.github.com/repos/lampepfl/dotty/statuses/d30e213f85629f4767b1d3a3e9dd013f61d715c2"}}}}