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

Take into account the result type of inline implicit conversions unless they are transparent #17924

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Inferencing.*
import ErrorReporting.*
import util.SourceFile
import TypeComparer.necessarySubType
import dotty.tools.dotc.core.Flags.Transparent

import scala.annotation.internal.sharable

Expand Down Expand Up @@ -105,14 +106,14 @@ object ProtoTypes {
if !res then ctx.typerState.constraint = savedConstraint
res

/** Constrain result with special case if `meth` is an inlineable method in an inlineable context.
/** Constrain result with special case if `meth` is a transparent inlineable method in an inlineable context.
* In that case, we should always succeed and not constrain type parameters in the expected type,
* because the actual return type can be a subtype of the currently known return type.
* However, we should constrain parameters of the declared return type. This distinction is
* achieved by replacing expected type parameters with wildcards.
*/
def constrainResult(meth: Symbol, mt: Type, pt: Type)(using Context): Boolean =
if (Inlines.isInlineable(meth)) {
if (Inlines.isInlineable(meth) && meth.is(Transparent)) {
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 am not 100% this is the right place to perform this test. Please let me know if there is a more appropriate place that contains more logic related to transparent definitions.

constrainResult(mt, wildApprox(pt))
true
}
Expand Down
9 changes: 9 additions & 0 deletions tests/neg-macros/i9685bis.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- [E008] Not Found Error: tests/neg-macros/i9685bis.scala:23:4 --------------------------------------------------------
23 | 1.asdf // error
| ^^^^^^
| value asdf is not a member of Int, but could be made available as an extension method.
|
| The following import might make progress towards fixing the problem:
|
| import foo.Baz.toBaz
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that only the transparent inline conversion is suggested.

|
23 changes: 23 additions & 0 deletions tests/neg-macros/i9685bis.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package foo

import scala.language.implicitConversions

class Foo

object Foo:

inline implicit def toFoo(x: Int): Foo = Foo()

class Bar

object Bar:
inline given Conversion[Int, Bar] with
def apply(x: Int): Bar = Bar()

class Baz

object Baz:
transparent inline implicit def toBaz(x: Int): Baz = Baz()

object Usage:
1.asdf // error