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

New using directive syntax #546

Merged
merged 13 commits into from
Jan 24, 2022
Merged

Conversation

romanowski
Copy link
Member

@romanowski romanowski commented Jan 15, 2022

This PR changes the syntax of using directives. Following syntax is supported:

  • using foo bar with using keyword
  • //> using scala "3" or /*> using scala "#" using keyword within a comment with special indicator >

Following syntax is deprecated:

  • @using scala 3, // @using scala 3 - with@using global annotation
  • // using scala "3", /* using scala "3"*/ - within a comment within special indicator.

Deprecated syntaxes result in a warning and will be supported within the next minor release: 0.1.x and results in error within 0.2.x.

Warnings are also working within BSP.

cc @julienrf

@romanowski romanowski changed the title Better comments New using directive syntax Jan 18, 2022
@romanowski romanowski marked this pull request as ready for review January 18, 2022 11:11
@romanowski romanowski force-pushed the better-comments branch 2 times, most recently from fb87f23 to 6920d8f Compare January 18, 2022 12:05
Copy link
Contributor

@alexarchambault alexarchambault left a comment

Choose a reason for hiding this comment

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

I didn't review the core of the changes yet, but I wish the git history was clearer: as of now, this mixes various unrelated changes (refactorings, new using directive syntax, changes in the build), so I'd rather not squash-and-merge, but I wouldn't want to merge with a merge commit either to avoid "reformating" or "address CI failures" commits (which don't really make sense on their own, when later bisecting issues for example).

project/deps.sc Outdated
Seq(
coursier.Repositories.sonatype("snapshots"),
// Uncomment for local development
coursier.MavenRepository(s"file://${System.getProperty("user.home")}/.m2/repository")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be commented out?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, my bad

scala-cli Outdated
@@ -17,7 +17,7 @@ if [ ! -x "$LAUNCHER_DIR/scala" ]; then
echo "native-image launcher not built yet." 1>&2
echo "In order to build it, go in $DIR, and run" 1>&2
echo 1>&2
echo " ./mill cli.nativeImage" 1>&2
echo " ./mill copyTo cli.nativeImage out/cli/nativeImage/dest/scala" 1>&2
Copy link
Contributor

Choose a reason for hiding this comment

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

Just ./mill cli.nativeImage should be enough. We should change LAUNCHER_DIR if the native image doesn't land there.


Until `using` directives becomes a part of the Scala specification, this is the only way that guarantees that your code will work well with IDEs, code formatters, and other tools.
:::

Within one file, only one flavor of using directives can be used. The keyword-base syntax (`using scala "3"`) has precedence over special comments (`//> using scala "3"`) and deprecated, plain comments (`// using scala "3"`) has lowest priority.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Within one file, only one flavor of using directives can be used. The keyword-base syntax (`using scala "3"`) has precedence over special comments (`//> using scala "3"`) and deprecated, plain comments (`// using scala "3"`) has lowest priority.
Within one file, only one flavor of using directives can be used. The keyword-based syntax (`using scala "3"`) has precedence over special comments (`//> using scala "3"`) and deprecated, plain comments (`// using scala "3"`) has lowest priority.


Until `using` directives becomes a part of the Scala specification, this is the only way that guarantees that your code will work well with IDEs, code formatters, and other tools.
:::

Within one file, only one flavor of using directives can be used. The keyword-base syntax (`using scala "3"`) has precedence over special comments (`//> using scala "3"`) and deprecated, plain comments (`// using scala "3"`) has lowest priority.

For now `using` and `@using` can be mixed within given syntax however we strongly suggest to does not use deprecated `@using`.
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Suggested change
For now `using` and `@using` can be mixed within given syntax however we strongly suggest to does not use deprecated `@using`.
For now `using` and `@using` can be mixed within a given syntax however we strongly suggest not to use deprecated `@using`.

```
## Deprecated syntax

As a part of `0.0.x` series we experimented with different syntax for using directives. Based on the feedback and discussions with Scala compiler teams we decided to deprecated `@using` (using annotations) and `// using` (using within plain import). Those syntaxes will work as a part of of `0.1.x` series and we will result with an error since `0.2.x`.
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Suggested change
As a part of `0.0.x` series we experimented with different syntax for using directives. Based on the feedback and discussions with Scala compiler teams we decided to deprecated `@using` (using annotations) and `// using` (using within plain import). Those syntaxes will work as a part of of `0.1.x` series and we will result with an error since `0.2.x`.
As a part of `0.0.x` series we experimented with different syntaxes for using directives. Based on feedback and discussions with the Scala compiler team, we decided to deprecate `@using` (using annotations) and `// using` (using within plain comment). Those syntaxes will keep working in the `0.1.x` series and will result in an error starting from `0.2.x`.


As a part of `0.0.x` series we experimented with different syntax for using directives. Based on the feedback and discussions with Scala compiler teams we decided to deprecated `@using` (using annotations) and `// using` (using within plain import). Those syntaxes will work as a part of of `0.1.x` series and we will result with an error since `0.2.x`.

Scala CLI produces a warnings if any of the syntaxes above is used:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Scala CLI produces a warnings if any of the syntaxes above is used:
Scala CLI produces warnings if any of the syntaxes above is used:

Copy link
Contributor

@alexarchambault alexarchambault left a comment

Choose a reason for hiding this comment

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

(It seems some *.semanticdb are committed too.)

@@ -135,40 +143,42 @@ final class BspImpl(
buildChangedTest
)

(mainScope, testScope)
// ala a a
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Suggested change
// ala a a

Copy link
Member Author

Choose a reason for hiding this comment

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

ah,I needed to force recompilation :/

notifyBuildChange(actualLocalServer)

Build.buildOnce(
Copy link
Contributor

Choose a reason for hiding this comment

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

Independently of the changes of this PR, it seems both Build.buildOnce calls should have been wrapped in a value { … } :/

Copy link
Contributor

Choose a reason for hiding this comment

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

Checking how it goes in #557.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can wrap it but let me know if value { } works

cause: Throwable = null
) extends Exception(message, cause)
) extends Exception(message, cause) with Diagnostic {
override def severity: Severity = Severity.Error
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't need to put a severity here… This might lead to methods returning Either[BuildException, T] to return a Left(ex) with ex.severity == Severity.Warning, which doesn't make sense (exceptions on the left here are implicitly assumed to be plain errors). The severity field makes "invalid states representable".

Copy link
Member Author

Choose a reason for hiding this comment

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

I've put severity here just to be able to process BuildException and diagnostics using the same code and hence I need to fix severity here. I can change it to final def to make sure nobody will treat Exception as warnings.

@@ -1,6 +1,13 @@
package scala.build.preprocessing

import _root_.com.virtuslab.using_directives.custom.utils.ast.UsingDefs
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT

Suggested change
import _root_.com.virtuslab.using_directives.custom.utils.ast.UsingDefs
import com.virtuslab.using_directives.custom.utils.ast.UsingDefs

case class Warn(messagePattern: String, line: Int, index: Int) extends Check {
def test(diags: Seq[Diagnostic]): Boolean = {
val matching = diags.filter(_.positions.collect {
case Position.File(_, (l, c), _) if l == line && index == c => l
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT

Suggested change
case Position.File(_, (l, c), _) if l == line && index == c => l
case Position.File(_, (`line`, `index`), _) => line

(That said, I often forget about matching with backticks myself, so there might be other cases in the code base where we can do the same…)

def test(diags: Seq[Diagnostic]): Boolean = {
val matching = diags.filter(_.positions.collect {
case Position.File(_, (l, c), _) if l == line && index == c => l
}.nonEmpty)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the .collect { … }.nonEmpty can be changed to .exists { case Position.File(…) => …; case _ => false }?

Comment on lines 22 to 24
matching.map(_.message).collect {
case Regex() => true
}.nonEmpty
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

Suggested change
matching.map(_.message).collect {
case Regex() => true
}.nonEmpty
matching.map(_.message).exists {
case Regex() => true
case _ => false
}

@romanowski
Copy link
Member Author

@alexarchambault I updated the commit structure so it should be easier to review. I can also separate 202f37a to different PR since all those changes are basically replacing // using with //> using

bloopServer
).left.map(_ -> scope)

value(doBuildOnce(preBuild.mainScope, Scope.Main))
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use for-comprehensions as long as we have problems with left-types mismatch in EitherCps

Copy link
Member Author

Choose a reason for hiding this comment

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

sure

Comment on lines +121 to +124
// coursier.MavenRepository(
// (os.Path(System.getProperty("user.home")) / ".m2" / "repository")
// .toNIO.toUri.toASCIIString
// )
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC you can add MavenLocal repo with: coursier.LocalRepositories.Dangerous.maven2Local


Until `using` directives becomes a part of the Scala specification, this is the only way that guarantees that your code will work well with IDEs, code formatters, and other tools.
:::

Within one file, only one flavor of using directives can be used. The keyword-based syntax (`using scala "3"`) has precedence over special comments (`//> using scala "3"`) and deprecated, plain comments (`// using scala "3"`) has lowest priority.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Within one file, only one flavor of using directives can be used. The keyword-based syntax (`using scala "3"`) has precedence over special comments (`//> using scala "3"`) and deprecated, plain comments (`// using scala "3"`) has lowest priority.
Within one file, only one flavor of using directives can be used. The keyword-based syntax (`using scala "3"`) has precedence over special comments (`//> using scala "3"`). The deprecated, plain comments (`// using scala "3"`) have lowest priority.

Comment on lines +1 to +2
//> using scala 2.13
//> using target scala-js
Copy link
Contributor

Choose a reason for hiding this comment

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

Did I omit a moment when we allowed strings without quotes?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we do not check examples if compile file, a good catch, I've open a new issue for that: #575

I will also include fixes in a followup PR.

Comment on lines +1 to +2
//> using scala 2.13
//> using target scala-native
Copy link
Contributor

Choose a reason for hiding this comment

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

As above

@@ -95,7 +95,7 @@ Using directives syntax is still experimental and may change in future versions
So when we will have:

```scala title=version.scala
// using scala 2.12.5
//> using scala 2.12.5
Copy link
Contributor

Choose a reason for hiding this comment

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

As above

@@ -1,6 +1,6 @@
// File was generated from based on docs/cookbooks/scala-versions.md, do not edit manually!

// using scala 2.12.5
//> using scala 2.12.5
Copy link
Contributor

Choose a reason for hiding this comment

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

As above

@romanowski romanowski merged commit 2c8add7 into VirtusLab:master Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants