-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02b4ec7
Make explicit arguments for context bounds an error from 3.5
odersky 41311bf
Don't run migration operations in ReTypers
odersky 697f107
Fix test
odersky 786852c
Fix context bound application in enum desugaring
nicolasstucki 13a71ef
Address review comments
odersky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package dotty.tools | ||
package dotc | ||
package typer | ||
|
||
import core.* | ||
import ast.* | ||
import Contexts.* | ||
import Types.* | ||
import Flags.* | ||
import Names.* | ||
import StdNames.* | ||
import Symbols.* | ||
import Trees.* | ||
import ProtoTypes.* | ||
import Decorators.* | ||
import config.MigrationVersion as mv | ||
import config.Feature.{sourceVersion, migrateTo3} | ||
import config.SourceVersion.* | ||
import reporting.* | ||
import NameKinds.ContextBoundParamName | ||
import rewrites.Rewrites.patch | ||
import util.Spans.Span | ||
import rewrites.Rewrites | ||
|
||
/** A utility trait containing source-dependent deprecation messages | ||
* and migrations. | ||
*/ | ||
trait Migrations: | ||
this: Typer => | ||
|
||
import tpd.* | ||
|
||
/** Run `migration`, asserting we are in the proper Typer (not a ReTyper) */ | ||
inline def migrate[T](inline migration: T): T = | ||
assert(!this.isInstanceOf[ReTyper]) | ||
migration | ||
|
||
/** Run `migration`, provided we are in the proper Typer (not a ReTyper) */ | ||
inline def migrate(inline migration: Unit): Unit = | ||
if !this.isInstanceOf[ReTyper] then migration | ||
|
||
/** Flag & migrate `?` used as a higher-kinded type parameter | ||
* Warning in 3.0-migration, error from 3.0 | ||
*/ | ||
def kindProjectorQMark(tree: untpd.TypeDef, sym: Symbol)(using Context): Unit = | ||
if tree.name eq tpnme.? then | ||
val addendum = if sym.owner.is(TypeParam) | ||
then ", use `_` to denote a higher-kinded type parameter" | ||
else "" | ||
val namePos = tree.sourcePos.withSpan(tree.nameSpan) | ||
report.errorOrMigrationWarning( | ||
em"`?` is not a valid type name$addendum", namePos, mv.Scala2to3) | ||
|
||
def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(using Context): Tree = { | ||
val untpd.PostfixOp(qual, Ident(nme.WILDCARD)) = tree: @unchecked | ||
val pt1 = if (defn.isFunctionNType(pt)) pt else AnyFunctionProto | ||
val nestedCtx = ctx.fresh.setNewTyperState() | ||
val res = typed(qual, pt1)(using nestedCtx) | ||
res match { | ||
case closure(_, _, _) => | ||
case _ => | ||
val recovered = typed(qual)(using ctx.fresh.setExploreTyperState()) | ||
val msg = OnlyFunctionsCanBeFollowedByUnderscore(recovered.tpe.widen, tree) | ||
report.errorOrMigrationWarning(msg, tree.srcPos, mv.Scala2to3) | ||
if mv.Scala2to3.needsPatch then | ||
// Under -rewrite, patch `x _` to `(() => x)` | ||
msg.actions | ||
.headOption | ||
.foreach(Rewrites.applyAction) | ||
return typed(untpd.Function(Nil, qual), pt) | ||
} | ||
nestedCtx.typerState.commit() | ||
|
||
lazy val (prefix, suffix) = res match { | ||
case Block(mdef @ DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure) => | ||
val arity = vparams.length | ||
if (arity > 0) ("", "") else ("(() => ", "())") | ||
case _ => | ||
("(() => ", ")") | ||
} | ||
val mversion = mv.FunctionUnderscore | ||
def remedy = | ||
if ((prefix ++ suffix).isEmpty) "simply leave out the trailing ` _`" | ||
else s"use `$prefix<function>$suffix` instead" | ||
def rewrite = Message.rewriteNotice("This construct", mversion.patchFrom) | ||
report.errorOrMigrationWarning( | ||
em"""The syntax `<function> _` is no longer supported; | ||
|you can $remedy$rewrite""", | ||
tree.srcPos, mversion) | ||
if mversion.needsPatch then | ||
patch(Span(tree.span.start), prefix) | ||
patch(Span(qual.span.end, tree.span.end), suffix) | ||
|
||
res | ||
} | ||
|
||
/** Flag & migrate explicit normal arguments to parameters coming from context bounds | ||
* Warning in 3.4, error in 3.5, rewrite in 3.5-migration. | ||
*/ | ||
def contextBoundParams(tree: Tree, tp: Type, pt: FunProto)(using Context): Unit = | ||
val mversion = mv.ExplicitContextBoundArgument | ||
def isContextBoundParams = tp.stripPoly match | ||
case MethodType(ContextBoundParamName(_) :: _) => true | ||
case _ => false | ||
if sourceVersion.isAtLeast(`3.4`) | ||
&& isContextBoundParams | ||
&& pt.applyKind != ApplyKind.Using | ||
then | ||
def rewriteMsg = Message.rewriteNotice("This code", mversion.patchFrom) | ||
report.errorOrMigrationWarning( | ||
em"""Context bounds will map to context parameters. | ||
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""", | ||
tree.srcPos, mversion) | ||
if mversion.needsPatch then | ||
patch(Span(pt.args.head.span.start), "using ") | ||
end contextBoundParams | ||
|
||
end Migrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//> using options -source 3.4 | ||
|
||
class C[T] | ||
def foo[X: C] = () | ||
|
||
given [T]: C[T] = C[T]() | ||
|
||
def Test = | ||
foo(C[Int]()) // warn | ||
foo(using C[Int]()) // ok |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
anobject
and redefiningTyper.typedAsFunction
asThere was a problem hiding this comment.
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.