Skip to content

Handle AvoidMap recursions #15393

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 2 commits into from
Jun 12, 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
66 changes: 35 additions & 31 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -423,38 +423,42 @@ object TypeOps:
sym.is(Package) || sym.isStatic && isStaticPrefix(pre.prefix)
case _ => true

override def apply(tp: Type): Type = tp match
case tp: TermRef
if toAvoid(tp) =>
tp.info.widenExpr.dealias match {
case info: SingletonType => apply(info)
case info => range(defn.NothingType, apply(info))
}
case tp: TypeRef if toAvoid(tp) =>
tp.info match {
case info: AliasingBounds =>
apply(info.alias)
case TypeBounds(lo, hi) =>
range(atVariance(-variance)(apply(lo)), apply(hi))
case info: ClassInfo =>
range(defn.NothingType, apply(classBound(info)))
override def apply(tp: Type): Type =
try
tp match
case tp: TermRef
if toAvoid(tp) =>
tp.info.widenExpr.dealias match {
case info: SingletonType => apply(info)
case info => range(defn.NothingType, apply(info))
}
case tp: TypeRef if toAvoid(tp) =>
tp.info match {
case info: AliasingBounds =>
apply(info.alias)
case TypeBounds(lo, hi) =>
range(atVariance(-variance)(apply(lo)), apply(hi))
case info: ClassInfo =>
range(defn.NothingType, apply(classBound(info)))
case _ =>
emptyRange // should happen only in error cases
}
case tp: ThisType =>
// ThisType is only used inside a class.
// Therefore, either they don't appear in the type to be avoided, or
// it must be a class that encloses the block whose type is to be avoided.
tp
case tp: LazyRef =>
if localParamRefs.contains(tp.ref) then tp
else if isExpandingBounds then emptyRange
else mapOver(tp)
case tl: HKTypeLambda =>
localParamRefs ++= tl.paramRefs
mapOver(tl)
case _ =>
emptyRange // should happen only in error cases
}
case tp: ThisType =>
// ThisType is only used inside a class.
// Therefore, either they don't appear in the type to be avoided, or
// it must be a class that encloses the block whose type is to be avoided.
tp
case tp: LazyRef =>
if localParamRefs.contains(tp.ref) then tp
else if isExpandingBounds then emptyRange
else mapOver(tp)
case tl: HKTypeLambda =>
localParamRefs ++= tl.paramRefs
mapOver(tl)
case _ =>
super.apply(tp)
super.apply(tp)
catch case ex: Throwable =>
handleRecursive("traversing for avoiding local references", s"${tp.show}" , ex)
end apply

/** Three deviations from standard derivedSelect:
Expand Down
17 changes: 17 additions & 0 deletions tests/neg-custom-args/allow-deep-subtypes/i15365.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trait Tagged[U]
Copy link
Member

Choose a reason for hiding this comment

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

Btw, you can also just add // scalac: -Yno-deep-subtypes

type WithTag[+TT, UU] = TT & Tagged[UU]

trait FromInput[Val]
implicit def coercedScalaInput[Tc1]: FromInput[WithTag[Tc1, Int]] = ???
implicit def optionInput[To](implicit ev: FromInput[To]): FromInput[Option[To]] = ???

trait WithoutInputTypeTags[TW]
implicit def coercedOptArgTpe[Tc]: WithoutInputTypeTags[Option[WithTag[Tc, Int]]] = ???

trait InputType[+TI]
class OptionInputType[TO](ofType: InputType[TO]) extends InputType[Option[TO]]

type Argument[TA]
def argument[Ta](argumentType: InputType[Ta])(implicit fromInput: FromInput[Ta], res: WithoutInputTypeTags[Ta]): Argument[Option[Ta]] = ???

def test = argument(OptionInputType(??? : InputType[WithTag[Boolean, Int]])) :: Nil // error