-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathi15503a.scala
462 lines (389 loc) · 9.14 KB
/
i15503a.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//> using options -Wunused:imports -Wconf:origin=Suppressed.*:s
object FooUnused:
import collection.mutable.Set // warn
import collection.mutable.{Map => MutMap} // warn
import collection.mutable._ // warn
object FooWildcardUnused:
import collection.mutable._ // warn
object Foo:
import collection.mutable.Set // OK
import collection.mutable.{Map => MutMap} // OK
val bar = Set() // OK
val baz = MutMap() // OK
object FooWildcard:
import collection.mutable._ // OK
val bar = Set() // OK
object FooNestedUnused:
import collection.mutable.Set // warn
object Nested:
def hello = 1
object FooNested:
import collection.mutable.Set // OK
object Nested:
def hello = Set()
object FooGivenUnused:
import SomeGivenImports.given // warn
object FooGiven:
import SomeGivenImports.given // OK
import SomeGivenImports._ // warn
val foo = summon[Int]
/**
* Import used as type name are considered
* as used.
*
* Import here are only used as types, not as
* Term
*/
object FooTypeName:
import collection.mutable.Set // OK
import collection.mutable.Map // OK
import collection.mutable.Seq // OK
import collection.mutable.ArrayBuilder // OK
import collection.mutable.ListBuffer // warn
def checkImplicit[A](using Set[A]) = ()
def checkParamType[B](a: Map[B,B]): Seq[B] = ???
def checkTypeParam[A] = ()
checkTypeParam[ArrayBuilder[Int]]
object InlineChecks:
object InlineFoo:
import collection.mutable.Set // ok
import collection.mutable.Map // warn
inline def getSet = Set(1)
object InlinedBar:
import collection.mutable.Set // warn (don't be fooled by inline expansion)
import collection.mutable.Map // warn
val a = InlineFoo.getSet
object MacroChecks:
object StringInterpol:
import collection.mutable.Set // OK
import collection.mutable.Map // OK
println(s"This is a mutableSet : ${Set[Map[Int,Int]]()}")
object InnerMostCheck:
import collection.mutable.* // warn
def check =
import collection.mutable.* //OK
val a = Set(1)
object IgnoreExclusion:
import collection.mutable.{Set => _} // OK
import collection.mutable.{Map => _} // OK
import collection.mutable.{ListBuffer} // warn
def check =
val a = Set(1)
val b = Map(1 -> 2)
/**
* Some given values for the test
*/
object SomeGivenImports:
given Int = 0
given String = "foo"
/* BEGIN : Check on packages*/
package nestedpackageimport:
package p:
class C
package p:
package q:
import p.* // warn
class U:
def f = new C
package unnestedpackageimport:
package p:
class C
package p.q:
import p.* // nowarn
class U:
def f = new C
package redundancy:
object redundant:
def f = 42
import redundancy.* // warn superseded by def in scope
class R:
def g = redundant.f
package testpackageimport:
package a:
val x: Int = 0
package b:
import a._ // warn
/* END : Check on packages*/
/* BEGIN : tests on meta-language features */
object TestGivenCoversionScala2:
/* note: scala3 Conversion[U,T] do not require an import */
import language.implicitConversions // OK
implicit def doubleToInt(d:Double):Int = d.toInt
def idInt(i:Int):Int = i
val someInt = idInt(1.0)
object TestTailrecImport:
import annotation.tailrec // OK
@tailrec
def fac(x:Int, acc:Int = 1): Int =
if x == 0 then acc else fac(x - 1, acc * x)
/* END : tests on meta-language features */
/* BEGIN : tests on given import order */
object GivenImportOrderAtoB:
class X
class Y extends X
object A { implicit val x: X = new X }
object B { implicit val y: Y = new Y }
class C {
import A._ // warn
import B._ // OK
def t = implicitly[X]
}
object GivenImportOrderBtoA:
class X
class Y extends X
object A { implicit val x: X = new X }
object B { implicit val y: Y = new Y }
class C {
import B._ // OK
import A._ // warn
def t = implicitly[X]
}
/* END : tests on given import order */
/* Scala 2 implicits */
object Scala2ImplicitsGiven:
object A:
implicit val x: Int = 1
object B:
import A.given // OK
val b = summon[Int]
object C:
import A.given // warn
val b = 1
object D:
import A._ // OK
val b = summon[Int]
object E:
import A._ // warn
val b = 1
object F:
import A.x // OK
val b = summon[Int]
object G:
import A.x // warn
val b = 1
// -------------------------------------
object TestNewKeyword:
object Foo:
class Aa[T](val x: T)
object Bar:
import Foo.Aa // OK
val v = 1
val a = new Aa(v)
// -------------------------------------
object testAnnotatedType:
import annotation.switch // OK
val a = (??? : @switch) match
case _ => ???
//-------------------------------------
package testImportsInImports:
package a:
package b:
val x = 1
package c:
import a.b // OK
import b.x // OK
import b.x as z // OK
val y = x + z
//-------------------------------------
package testOnOverloadedMethodsImports:
package a:
trait A
trait B
trait C:
def foo(x: A):A = ???
def foo(x: B):B = ???
package b:
object D extends a.C
package c:
import b.D.foo // warn
package d:
import b.D.foo // OK
def bar = foo((??? : a.A))
package e:
import b.D.foo // OK
def bar = foo((??? : a.B))
package f:
import b.D.foo // OK
def bar = foo((??? : a.A))
def baz = foo((??? : a.B))
//-------------------------------------
package foo.testing.rename.imports:
import collection.mutable.{Set => MutSet1} // OK
import collection.mutable.{Set => MutSet2} // OK
import collection.mutable.{Set => MutSet3} // warn
type A[X] = MutSet1[X]
val a = MutSet2(1)
//-------------------------------------
package foo.testing.imports.precedence:
import scala.collection.immutable.{BitSet => _, _} // warn
import scala.collection.immutable.BitSet // OK
def t = BitSet.empty
package foo.test.enums:
enum A: // OK
case B extends A // OK
case C extends A // OK
package foo.test.typeapply.hklamdba.i16680:
package foo:
trait IO[A]
package bar:
import foo.IO // OK
def f[F[_]]: String = "hello"
def go = f[IO]
object Selections:
def f(list: List[Int]): Int =
import list.{head => first} // OK
first
def f2(list: List[Int]): Int =
import list.head // OK
head
def f3(list: List[Int]): Int =
import list.head // warn
list.head
object N:
val ns: List[Int] = Nil
def g(): Int =
import N.ns // OK
ns.head
end Selections
object `more nestings`:
object Outer:
object Inner:
val thing = 42
def j() =
import Inner.thing // warn
thing
def k() =
import Inner.thing // warn
Inner.thing
object Thing:
object Inner:
val thing = 42
import Inner.thing // warn
def j() =
thing
def k() =
Inner.thing
object Suppressed:
val suppressed = 42
object Suppressing:
import Suppressed.* // no warn, see options
def f = 42
package i22692:
import javax.swing.*
import javax.swing.event as swingEvent // no warn, regression test for warning in 3.6
type b = AbstractButton
type t = swingEvent.AncestorListener
package ancient:
package p {
class Bippo {
def length: Int = 123
class Tree
}
}
package object p1 {
import p._
class A
implicit class B(val s: String) { def bippy = s }
val c: Bippo = new Bippo
type D = String
}
package object p2 {
import p._
class A
implicit class B(val s: String) { def bippy = s }
val c: Bippo = new Bippo
type D = Int
}
trait NoWarn {
{
import p1._ // no warn
println("abc".bippy)
}
{
import p1._ // no warn
println(new A)
}
{
import p1.B // no warn
println("abc".bippy)
}
{
import p1._ // no warn
import c._ // no warn
println(length)
}
{
import p1._ // no warn
import c._ // no warn
val x: Tree = null
println(x)
}
{
import p1.D // no warn
val x: D = null
println(x)
}
}
trait Warn {
{
import p1.A // warn
println(123)
}
{
import p1.{ A, B } // warn on A
println("abc".bippy)
}
{
import p1.{ A, B } //warn // warn on both
println(123)
}
{
import p1._ // no warn (technically this could warn, but not worth the effort to unroll unusedness transitively)
import c._ // warn
println(123)
}
{
import p1._ // warn
println(123)
}
{
class Tree
import p1._ // no warn
import c._ // warn
val x: Tree = null
println(x)
}
{
import p1.c._ // warn
println(123)
}
}
trait Nested {
{
import p1._ // warn
trait Warn {
import p2.*
println(new A)
println("abc".bippy)
}
println("")
}
{
import p1._ // no warn
trait NoWarn {
import p2.B // no warn
println("abc".bippy)
println(new A)
}
println(new NoWarn { })
}
{
import p1.A // warn
trait Warn {
import p2.A
println(new A)
}
println(new Warn { })
}
}
end ancient