Skip to content

Commit d4d6cec

Browse files
committed
use new infer expected type for singleton complations
1 parent d490d13 commit d4d6cec

File tree

2 files changed

+2
-82
lines changed

2 files changed

+2
-82
lines changed

presentation-compiler/src/main/dotty/tools/pc/InferExpectedType.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dotty.tools.dotc.core.Contexts.Context
77
import dotty.tools.dotc.core.Flags
88
import dotty.tools.dotc.core.StdNames
99
import dotty.tools.dotc.core.Symbols
10+
import dotty.tools.dotc.core.Symbols.defn
1011
import dotty.tools.dotc.core.Types.*
1112
import dotty.tools.dotc.core.Types.Type
1213
import dotty.tools.dotc.interactive.Interactive
@@ -61,7 +62,7 @@ class InferExpectedType(
6162
object InterCompletionType:
6263
def inferType(path: List[Tree])(using Context): Option[Type] =
6364
path match
64-
case (lit: Literal) :: Select(Literal(_), _) :: Apply(Select(Literal(_), _), List(Literal(Constant(null)))) :: rest => inferType(rest, lit.span)
65+
case (lit: Literal) :: Select(Literal(_), _) :: Apply(Select(Literal(_), _), List(s: Select)) :: rest if s.symbol == defn.Predef_undefined => inferType(rest, lit.span)
6566
case ident :: rest => inferType(rest, ident.span)
6667
case _ => None
6768

presentation-compiler/src/main/dotty/tools/pc/completions/SingletonCompletions.scala

-81
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@ import dotty.tools.pc.completions.CompletionValue.SingletonValue
77
import dotty.tools.dotc.ast.tpd.*
88
import dotty.tools.dotc.core.Constants.Constant
99
import dotty.tools.dotc.core.Contexts.Context
10-
import dotty.tools.dotc.core.Flags
11-
import dotty.tools.dotc.core.StdNames
1210
import dotty.tools.dotc.core.Symbols
1311
import dotty.tools.dotc.core.Types.AndType
1412
import dotty.tools.dotc.core.Types.AppliedType
1513
import dotty.tools.dotc.core.Types.ConstantType
1614
import dotty.tools.dotc.core.Types.OrType
17-
import dotty.tools.dotc.core.Types.TermRef
1815
import dotty.tools.dotc.core.Types.Type
1916
import dotty.tools.dotc.core.Types.TypeRef
20-
import dotty.tools.dotc.util.Spans.Span
21-
import dotty.tools.dotc.core.Symbols.defn
2217

2318
object SingletonCompletions:
2419
def contribute(
@@ -55,79 +50,3 @@ object SingletonCompletions:
5550
collectSingletons(tpe1).intersect(collectSingletons(tpe2))
5651
case _ => Nil
5752

58-
object InterCompletionType:
59-
def inferType(path: List[Tree])(using Context): Option[Type] =
60-
path match
61-
case (lit: Literal) :: Select(Literal(_), _) :: Apply(Select(Literal(_), _), List(s: Select)) :: rest if s.symbol == defn.Predef_undefined =>
62-
inferType(rest, lit.span)
63-
case ident :: rest => inferType(rest, ident.span)
64-
case _ => None
65-
66-
def inferType(path: List[Tree], span: Span)(using Context): Option[Type] =
67-
path match
68-
case Apply(head, List(p : Select)) :: rest if p.name == StdNames.nme.??? && p.qualifier.symbol.name == StdNames.nme.Predef && p.span.isSynthetic =>
69-
inferType(rest, span)
70-
case Block(_, expr) :: rest if expr.span.contains(span) =>
71-
inferType(rest, span)
72-
case If(cond, _, _) :: rest if !cond.span.contains(span) =>
73-
inferType(rest, span)
74-
case Typed(expr, tpt) :: _ if expr.span.contains(span) && !tpt.tpe.isErroneous => Some(tpt.tpe)
75-
case Block(_, expr) :: rest if expr.span.contains(span) =>
76-
inferType(rest, span)
77-
case Bind(_, body) :: rest if body.span.contains(span) => inferType(rest, span)
78-
case Alternative(_) :: rest => inferType(rest, span)
79-
case Try(block, _, _) :: rest if block.span.contains(span) => inferType(rest, span)
80-
case CaseDef(_, _, body) :: Try(_, cases, _) :: rest if body.span.contains(span) && cases.exists(_.span.contains(span)) => inferType(rest, span)
81-
case If(cond, _, _) :: rest if !cond.span.contains(span) => inferType(rest, span)
82-
case CaseDef(_, _, body) :: Match(_, cases) :: rest if body.span.contains(span) && cases.exists(_.span.contains(span)) =>
83-
inferType(rest, span)
84-
case NamedArg(_, arg) :: rest if arg.span.contains(span) => inferType(rest, span)
85-
// x match
86-
// case @@
87-
case CaseDef(pat, _, _) :: Match(sel, cases) :: rest if pat.span.contains(span) && cases.exists(_.span.contains(span)) && !sel.tpe.isErroneous =>
88-
sel.tpe match
89-
case tpe: TermRef => Some(tpe.symbol.info).filterNot(_.isErroneous)
90-
case tpe => Some(tpe)
91-
// List(@@)
92-
case SeqLiteral(_, tpe) :: _ if !tpe.tpe.isErroneous =>
93-
Some(tpe.tpe)
94-
// val _: T = @@
95-
// def _: T = @@
96-
case (defn: ValOrDefDef) :: rest if !defn.tpt.tpe.isErroneous => Some(defn.tpt.tpe)
97-
// f(@@)
98-
case (app: Apply) :: rest =>
99-
val param =
100-
for {
101-
ind <- app.args.zipWithIndex.collectFirst {
102-
case (arg, id) if arg.span.contains(span) => id
103-
}
104-
params <- app.symbol.paramSymss.find(!_.exists(_.isTypeParam))
105-
param <- params.get(ind)
106-
} yield param.info
107-
param match
108-
// def f[T](a: T): T = ???
109-
// f[Int](@@)
110-
// val _: Int = f(@@)
111-
case Some(t : TypeRef) if t.symbol.is(Flags.TypeParam) =>
112-
for {
113-
(typeParams, args) <-
114-
app match
115-
case Apply(TypeApply(fun, args), _) =>
116-
val typeParams = fun.symbol.paramSymss.headOption.filter(_.forall(_.isTypeParam))
117-
typeParams.map((_, args.map(_.tpe)))
118-
// val f: (j: "a") => Int
119-
// f(@@)
120-
case Apply(Select(v, StdNames.nme.apply), _) =>
121-
v.symbol.info match
122-
case AppliedType(des, args) =>
123-
Some((des.typeSymbol.typeParams, args))
124-
case _ => None
125-
case _ => None
126-
ind = typeParams.indexOf(t.symbol)
127-
tpe <- args.get(ind)
128-
if !tpe.isErroneous
129-
} yield tpe
130-
case Some(tpe) => Some(tpe)
131-
case _ => None
132-
case _ => None
133-

0 commit comments

Comments
 (0)