From e3650c7495c1b94900bf3ab09895c5ea5a4aea98 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 15 Feb 2019 17:12:48 +0100 Subject: [PATCH 1/3] Fix #5004: Turn assertion into test --- compiler/src/dotty/tools/dotc/core/Contexts.scala | 1 - compiler/src/dotty/tools/dotc/typer/Applications.scala | 3 ++- tests/neg/i5004.scala | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i5004.scala diff --git a/compiler/src/dotty/tools/dotc/core/Contexts.scala b/compiler/src/dotty/tools/dotc/core/Contexts.scala index 842132270615..9fb22b758f75 100644 --- a/compiler/src/dotty/tools/dotc/core/Contexts.scala +++ b/compiler/src/dotty/tools/dotc/core/Contexts.scala @@ -378,7 +378,6 @@ object Contexts { * - as scope: The parameters of the auxiliary constructor. */ def thisCallArgContext: Context = { - assert(owner.isClassConstructor) val constrCtx = outersIterator.dropWhile(_.outer.owner == owner).next() superOrThisCallContext(owner, constrCtx.scope) .setTyperState(typerState) diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 5f3449a89080..460a9c21a8b5 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -747,7 +747,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic => * otherwise the current context. */ def argCtx(app: untpd.Tree)(implicit ctx: Context): Context = - if (untpd.isSelfConstrCall(app)) ctx.thisCallArgContext else ctx + if (ctx.owner.isClassConstructor && untpd.isSelfConstrCall(app)) ctx.thisCallArgContext + else ctx /** Typecheck application. Result could be an `Apply` node, * or, if application is an operator assignment, also an `Assign` or diff --git a/tests/neg/i5004.scala b/tests/neg/i5004.scala new file mode 100644 index 000000000000..16532bbec69e --- /dev/null +++ b/tests/neg/i5004.scala @@ -0,0 +1,6 @@ +object i0 { +1 match { +def this(): Int // error // error + def this() // error +} // error +} \ No newline at end of file From c496db782912eb405d177a7744f6ecad0b16bafc Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 15 Feb 2019 17:32:43 +0100 Subject: [PATCH 2/3] Exempt combinations of Java fields and methods as double defs Exempt combinations of Java fields and methods from being flagged as double definitions. Java fields don't get a getter, so the pattern int f int f() = f is OK (and quite common). --- compiler/src/dotty/tools/dotc/typer/Checking.scala | 5 ++++- tests/pos/i4739.java | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i4739.java diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 814459a672d6..226d40b6f875 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -744,7 +744,10 @@ trait Checking { def checkDecl(decl: Symbol): Unit = { for (other <- seen(decl.name)) { typr.println(i"conflict? $decl $other") - if (decl.matches(other)) { + def javaFieldMethodPair = + decl.is(JavaDefined) && other.is(JavaDefined) && + decl.is(Method) != other.is(Method) + if (decl.matches(other) && !javaFieldMethodPair) { def doubleDefError(decl: Symbol, other: Symbol): Unit = if (!decl.info.isErroneous && !other.info.isErroneous) ctx.error(DoubleDeclaration(decl, other), decl.sourcePos) diff --git a/tests/pos/i4739.java b/tests/pos/i4739.java new file mode 100644 index 000000000000..a959eec4e2d1 --- /dev/null +++ b/tests/pos/i4739.java @@ -0,0 +1,6 @@ +public class TestJava { + int foo; + int foo() { + return this.foo; + } +} \ No newline at end of file From 5e9f086eaa13b53337659dbc10b6344f267976bc Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 15 Feb 2019 19:00:54 +0100 Subject: [PATCH 3/3] Fix test --- tests/pos/i4739.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pos/i4739.java b/tests/pos/i4739.java index a959eec4e2d1..6daa9ff113d5 100644 --- a/tests/pos/i4739.java +++ b/tests/pos/i4739.java @@ -1,4 +1,4 @@ -public class TestJava { +public class i4739 { int foo; int foo() { return this.foo;