-
Notifications
You must be signed in to change notification settings - Fork 1.1k
cant define nn in usercode with -Yexplicit-nulls #7882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Looks like the type is considered not stable because it's not normalized enough after flow typing. As a workaround, you can work around flow typing and write: def nonnull[T](x: T | Null): x.type & T =
val isNull = x == null
if (isNull) throw new NullPointerException("tried to cast away nullability, but value is null")
else x.asInstanceOf[x.type & T] |
nice |
Actually, with flow typing, you don't need def nonnull[T](x: T|Null): x.type & T =
if (x == null) throw new NullPointerException("tried to cast away nullability, but value is null")
else x The problem is from the /** Does this type denote a stable reference (i.e. singleton type)?
*
* Like in isStableMember, "stability" means idempotence.
* Rationale: If an expression has a stable type, the expression must be idempotent, so stable types
* must be singleton types of stable expressions. */
final def isStable(implicit ctx: Context): Boolean = stripTypeVar match {
case tp: TermRef => tp.symbol.isStableMember && tp.prefix.isStable || tp.info.isStable
case _: SingletonType | NoPrefix => true
case tp: RefinedOrRecType => tp.parent.isStable
case tp: ExprType => tp.resultType.isStable
case tp: AnnotatedType => tp.parent.isStable
case tp: AndType =>
tp.tp1.isStable && (realizability(tp.tp2) eq Realizable) ||
tp.tp2.isStable && (realizability(tp.tp1) eq Realizable)
case _ => false
} I add the In this case, We had some discussion at #7546 (comment) |
You are right. The real problem is the way flow-typing adapts variable references The underlying problem is that Dotty lacks proper reasoning capabilities for singleton types. I have opened an issue about this problem here: #7885 |
Let's keep discussion about this in #7885 |
minimized code
compile with
-Yexplicit-nulls
errors with
this makes it hard to define other APIs in user code that eliminate
Null
, such asOption.apply
The text was updated successfully, but these errors were encountered: