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

Add support for var in refinements #19982

Merged
merged 1 commit into from
Apr 3, 2024
Merged

Conversation

hamzaremmal
Copy link
Member

@hamzaremmal hamzaremmal commented Mar 19, 2024

Closes #19809

@hamzaremmal hamzaremmal added the needs-minor-release This PR cannot be merged until the next minor release label Mar 19, 2024
@hamzaremmal hamzaremmal self-assigned this Mar 19, 2024
@hamzaremmal hamzaremmal added this to the 3.5.0 milestone Mar 19, 2024
@hamzaremmal hamzaremmal requested a review from odersky March 20, 2024 09:11
@@ -1943,12 +1943,26 @@ object desugar {
case AndType(tp1, tp2) => stripToCore(tp1) ::: stripToCore(tp2)
case _ => defn.AnyType :: Nil
}

val refinements1 = refinements.flatMap {
Copy link
Contributor

@odersky odersky Mar 23, 2024

Choose a reason for hiding this comment

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

It's more efficient to make this a refinements.mapConserve(...).flatten. If there are no var refinements this will then never copy the trees.

case tree: ValDef if tree.mods.is(Mutable) =>
val getter =
cpy.DefDef(tree)(name = tree.name, paramss = Nil, tpt = tree.tpt, rhs = tree.rhs)
.withMods(Modifiers(tree.mods.flags & (AccessFlags | Synthetic), tree.mods.privateWithin))
Copy link
Contributor

Choose a reason for hiding this comment

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

This expression appears twice and could be lifted out. Also, one can use withFlags.

val mods1 = tree.mods.withFlags(tree.mods.flags & AccessFlags | Synthetic)

val setter =
cpy.DefDef(tree)(name = tree.name.setterName, paramss = (setterParam :: Nil) :: Nil, tpt = untpd.scalaUnit, rhs = EmptyTree)
.withMods(Modifiers(tree.mods.flags & (AccessFlags | Synthetic), tree.mods.privateWithin))
List(getter, setter)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we go with mapConserve, this will be Thicket(getter, setter)

cpy.DefDef(tree)(name = tree.name.setterName, paramss = (setterParam :: Nil) :: Nil, tpt = untpd.scalaUnit, rhs = EmptyTree)
.withMods(Modifiers(tree.mods.flags & (AccessFlags | Synthetic), tree.mods.privateWithin))
List(getter, setter)
case tree => tree :: Nil
Copy link
Contributor

Choose a reason for hiding this comment

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

If we go with mapConserve, this will be tree.

@@ -431,6 +431,7 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ |
### Definitions
```ebnf
RefineDcl ::= ‘val’ ValDcl
| ‘var’ ValDcl
Copy link
Contributor

Choose a reason for hiding this comment

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

Same change needed in reference/syntax.md.

@odersky odersky assigned hamzaremmal and unassigned hamzaremmal Mar 23, 2024
Co-authored-by: Anna Herlihy <[email protected]>
Co-authored-by: Nicolas Stucki <[email protected]>
@Gedochao
Copy link
Contributor

Gedochao commented Apr 3, 2024

This has been decided to be included in the 3.5.0 release.

@hamzaremmal hamzaremmal merged commit ece87c3 into scala:main Apr 3, 2024
19 checks passed
@hamzaremmal hamzaremmal deleted the i19809 branch April 3, 2024 14:20
@hamzaremmal hamzaremmal added the release-notes Should be mentioned in the release notes label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-minor-release This PR cannot be merged until the next minor release release-notes Should be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow var in refinements
3 participants