Skip to content

Implement applied constructor types #22543

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 16 commits into from
May 1, 2025

Conversation

KacperFKorban
Copy link
Member

@KacperFKorban KacperFKorban commented Feb 6, 2025

closes #22542

Introduce new syntax to make classes with tracked parameters
easier to use. The new syntax is essentially the ability to use an application
of a class constructor as a type, we call such types applied constructor types.

With this new feature the following example compiles correctly and the type in
the comment is the resulting type of the applied constructor types.

import scala.language.experimental.modularity

class C(tracked val v: Any)

val c: C(42) /* C { val v: 42 } */ = C(42)

Syntax change

SimpleType        ::=  SimpleLiteral
                    |  ‘?’ TypeBounds
---                 |  SimpleType1
+++                 |  SimpleType1 {ParArgumentExprs}

A SimpleType can now optionally be followed by ParArgumentExprs.

@KacperFKorban KacperFKorban added the release-notes Should be mentioned in the release notes label Feb 7, 2025
@KacperFKorban KacperFKorban force-pushed the mb/applied-constructors branch from a12b589 to f64cc82 Compare February 7, 2025 17:16
@KacperFKorban KacperFKorban marked this pull request as ready for review February 10, 2025 11:43
@KacperFKorban
Copy link
Member Author

The CI failure seems to be the same as on main and looks unrelated.

@KacperFKorban KacperFKorban force-pushed the mb/applied-constructors branch from 51894ab to 64aee9d Compare February 11, 2025 08:12
@mbovel
Copy link
Member

mbovel commented Feb 11, 2025

I've noticed during your presentation that it also works for using parameters:

import language.experimental.modularity

class Box[T](using tracked val value: Int)

def main =
  given Int = 42
  val b: Box() = Box()

after typer:

def main: Unit =
  final lazy given val given_Int: Int = 42
  val b: Box[Any]{val value: (given_Int : Int)} = new Box[Any](given_Int)()

classTp.classSymbol.primaryConstructor.paramSymss.flatten.filter(_.isTerm).forall(_.is(Tracked))
if !preciseTp.isError && !classSymbolHasOnlyTrackedParameters then
report.warning(OnlyFullyDependentAppliedConstructorType(), tree.srcPos)
if !preciseTp.isError && (preciseTp frozen_=:= classTp) then
Copy link
Contributor

Choose a reason for hiding this comment

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

I did not understand the condition. I guess you want to exclude constructors with no value parameters, but I am not clear why the condition preciseTp frozen_=:= classTp is equivalent to that.

Copy link
Member Author

Choose a reason for hiding this comment

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

This checks a slightly different condition. This also triggers for classes with non-tracked parameters.
e.g.
In the following case, the type of UnspecificBox and UnspecificBox(4) is the same.

class UnspecificBox(val v: Any)
val v1: UnspecificBox(4) = UnspecificBox(4) // warn

@@ -3525,7 +3517,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer

def typedUnnamed(tree: untpd.Tree): Tree = tree match {
case tree: untpd.Apply =>
if (ctx.mode is Mode.Pattern) typedUnApply(tree, pt) else typedApply(tree, pt)
if (ctx.mode is Mode.Pattern) typedUnApply(tree, pt)
else if (Feature.enabled(modularity) && ctx.mode.is(Mode.Type) && !ctx.isAfterTyper) typedAppliedConstructorType(tree)
Copy link
Contributor

Choose a reason for hiding this comment

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

The new case should not be added here. This is about the hottest code that exists in the compiler. We need to keep it as small as possible. Instead I would suggest to add the distinction to Applications.scala following line 1283:

val app = tree.fun match
  case Select(New(tpt), _) 
  if ctx.mode.is(Mode.Type) && Feature.enabled(modularity && !ctx.isAfterTyper => 
    typedAppliedConstructorType(tpt, tree.args)
  case untpd.TypeApply(_: untpd.SplicePattern, _) if Feature.quotedPatternsWithPolymorphicFunctionsEnabled =>
    ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I moved it to Applications.scala. Though I added a nested case (that defaults to realApply) for applied constructors, since we match on untpd.methPart(tree.fun), instead of just tree.fun

@odersky odersky assigned KacperFKorban and unassigned odersky Apr 7, 2025
@KacperFKorban KacperFKorban force-pushed the mb/applied-constructors branch from 838a3bc to a7d8cb4 Compare April 23, 2025 12:40
@KacperFKorban KacperFKorban requested a review from odersky April 24, 2025 09:37
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

LGTM. Needs a conflict resolution with ErrorMessageID.

@odersky odersky assigned KacperFKorban and unassigned odersky Apr 24, 2025
@KacperFKorban KacperFKorban force-pushed the mb/applied-constructors branch from a7d8cb4 to 8515ad0 Compare April 25, 2025 08:59
@KacperFKorban KacperFKorban force-pushed the mb/applied-constructors branch from 8515ad0 to aa14a16 Compare May 1, 2025 08:45
@KacperFKorban KacperFKorban force-pushed the mb/applied-constructors branch from aa14a16 to d3fe2d5 Compare May 1, 2025 08:47
@KacperFKorban KacperFKorban merged commit 83ffe00 into scala:main May 1, 2025
29 checks passed
@KacperFKorban KacperFKorban deleted the mb/applied-constructors branch May 1, 2025 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes Should be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support "Applied Constructor Types"
4 participants