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

Trees after typer losing information #15750

Closed
tgodzik opened this issue Jul 26, 2022 · 5 comments · Fixed by #15825
Closed

Trees after typer losing information #15750

tgodzik opened this issue Jul 26, 2022 · 5 comments · Fixed by #15825
Milestone

Comments

@tgodzik
Copy link
Contributor

tgodzik commented Jul 26, 2022

Compiler version

3.1.3

Minimized code

object Main {
  def foo: ArrayBuffer[Int] = ???
}

ArrayBuffer is missing from imports.

Output

Parser tree

PackageDef(
  pid = Ident(name = <empty>),
  stats = List(
    ModuleDef(
      name = Main,
      impl = Template(
        constr = DefDef(
          name = <init>,
          paramss = List(),
          tpt = TypeTree,
          preRhs = Thicket(trees = List())
        ),
        parentsOrDerived = List(),
        self = ValDef(name = _, tpt = Thicket(trees = List()), preRhs = Thicket(trees = List())),
        preBody = List(
          DefDef(
            name = foo,
            paramss = List(),
            tpt = AppliedTypeTree(tpt = Ident(name = ArrayBuffer), args = List(Ident(name = Int))),
            preRhs = Ident(name = ???)
          )
        )
      )
    )
  )
)

Notice tpt = AppliedTypeTree(tpt = Ident(name = ArrayBuffer), args = List(Ident(name = Int))), <- AppliedTypeTree is available.

Typed tree though:

 tree = TypeDef(
        name = Main$,
        rhs = Template(
          constr = DefDef(
            name = <init>,
            paramss = List(List()),
            tpt = TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Unit)],
            preRhs = Thicket(trees = List())
          ),
          parentsOrDerived = List(
            Apply(
              fun = Select(
                qualifier = New(
                  tpt = TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class Object)]
                ),
                name = <init>
              ),
              args = List()
            )
          ),
          self = ValDef(
            name = _,
            tpt = SingletonTypeTree(ref = Ident(name = Main)),
            preRhs = Thicket(trees = List())
          ),
          preBody = List(
            DefDef(
              name = foo,
              paramss = List(),
              tpt = Ident(name = ArrayBuffer),
              preRhs = Ident(name = ???)
            )
          )
        )
      ),

Notice that the tpt of the DefDef is tpt = Ident(name = ArrayBuffer),

Seems that if the type is not available then we are missing TypeApply, which is useful for the IDE to figure out if we need to insert []

Expectation

TypeApply should still be available in tpt of DefDef. It might be an error type, but it should be available there.

@tgodzik tgodzik added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 26, 2022
@dwijnand dwijnand removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Jul 26, 2022
@dwijnand
Copy link
Member

Something like this should do the trick:

--- compiler/src/dotty/tools/dotc/typer/Typer.scala
+++ compiler/src/dotty/tools/dotc/typer/Typer.scala
@@ -1993,7 +1993,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
       typed(tree.tpt, AnyTypeConstructorProto)
     }
     val tparams = tpt1.tpe.typeParams
-    if (tparams.isEmpty) {
+    if tpt1.tpe.isError then
+      val args1 = tree.args.mapconserve(typedType(_))
+      assignType(cpy.AppliedTypeTree(tree)(tpt1, args1), tpt1, args1)
+    else if (tparams.isEmpty) {
       report.error(TypeDoesNotTakeParameters(tpt1.tpe, tree.args), tree.srcPos)
       tpt1
     }

@tgodzik
Copy link
Contributor Author

tgodzik commented Jul 26, 2022

Looks like this is exactly what I am looking for, thanks! I can take a look and try and raise a PR with fix or would you want to do it?

@dwijnand
Copy link
Member

All yours - I got curious to try, but not curious enough to babysit the PR into main. 😄

@vzmerr
Copy link

vzmerr commented Jul 27, 2022

I invite you to take a look at the problem this issue has caused in Metals, for the detection of existing type parameters, so that an extra type bracket is not inserted upon type completion.
scalameta/metals#4174 (comment)

@tgodzik
Copy link
Contributor Author

tgodzik commented Aug 5, 2022

@dwijnand Seems like that did the trick! Thanks! #15825

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants