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

Make explicit arguments for context bounds an error from 3.5 #19316

Merged
merged 5 commits into from
Jan 10, 2024

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Dec 20, 2023

Warning in 3.4. Rewrite available in 3.5-migration.

[test_scala2_library_tasty]

@odersky
Copy link
Contributor Author

odersky commented Dec 20, 2023

There's a difference in neg/hidden-type-errors and a failure in ReplTests. Both look like some internals of quotes pass
an explicit argument to a context bound parameter. It should be a using argument instead. @nicolasstucki , can you check?

@odersky
Copy link
Contributor Author

odersky commented Dec 23, 2023

@nicolasstucki There was an error in scala2-library-tasty-test. Can you advise what the reason could be?

- Error: /__w/dotty/dotty/scala2-library-tasty-tests/src/Main.scala:7:19 ------
Error:  7 |  case Red, Green, Blue
Error:    |                   ^
Error:    |Context bounds will map to context parameters.
Error:    |A `using` clause is needed to pass explicit arguments to them.
Error:    |This code can be rewritten automatically under -rewrite -source 3.5-migration.
Error:  one error found

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Jan 3, 2024

@nicolasstucki There was an error in scala2-library-tasty-test. Can you advise what the reason could be?

- Error: /__w/dotty/dotty/scala2-library-tasty-tests/src/Main.scala:7:19 ------
Error:  7 |  case Red, Green, Blue
Error:    |                   ^
Error:    |Context bounds will map to context parameters.
Error:    |A `using` clause is needed to pass explicit arguments to them.
Error:    |This code can be rewritten automatically under -rewrite -source 3.5-migration.
Error:  one error found

The reason is that we compile the standard library with this new mode. In particular the def apply[T: ClassTag](xs: T*): Array[T] of Array now expects the using when the ClassTag argument is passed explicitly. This is happening in the desugaring of the enumeration in:

    private[this] val $values: Array[Color] =
      Array.apply[Color]([this.Red,this.Green,this.Blue : Color]*)(
        scala.reflect.ClassTag.apply[Color](classOf[Color]))

The solution is to make sure all desugarings that explicitly apply a context-bound set the ApplyKind to Using.

This particular error can be fixed in DesugarEnums.scala

private def ArrayLiteral(values: List[Tree], tpt: Tree)(using Context): Tree =
    val clazzOf = TypeApply(ref(defn.Predef_classOf.termRef), tpt :: Nil)
    val ctag    = Apply(TypeApply(ref(defn.ClassTagModule_apply.termRef), tpt :: Nil), clazzOf :: Nil)
    val apply   = Select(ref(defn.ArrayModule.termRef), nme.apply)
-   Apply(Apply(TypeApply(apply, tpt :: Nil), values), ctag :: Nil)
+   Apply(Apply(TypeApply(apply, tpt :: Nil), values), ctag :: Nil).setApplyKind(ApplyKind.Using)

I pushed this fix to this PR.

This fix makes scala2-library-tasty-tests/run and scala2-library-tasty-tests/test pass, though we might find other cases when running all tests with the TASTy library in test_scala2_library_tasty CI job. This one is usually tested on nightlies, I will enable it on this PR to make sure we are not missing something else.

@odersky odersky force-pushed the context-bounds-migration branch from 2dd57a0 to 786852c Compare January 6, 2024 18:38
@odersky
Copy link
Contributor Author

odersky commented Jan 7, 2024

@nicolasstucki Ready for review.

@nicolasstucki nicolasstucki added this to the 3.5.0 milestone Jan 8, 2024
report.errorOrMigrationWarning(
em"""Context bounds will map to context parameters.
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""",
tree.srcPos, MigrationVersion(`3.4`, `3.5`))
Copy link
Contributor

Choose a reason for hiding this comment

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

This migration version should be defined in MigrationVersion.scala as ExplicitContextBoundArgument.

em"""Context bounds will map to context parameters.
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""",
tree.srcPos, MigrationVersion(`3.4`, `3.5`))
if sourceVersion.isAtLeast(`3.5-migration`) then
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use ExplicitContextBoundArgument.needsPatch. It will use 3.4-migration and check that the version is -migrating.

&& isContextBoundParams
&& pt.applyKind != ApplyKind.Using
then
def rewriteMsg = Message.rewriteNotice("This code", `3.5-migration`)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use ExplicitContextBoundArgument. See comment bellow.

def remedy =
if ((prefix ++ suffix).isEmpty) "simply leave out the trailing ` _`"
else s"use `$prefix<function>$suffix` instead"
def rewrite = Message.rewriteNotice("This construct", `3.4-migration`)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be

Suggested change
def rewrite = Message.rewriteNotice("This construct", `3.4-migration`)
def rewrite = Message.rewriteNotice("This construct", MigrationVersion.FunctionUnderscore.warnVersion)

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rewriteNotice should receive a MigrationVersion instead. I will check that later an refactor all the use cases if needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Tests with warnings should now be added to tests/warn.

tests/warn/context-bounds-migration.scala

class C[T]
def foo[X: C] = ()

given [T]: C[T] = C[T]()

def Test =
  foo(C[Int]())  // warn
  foo(using C[Int]()) // ok

* and migrations.
*/
trait Migrations:
this: Typer =>
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not convinced we should make this one a mixin. The one use case right now for typedAsFunction, all other functions that are here and could go in here seem to be static.

I would suggest making Migrations an object and redefining Typer.typedAsFunction as

def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(using Context): Tree =
    migrate(Migrations.typedAsFunction(tree, pt), throw new AssertionError("can't retype a PostfixOp"))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there's nothing wrong with a mixin, and the hack of passing an explicit typer is worse. Right now it only applies tp typedAsFunction but I am sure that will not stay the only one.

@@ -158,6 +158,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// Overridden in derived typers
def newLikeThis(nestingLevel: Int): Typer = new Typer(nestingLevel)

// Overridden to do nothing in derived typers
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
// Overridden to do nothing in derived typers
// Overridden to call `disabled` in derived typers

@odersky odersky modified the milestones: 3.5.0, 3.4.0 Jan 8, 2024
@Kordyjan Kordyjan added the backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. label Jan 8, 2024
@odersky odersky assigned nicolasstucki and unassigned odersky Jan 8, 2024
@odersky odersky merged commit fa93c73 into scala:main Jan 10, 2024
19 checks passed
@odersky odersky deleted the context-bounds-migration branch January 10, 2024 08:41
@Kordyjan Kordyjan added backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" and removed backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. labels Jan 18, 2024
Kordyjan added a commit that referenced this pull request Jan 19, 2024
@Kordyjan Kordyjan added backport:done This PR was successfully backported. and removed backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" labels Jan 19, 2024
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Jan 22, 2024
nicolasstucki added a commit that referenced this pull request Jan 23, 2024
Kordyjan pushed a commit that referenced this pull request Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:done This PR was successfully backported.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants