Skip to content
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

Fix #19907: Skip soft unions in widenSingle of widenInferred #19995

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 16 additions & 6 deletions compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,13 @@ trait ConstraintHandling {
* as those could leak the annotation to users (see run/inferred-repeated-result).
*/
def widenInferred(inst: Type, bound: Type, widenUnions: Boolean)(using Context): Type =
def typeSize(tp: Type): Int = tp match
case tp: AndOrType => typeSize(tp.tp1) + typeSize(tp.tp2)
case _ => 1

def widenOr(tp: Type) =
if widenUnions then
val tpw = tp.widenUnion
if (tpw ne tp) && !tpw.isTransparent() && (tpw <:< bound) then tpw else tp
else tp.hardenUnions
val tpw = tp.widenUnion
if (tpw ne tp) && !tpw.isTransparent() && (tpw <:< bound) then tpw else tp

def widenSingle(tp: Type) =
val tpw = tp.widenSingletons
Expand All @@ -665,8 +667,16 @@ trait ConstraintHandling {
val wideInst =
if isSingleton(bound) then inst
else
val widenedFromSingle = widenSingle(inst)
val widenedFromUnion = widenOr(widenedFromSingle)
val widenedFromUnion =
if widenUnions && typeSize(inst) > 64 then
// If the inferred type `inst` is too large, the subtype check for `bound` in `widenSingle`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very useful diagnosis! I think we can even go further: If we have widenUnions then we never need to widen singletons in soft unions, since we will do an OrDominator afterwards, and this is the same for original or widen types. We don't even need a size limit for this.

I'll push a commit that tries this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correction: It does not work since we sometimes leave original orType if it is transparent, and then we should ideally widen singleton types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could fix it and account for transparent unions. See latest commit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I updated the title.

// can be expensive due to comparisons between large union types, so we avoid it by
// `widenUnion` directly here.
// See issue #19907.
widenOr(inst)
else
val widenedFromSingle = widenSingle(inst)
if widenUnions then widenOr(widenedFromSingle) else widenedFromSingle.hardenUnions
val widened = dropTransparentTraits(widenedFromUnion, bound)
widenIrreducible(widened)

Expand Down
Loading