Skip to content

Workaround case x @ U() : T => (fix main branch) #16369

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

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions tests/pos-with-compiler-cc/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1289,11 +1289,15 @@ object Types {
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
*/
def widenUnion(using Context): Type = widen match
case tp @ OrNull(tp1): OrType =>
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
val tp1Widen = tp1.widenUnionWithoutNull
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
else tp.derivedOrType(tp1Widen, defn.NullType)
case tp @ OrNull(tp1) =>
tp match
case tp: OrType =>
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
val tp1Widen = tp1.widenUnionWithoutNull
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
else tp.derivedOrType(tp1Widen, defn.NullType)
case _ =>
tp.widenUnionWithoutNull
case tp =>
tp.widenUnionWithoutNull

Expand Down
18 changes: 11 additions & 7 deletions tests/pos-with-compiler-cc/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
* (x: T | Null) => x.$asInstanceOf$[x.type & T]
*/
def toNotNullTermRef(tree: Tree, pt: Type)(using Context): Tree = tree.tpe match
case ref @ OrNull(tpnn) : TermRef
if pt != AssignProto && // Ensure it is not the lhs of Assign
ctx.notNullInfos.impliesNotNull(ref) &&
// If a reference is in the context, it is already trackable at the point we add it.
// Hence, we don't use isTracked in the next line, because checking use out of order is enough.
!ref.usedOutOfOrder =>
tree.cast(AndType(ref, tpnn))
case tp @ OrNull(tpnn) =>
tp match
case ref: TermRef
if pt != AssignProto && // Ensure it is not the lhs of Assign
ctx.notNullInfos.impliesNotNull(ref) &&
// If a reference is in the context, it is already trackable at the point we add it.
// Hence, we don't use isTracked in the next line, because checking use out of order is enough.
!ref.usedOutOfOrder =>
tree.cast(AndType(ref, tpnn))
case _ =>
tree
case _ =>
tree

Expand Down