Skip to content

Commit af21baf

Browse files
Martijn HoekstraMartijn Hoekstra
authored andcommitted
honor -encoding compiler flag and defaults
1 parent 76f5ff3 commit af21baf

17 files changed

+73
-35
lines changed

.gitattributes

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# These files are text and should be normalized (convert crlf => lf)
2-
*.c text
3-
*.check text
4-
*.css text
5-
*.html text
6-
*.java text
7-
*.js text
8-
*.sbt text
9-
*.scala text
10-
*.sh text
11-
*.txt text
12-
*.xml text
2+
3+
*.c text eol=lf
4+
*.check text eol=lf
5+
*.css text eol=lf
6+
*.html text eol=lf
7+
*.java text eol=lf
8+
*.js text eol=lf
9+
*.sbt text eol=lf
10+
*.scala text eol=lf
11+
*.sh text eol=lf
12+
*.txt text eol=lf
13+
*.xml text eol=lf
1314

1415
# Windows-specific files get windows endings
1516
*.bat eol=crlf

dottydoc/test/BaseTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import model.Package
1212
trait DottyTest {
1313
dotty.tools.dotc.parsing.Scanners // initialize keywords
1414

15-
implicit var ctx: FreshContext = {
15+
implicit val ctx: FreshContext = {
1616
val base = new ContextBase
1717
import base.settings._
1818
val ctx = base.initialCtx.fresh

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object DottyBuild extends Build {
4040
homepage in Global := Some(url("https://github.com/lampepfl/dotty")),
4141

4242
// scalac options
43-
scalacOptions in Global ++= Seq("-feature", "-deprecation", "-language:existentials,higherKinds,implicitConversions"),
43+
scalacOptions in Global ++= Seq("-feature", "-deprecation", "-encoding", "UTF8", "-language:existentials,higherKinds,implicitConversions"),
4444

4545
javacOptions in Global ++= Seq("-Xlint:unchecked", "-Xlint:deprecation")
4646
)

src/dotty/tools/dotc/Run.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Phases._
99
import Decorators._
1010
import dotty.tools.dotc.transform.TreeTransforms.TreeTransformer
1111
import io.PlainFile
12+
import scala.io.Codec
1213
import util._
1314
import reporting.Reporter
1415
import transform.TreeChecker
@@ -28,8 +29,9 @@ class Run(comp: Compiler)(implicit ctx: Context) {
2829
var units: List[CompilationUnit] = _
2930

3031
def getSource(fileName: String): SourceFile = {
32+
val encoding = ctx.settings.encoding.value
3133
val f = new PlainFile(fileName)
32-
if (f.exists) new SourceFile(f)
34+
if (f.exists) new SourceFile(f, Codec(encoding))
3335
else {
3436
ctx.error(s"not found: $fileName")
3537
NoSource
@@ -113,7 +115,7 @@ class Run(comp: Compiler)(implicit ctx: Context) {
113115
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) // buffering is still advised by javadoc
114116
writer.write(sourceCode)
115117
writer.close()
116-
compileSources(List(new SourceFile(virtualFile)))
118+
compileSources(List(new SourceFile(virtualFile, Codec.UTF8)))
117119
}
118120

119121
/** The context created for this run */

src/dotty/tools/dotc/config/Settings.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ object Settings {
2525
private var values = ArrayBuffer(initialValues: _*)
2626
private var _wasRead: Boolean = false
2727

28+
override def toString = s"SettingsState(values: ${values.toList})"
29+
2830
def value(idx: Int): Any = {
2931
_wasRead = true
3032
values(idx)

src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import java.io.IOException
1010
import Chars._
1111
import ScriptSourceFile._
1212
import Positions._
13+
import scala.io.Codec
1314

1415
import java.util.Optional
1516

@@ -36,9 +37,9 @@ object ScriptSourceFile {
3637

3738
case class SourceFile(file: AbstractFile, content: Array[Char]) extends interfaces.SourceFile {
3839

39-
def this(_file: AbstractFile) = this(_file, _file.toCharArray)
40-
def this(sourceName: String, cs: Seq[Char]) = this(new VirtualFile(sourceName), cs.toArray)
41-
def this(file: AbstractFile, cs: Seq[Char]) = this(file, cs.toArray)
40+
def this(_file: AbstractFile, codec: Codec) = this(_file, new String(_file.toByteArray, codec.charSet).toCharArray)
41+
def this(sourceName: String, cs: Seq[Char]) = this(new VirtualFile(sourceName), cs.toArray)
42+
def this(file: AbstractFile, cs: Seq[Char]) = this(file, cs.toArray)
4243

4344
/** Tab increment; can be overridden */
4445
def tabInc = 8

test/dotc/tests.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class tests extends CompilerTest {
2323
val defaultOutputDir = "./out/"
2424

2525
implicit val defaultOptions = noCheckOptions ++ List(
26-
"-Yno-deep-subtypes", "-Yno-double-bindings", "-Yforce-sbt-phases",
27-
"-d", defaultOutputDir) ++ {
26+
"-Yno-deep-subtypes", "-Yno-double-bindings", "-Yforce-sbt-phases", "-d", defaultOutputDir) ++ {
2827
if (isRunByJenkins) List("-Ycheck:tailrec,resolveSuper,mixin,restoreScopes,labelDef") // should be Ycheck:all, but #725
2928
else List("-Ycheck:tailrec,resolveSuper,mixin,restoreScopes,labelDef")
3029
}

test/test/CompilerTest.scala

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,21 +449,39 @@ abstract class CompilerTest {
449449
/** Recursively copy over source files and directories, excluding extensions
450450
* that aren't in extensionsToCopy. */
451451
private def recCopyFiles(sourceFile: Path, dest: Path): Unit = {
452-
processFileDir(sourceFile, { sf =>
453-
if (extensionsToCopy.contains(sf.extension)) {
454-
dest.parent.jfile.mkdirs
452+
453+
def copyfile(file: SFile, bytewise: Boolean): Unit = {
454+
if (bytewise) {
455+
val in = file.inputStream()
456+
val out = SFile(dest).outputStream()
457+
val buffer = new Array[Byte](1024)
458+
def loop(available: Int):Unit = {
459+
if (available < 0) {()}
460+
else {
461+
out.write(buffer, 0, available)
462+
val read = in.read(buffer)
463+
loop(read)
464+
}
465+
}
466+
loop(0)
467+
in.close()
468+
out.close()
469+
} else {
455470
try {
456-
scala.reflect.io.File(dest)(scala.io.Codec.UTF8).writeAll(s"/* !!!!! WARNING: DO NOT MODIFY. Original is at: $sf !!!!! */", sf.slurp("UTF-8"))
471+
SFile(dest)(scala.io.Codec.UTF8).writeAll(s"/* !!!!! WARNING: DO NOT MODIFY. Original is at: $file !!!!! */", file.slurp("UTF-8"))
457472
} catch {
458-
case unmappable: java.nio.charset.UnmappableCharacterException => {
459-
log(s"ERROR: reading file $sf failed to decode: $unmappable")
460-
throw unmappable
461-
}
462-
case t: Throwable => {
463-
log(s"ERROR: file $sf failed for some other reason: $t")
464-
throw t
465-
}
473+
case unmappable: java.nio.charset.UnmappableCharacterException =>
474+
copyfile(file, true) //there are bytes that can't be mapped with UTF-8. Bail and just do a straight byte-wise copy without the warning header.
475+
case unmappable: java.nio.charset.MalformedInputException =>
476+
copyfile(file, true) //there are bytes that can't be mapped with UTF-8. Bail and just do a straight byte-wise copy without the warning header.
466477
}
478+
}
479+
}
480+
481+
processFileDir(sourceFile, { sf =>
482+
if (extensionsToCopy.contains(sf.extension)) {
483+
dest.parent.jfile.mkdirs
484+
copyfile(sf, false)
467485
} else {
468486
log(s"WARNING: ignoring $sf")
469487
}

test/test/DottyTest.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class DottyTest /*extends ContextEscapeDetection*/ {
2323
import base.settings._
2424
val ctx = base.initialCtx.fresh
2525
base.initialize()(ctx)
26+
ctx.setSetting(ctx.settings.encoding, "UTF8")
2627
ctx
2728
}
2829
/*

test/test/ParserTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import scala.reflect.io._
44
import dotty.tools.dotc.util._
55
import dotty.tools.dotc.core._
66
import dotty.tools.dotc.parsing._
7+
import scala.io.Codec
78
import Tokens._, Parsers._
89
import dotty.tools.dotc.ast.untpd._
910
import org.junit.Test
@@ -23,7 +24,7 @@ class ParserTest extends DottyTest {
2324

2425
def parse(file: PlainFile): Tree = {
2526
//println("***** parsing " + file)
26-
val source = new SourceFile(file)
27+
val source = new SourceFile(file, Codec.UTF8)
2728
val parser = new Parser(source)
2829
val tree = parser.parse()
2930
parsed += 1

test/test/ScannerTest.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test
22

33
import scala.reflect.io._
4+
import scala.io.Codec
45
import dotty.tools.dotc.util._
56
import dotty.tools.dotc.parsing._
67
import Tokens._, Scanners._
@@ -16,8 +17,8 @@ class ScannerTest extends DottyTest {
1617
def scan(name: String): Unit = scan(new PlainFile(name))
1718

1819
def scan(file: PlainFile): Unit = {
19-
println("***** scanning " + file)
20-
val source = new SourceFile(file)
20+
//println("***** scanning " + file)
21+
val source = new SourceFile(file, Codec.UTF8)
2122
val scanner = new Scanner(source)
2223
var i = 0
2324
while (scanner.token != EOF) {

tests/run/utf16encoded.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

tests/run/utf16encoded.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-encoding UTF16

tests/run/utf16encoded.scala

352 Bytes
Binary file not shown.

tests/run/utf8encoded.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

tests/run/utf8encoded.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-encoding UTF8

tests/run/utf8encoded.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//this file is saved as UTF-8
2+
object UTF8Encoded {
3+
def main(args: Array[String]): Unit = {
4+
val testchar = '⇒'
5+
println(testchar == '\u21D2')
6+
}
7+
8+
}

0 commit comments

Comments
 (0)