File tree 4 files changed +25
-5
lines changed
compiler/src/dotty/tools/dotc 4 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -490,7 +490,10 @@ object messages {
490
490
| ${" var a = _" }
491
491
|
492
492
|Note that this use of `_` is not placeholder syntax,
493
- |but an uninitialized var definition """
493
+ |but an uninitialized var definition.
494
+ |Only fields can be left uninitialized in this manner; local variables
495
+ |must be initialized.
496
+ | """
494
497
}
495
498
496
499
case class IllegalStartSimpleExpr (illegalToken : String )(implicit ctx : Context )
Original file line number Diff line number Diff line change @@ -774,6 +774,7 @@ class RefChecks extends MiniPhase { thisTransformer =>
774
774
775
775
import tpd ._
776
776
import reporting .diagnostic .messages .ForwardReferenceExtendsOverDefinition
777
+ import dotty .tools .dotc .reporting .diagnostic .messages .UnboundPlaceholderParameter
777
778
778
779
override def phaseName : String = " refchecks"
779
780
@@ -796,12 +797,19 @@ class RefChecks extends MiniPhase { thisTransformer =>
796
797
override def transformValDef (tree : ValDef )(implicit ctx : Context , info : TransformerInfo ) = {
797
798
checkDeprecatedOvers(tree)
798
799
val sym = tree.symbol
799
- if (sym.exists && sym.owner.isTerm && ! sym.is(Lazy ))
800
- currentLevel.levelAndIndex.get(sym) match {
801
- case Some ((level, symIdx)) if symIdx <= level.maxIndex =>
802
- ctx.error(ForwardReferenceExtendsOverDefinition (sym, level.refSym), level.refPos)
800
+ if (sym.exists && sym.owner.isTerm) {
801
+ tree.rhs match {
802
+ case Ident (nme.WILDCARD ) => ctx.error(UnboundPlaceholderParameter (), sym.pos)
803
803
case _ =>
804
804
}
805
+ if (! sym.is(Lazy )) {
806
+ currentLevel.levelAndIndex.get(sym) match {
807
+ case Some ((level, symIdx)) if symIdx <= level.maxIndex =>
808
+ ctx.error(ForwardReferenceExtendsOverDefinition (sym, level.refSym), level.refPos)
809
+ case _ =>
810
+ }
811
+ }
812
+ }
805
813
tree
806
814
}
807
815
Original file line number Diff line number Diff line change
1
+ class C {
2
+ def foo () = {
3
+ var x : String = _ // error: local variables can't be left uninitialized
4
+ }
5
+ }
Original file line number Diff line number Diff line change
1
+ class C {
2
+ // A `var` field can be left uninitialized.
3
+ var x : String = _
4
+ }
You can’t perform that action at this time.
0 commit comments