Skip to content

Commit

Permalink
An annotation should not prevent an inferred type from being widened.
Browse files Browse the repository at this point in the history
  • Loading branch information
milessabin committed Jan 17, 2018
1 parent 8b91a3b commit f895d81
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/Namers.scala
Expand Up @@ -1063,7 +1063,11 @@ trait Namers extends MethodSynthesis {
// Are we inferring the result type of a stable symbol, whose type doesn't refer to a hidden symbol?
// If we refer to an inaccessible symbol, let's hope widening will result in an expressible type.
// (A LiteralType should be widened because it's too precise for a definition's type.)
val mayKeepSingletonType = !tpe.isInstanceOf[ConstantType] && sym.isStable && !refersToSymbolLessAccessibleThan(tpe, sym)
val mayKeepSingletonType =
tpe match {
case ConstantType(_) | AnnotatedType(_, ConstantType(_)) => false
case _ => sym.isStable && !refersToSymbolLessAccessibleThan(tpe, sym)
}

// Only final vals may be constant folded, so deconst inferred type of other members.
@inline def keepSingleton = if (sym.isFinal) tpe else tpe.deconst
Expand Down
7 changes: 6 additions & 1 deletion test/files/neg/sip23-widen.check
Expand Up @@ -48,4 +48,9 @@ sip23-widen.scala:62: error: type mismatch;
required: 4
f13 = 5
^
10 errors found
sip23-widen.scala:75: error: type mismatch;
found : Test.annot0.type (with underlying type Int)
required: 1 @unchecked
annot0: 1 @unchecked
^
11 errors found
19 changes: 19 additions & 0 deletions test/files/neg/sip23-widen.scala
Expand Up @@ -60,4 +60,23 @@ object Test {
//f13: Int
//f13: 4
f13 = 5

//final val one = 1
//final val compiles: 2 = one + 1

//final val literalOne: 1 = 1
//final val alsoCompiles: 2 = literalOne + 1

//final val recFive : 5 = recFive + 0

val annot0 = 1: @unchecked
//annot0: Int
//annot0: Int @unchecked
annot0: 1 @unchecked

//final val annot1 = 1: @unchecked
//annot1: Int
//annot1: Int @unchecked
//annot1: 1
//annot1: 1 @unchecked
}
11 changes: 11 additions & 0 deletions test/files/pos/sip23-widen.scala
Expand Up @@ -68,4 +68,15 @@ object Test {
final val alsoCompiles: 2 = literalOne + 1

final val recFive : 5 = recFive + 0

val annot0 = 1: @unchecked
annot0: Int
annot0: Int @unchecked
//annot0: 1 @unchecked

final val annot1 = 1: @unchecked
annot1: Int
annot1: Int @unchecked
annot1: 1
annot1: 1 @unchecked
}

0 comments on commit f895d81

Please sign in to comment.