From 2d001bfb0b01f5dc66acce621316bc094461a105 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Wed, 22 May 2019 16:14:16 +0200 Subject: [PATCH] Fix error message when overriding with a lazy val --- compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 2 +- compiler/test/dotty/tools/dotc/CompilationTests.scala | 1 + tests/neg-custom-args/ovlazy.check | 5 +++++ tests/neg-custom-args/ovlazy.scala | 6 ++++++ 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/neg-custom-args/ovlazy.check create mode 100644 tests/neg-custom-args/ovlazy.scala diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index 50ba767d7aec..40451db18130 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -388,7 +388,7 @@ object RefChecks { } else if (member.is(ModuleVal) && !other.isRealMethod && !other.is(Deferred | Lazy)) { overrideError("may not override a concrete non-lazy value") } else if (member.is(Lazy, butNot = Module) && !other.isRealMethod && !other.is(Lazy) && - !ctx.testScala2Mode("may not override a non-lazy value", member.sourcePos)) { + !ctx.testScala2Mode(overrideErrorMsg("may not override a non-lazy value"), member.sourcePos)) { overrideError("may not override a non-lazy value") } else if (other.is(Lazy) && !other.isRealMethod && !member.is(Lazy)) { overrideError("must be declared lazy to override a lazy value") diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 1eb150c655f5..9e32cc6a08b0 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -160,6 +160,7 @@ class CompilationTests extends ParallelTesting { compileFile("tests/neg-custom-args/implicit-conversions-old.scala", defaultOptions.and("-Xfatal-warnings", "-feature")), compileFile("tests/neg-custom-args/i3246.scala", scala2Mode), compileFile("tests/neg-custom-args/overrideClass.scala", scala2Mode), + compileFile("tests/neg-custom-args/ovlazy.scala", scala2Mode.and("-migration", "-Xfatal-warnings")), compileFile("tests/neg-custom-args/autoTuplingTest.scala", defaultOptions.and("-language:noAutoTupling")), compileFile("tests/neg-custom-args/nopredef.scala", defaultOptions.and("-Yno-predef")), compileFile("tests/neg-custom-args/noimports.scala", defaultOptions.and("-Yno-imports")), diff --git a/tests/neg-custom-args/ovlazy.check b/tests/neg-custom-args/ovlazy.check new file mode 100644 index 000000000000..9dab32c76bfb --- /dev/null +++ b/tests/neg-custom-args/ovlazy.check @@ -0,0 +1,5 @@ +-- Error: tests/neg-custom-args/ovlazy.scala:5:20 ---------------------------------------------------------------------- +5 | override lazy val x: Int = 2 // error + | ^ + | error overriding value x in class A of type Int; + | lazy value x of type Int may not override a non-lazy value diff --git a/tests/neg-custom-args/ovlazy.scala b/tests/neg-custom-args/ovlazy.scala new file mode 100644 index 000000000000..3a263e5ed4bf --- /dev/null +++ b/tests/neg-custom-args/ovlazy.scala @@ -0,0 +1,6 @@ +class A { + val x: Int = 1 +} +class B extends A { + override lazy val x: Int = 2 // error +}