Content-Length: 781960 | pFad | http://github.com/scala/scala/commit/4867f4512429772a15c5e3790a70ad833bd15887

30 Merge pull request #10846 from som-snytt/issue/i20006-mitigate · scala/scala@4867f45 · GitHub
Skip to content

Commit 4867f45

Browse files
authored
Merge pull request #10846 from som-snytt/issue/i20006-mitigate
-Xsource-features:double-definitions
2 parents 07b6efa + 25c521f commit 4867f45

File tree

9 files changed

+384
-101
lines changed

9 files changed

+384
-101
lines changed

src/compiler/scala/tools/nsc/Global.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ class Global(var currentSettings: Settings, reporter0: Reporter)
11951195
def leadingInfix = isScala3 && contains(o.leadingInfix)
11961196
def packagePrefixImplicits = isScala3 && contains(o.packagePrefixImplicits)
11971197
def implicitResolution = isScala3 && contains(o.implicitResolution) || settings.Yscala3ImplicitResolution.value
1198+
def doubleDefinitions = isScala3 && contains(o.doubleDefinitions)
11981199
}
11991200

12001201
// used in sbt

src/compiler/scala/tools/nsc/settings/ScalaSettings.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ trait ScalaSettings extends StandardScalaSettings with Warnings { _: MutableSett
181181
val leadingInfix = Choice("leading-infix", "Leading infix operators continue the previous line.")
182182
val packagePrefixImplicits = Choice("package-prefix-implicits", "The package prefix p is no longer part of the implicit search scope for type p.A.")
183183
val implicitResolution = Choice("implicit-resolution", "Use Scala-3-style downwards comparisons for implicit search and overloading resolution (see github.com/scala/scala/pull/6037).")
184+
val doubleDefinitions = Choice("double-definitions", "Correctly disallow double definitions differing in empty parens.")
184185

185186
val v13_13_choices = List(caseApplyCopyAccess, caseCompanionFunction, inferOverride, any2StringAdd, unicodeEscapesRaw, stringContextScope, leadingInfix, packagePrefixImplicits)
186187

@@ -190,11 +191,16 @@ trait ScalaSettings extends StandardScalaSettings with Warnings { _: MutableSett
190191
expandsTo = v13_13_choices)
191192

192193
val v13_14_choices = implicitResolution :: v13_13_choices
193-
194194
val v13_14 = Choice(
195195
"v2.13.14",
196196
"v2.13.13 plus implicit-resolution",
197197
expandsTo = v13_14_choices)
198+
199+
val v13_15_choices = doubleDefinitions :: v13_14_choices
200+
val v13_15 = Choice(
201+
"v2.13.15",
202+
"v2.13.14 plus double-definitions",
203+
expandsTo = v13_15_choices)
198204
}
199205
val XsourceFeatures = MultiChoiceSetting(
200206
name = "-Xsource-features",

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3438,7 +3438,12 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
34383438
}
34393439
nn(sym) && nn(sym1)
34403440
}
3441-
val conflicted = inBlock || (!sym.isMethod && !sym1.isMethod) || nullaryNilary || sym.tpe.matches(sym1.tpe)
3441+
def correctly: Boolean = nullaryNilary.tap(if (_) reportCorrection()) && currentRun.sourceFeatures.doubleDefinitions
3442+
def reportCorrection(): Unit =
3443+
if (currentRun.isScala3 && !currentRun.sourceFeatures.doubleDefinitions)
3444+
context.warning(sym.pos, s"Double definition will be detected in Scala 3; the conflicting $sym1 is defined at ${sym1.pos.line}:${sym1.pos.column}", Scala3Migration)
3445+
3446+
val conflicted = inBlock || (!sym.isMethod && !sym1.isMethod) || sym.tpe.matches(sym1.tpe) || correctly
34423447

34433448
// default getters are defined twice when multiple overloads have defaults.
34443449
// The error for this is deferred until RefChecks.checkDefaultsInOverloaded

test/files/neg/i20006.check

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,10 @@ i20006.scala:7: error: method hasNext is defined twice;
66
the conflicting value hasNext was defined at line 5:7
77
override def hasNext: Boolean = first || hasNext(acc) // error
88
^
9-
i20006.scala:22: error: method next is defined twice;
10-
the conflicting value next was defined at line 18:65
11-
override def next(): T = { // error
12-
^
139
i20006.scala:21: error: method hasNext is defined twice;
1410
the conflicting value hasNext was defined at line 18:42
1511
override def hasNext: Boolean = first || hasNext(acc) // error
1612
^
17-
i20006.scala:36: error: method next is defined twice;
18-
the conflicting value next was defined at line 32:65
19-
def next(): T = { // error
20-
^
2113
i20006.scala:35: error: method hasNext is defined twice;
2214
the conflicting value hasNext was defined at line 32:42
2315
def hasNext: Boolean = first || hasNext(acc) // error
@@ -32,16 +24,11 @@ i20006.scala:50: error: variable x is defined twice;
3224
i20006.scala:53: error: x is already defined as value x
3325
private[this] var x: Int = 42 // error
3426
^
35-
i20006.scala:56: error: method x is defined twice;
36-
the conflicting value x was defined at line 55:9
37-
def x(): Int = 42 // error
38-
^
39-
i20006.scala:63: error: method x is defined twice;
40-
the conflicting value x was defined at line 62:21
41-
def x(): Int = 42 // error
42-
^
4327
i20006.scala:67: error: method x is defined twice;
4428
the conflicting method x was defined at line 66:21
4529
def x(): Int = 42 // error
4630
^
47-
12 errors
31+
i20006.scala:77: error: x is already defined as variable x
32+
def x(): Int = x // error
33+
^
34+
9 errors

test/files/neg/i20006.scala

Lines changed: 15 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class YIterateIterator[T](seed: T, hasNext: T => Boolean, next: T => T) ex
1919
private var first = true
2020
private var acc = seed
2121
override def hasNext: Boolean = first || hasNext(acc) // error
22-
override def next(): T = { // error
22+
override def next(): T = { // noerror
2323
if (first) {
2424
first = false
2525
} else {
@@ -33,7 +33,7 @@ final class ZIterateIterator[T](seed: T, hasNext: T => Boolean, next: T => T) {
3333
private var first = true
3434
private var acc = seed
3535
def hasNext: Boolean = first || hasNext(acc) // error
36-
def next(): T = { // error
36+
def next(): T = { // noerror
3737
if (first) {
3838
first = false
3939
} else {
@@ -53,94 +53,27 @@ class E(x: String) {
5353
private[this] var x: Int = 42 // error
5454
}
5555
class F(x: String) {
56-
def x(): Int = 42 // error
56+
def x(): Int = 42 // noerror
5757
}
5858
class G(x: String) {
5959
def x(i: Int): Int = i
6060
}
6161
class H {
6262
private[this] val x: String = ""
63-
def x(): Int = 42 // error
63+
def x(): Int = 42 // noerror
6464
}
6565
class I {
6666
private[this] def x: String = ""
6767
def x(): Int = 42 // error
6868
}
69-
70-
/*
71-
-- [E120] Naming Error: test/files/neg/i20006.scala:8:15 ---------------------------------------------------------------
72-
8 | override def hasNext: Boolean = first || hasNext(acc)
73-
| ^
74-
| Double definition:
75-
| val hasNext: T => Boolean in class XIterateIterator at line 6 and
76-
| override def hasNext: Boolean in class XIterateIterator at line 8
77-
-- [E120] Naming Error: test/files/neg/i20006.scala:9:15 ---------------------------------------------------------------
78-
9 | override def next(): T = {
79-
| ^
80-
| Double definition:
81-
| val next: T => T in class XIterateIterator at line 7 and
82-
| override def next(): T in class XIterateIterator at line 9
83-
-- [E120] Naming Error: test/files/neg/i20006.scala:22:15 --------------------------------------------------------------
84-
22 | override def hasNext: Boolean = first || hasNext(acc)
85-
| ^
86-
| Double definition:
87-
| private[this] val hasNext: T => Boolean in class YIterateIterator at line 19 and
88-
| override def hasNext: Boolean in class YIterateIterator at line 22
89-
-- [E120] Naming Error: test/files/neg/i20006.scala:23:15 --------------------------------------------------------------
90-
23 | override def next(): T = {
91-
| ^
92-
| Double definition:
93-
| private[this] val next: T => T in class YIterateIterator at line 19 and
94-
| override def next(): T in class YIterateIterator at line 23
95-
-- [E120] Naming Error: test/files/neg/i20006.scala:36:6 ---------------------------------------------------------------
96-
36 | def hasNext: Boolean = first || hasNext(acc)
97-
| ^
98-
| Double definition:
99-
| private[this] val hasNext: T => Boolean in class ZIterateIterator at line 33 and
100-
| def hasNext: Boolean in class ZIterateIterator at line 36
101-
-- [E120] Naming Error: test/files/neg/i20006.scala:37:6 ---------------------------------------------------------------
102-
37 | def next(): T = {
103-
| ^
104-
| Double definition:
105-
| private[this] val next: T => T in class ZIterateIterator at line 33 and
106-
| def next(): T in class ZIterateIterator at line 37
107-
-- [E120] Naming Error: test/files/neg/i20006.scala:48:6 ---------------------------------------------------------------
108-
48 | val x: String = "member" // error
109-
| ^
110-
| Double definition:
111-
| private[this] val x: String in class C at line 47 and
112-
| val x: String in class C at line 48
113-
-- [E120] Naming Error: test/files/neg/i20006.scala:51:14 --------------------------------------------------------------
114-
51 | private var x: Int = 42 // error
115-
| ^
116-
| Double definition:
117-
| private[this] val x: String in class D at line 50 and
118-
| private[this] var x: Int in class D at line 51
119-
-- [E120] Naming Error: test/files/neg/i20006.scala:54:20 --------------------------------------------------------------
120-
54 | private[this] var x: Int = 42 // error
121-
| ^
122-
| Double definition:
123-
| private[this] val x: String in class E at line 53 and
124-
| private[this] var x: Int in class E at line 54
125-
-- [E120] Naming Error: test/files/neg/i20006.scala:57:6 ---------------------------------------------------------------
126-
57 | def x(): Int = 42 // error
127-
| ^
128-
| Double definition:
129-
| private[this] val x: String in class F at line 56 and
130-
| def x(): Int in class F at line 57
131-
-- [E120] Naming Error: test/files/neg/i20006.scala:65:6 ---------------------------------------------------------------
132-
65 | def x(): Int = 42
133-
| ^
134-
| Double definition:
135-
| val x: String in class H at line 63 and
136-
| def x(): Int in class H at line 65
137-
-- Warning: test/files/neg/i20006.scala:54:16 --------------------------------------------------------------------------
138-
54 | private[this] var x: Int = 42 // error
139-
| ^
140-
| Ignoring [this] qualifier.
141-
| This syntax will be deprecated in the future; it should be dropped.
142-
| See: https://docs.scala-lang.org/scala3/reference/dropped-features/this-qualifier.html
143-
| This construct can be rewritten automatically under -rewrite -source 3.4-migration.
144-
1 warning found
145-
11 errors found
146-
*/
69+
class PrivateConflict {
70+
private[this] var x = 42
71+
def x(): Int = x
72+
def x_=(n: Int) = x = n
73+
}
74+
class LocalConflict {
75+
def f(): Unit = {
76+
var x = 42
77+
def x(): Int = x // error
78+
}
79+
}

test/files/neg/i20006b.check

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
i20006b.scala:9: error: method next is defined twice;
2+
the conflicting value next was defined at line 7:7
3+
override def next(): T = { // error
4+
^
5+
i20006b.scala:8: error: method hasNext is defined twice;
6+
the conflicting value hasNext was defined at line 6:7
7+
override def hasNext: Boolean = first || hasNext(acc) // error
8+
^
9+
i20006b.scala:22: error: method hasNext is defined twice;
10+
the conflicting value hasNext was defined at line 19:42
11+
override def hasNext: Boolean = first || hasNext(acc) // error
12+
^
13+
i20006b.scala:36: error: method hasNext is defined twice;
14+
the conflicting value hasNext was defined at line 33:42
15+
def hasNext: Boolean = first || hasNext(acc) // error
16+
^
17+
i20006b.scala:48: error: x is already defined as value x
18+
val x: String = "member" // error
19+
^
20+
i20006b.scala:51: error: variable x is defined twice;
21+
the conflicting value x was defined at line 50:9
22+
private var x: Int = 42 // error
23+
^
24+
i20006b.scala:54: error: x is already defined as value x
25+
private[this] var x: Int = 42 // error
26+
^
27+
i20006b.scala:68: error: method x is defined twice;
28+
the conflicting method x was defined at line 67:21
29+
def x(): Int = 42 // error
30+
^
31+
i20006b.scala:78: error: x is already defined as variable x
32+
def x(): Int = x // error
33+
^
34+
i20006b.scala:23: error: Double definition will be detected in Scala 3; the conflicting value next is defined at 19:65
35+
Scala 3 migration messages are issued as errors under -Xsource:3. Use -Wconf or @nowarn to demote them to warnings or suppress.
36+
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=YIterateIterator
37+
override def next(): T = { // werror
38+
^
39+
i20006b.scala:37: error: Double definition will be detected in Scala 3; the conflicting value next is defined at 33:65
40+
Scala 3 migration messages are issued as errors under -Xsource:3. Use -Wconf or @nowarn to demote them to warnings or suppress.
41+
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=ZIterateIterator
42+
def next(): T = { // werror
43+
^
44+
i20006b.scala:57: error: Double definition will be detected in Scala 3; the conflicting value x is defined at 56:9
45+
Scala 3 migration messages are issued as errors under -Xsource:3. Use -Wconf or @nowarn to demote them to warnings or suppress.
46+
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=F
47+
def x(): Int = 42 // werror
48+
^
49+
i20006b.scala:64: error: Double definition will be detected in Scala 3; the conflicting value x is defined at 63:21
50+
Scala 3 migration messages are issued as errors under -Xsource:3. Use -Wconf or @nowarn to demote them to warnings or suppress.
51+
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=H
52+
def x(): Int = 42 // werror
53+
^
54+
i20006b.scala:72: error: Double definition will be detected in Scala 3; the conflicting variable x is defined at 71:21
55+
Scala 3 migration messages are issued as errors under -Xsource:3. Use -Wconf or @nowarn to demote them to warnings or suppress.
56+
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=PrivateConflict
57+
def x(): Int = x // werror
58+
^
59+
14 errors

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/scala/scala/commit/4867f4512429772a15c5e3790a70ad833bd15887

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy