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

Unsoundness for singletons? Regression from #16664 #17036

Open
soronpo opened this issue Mar 3, 2023 · 3 comments
Open

Unsoundness for singletons? Regression from #16664 #17036

soronpo opened this issue Mar 3, 2023 · 3 comments

Comments

@soronpo
Copy link
Contributor

soronpo commented Mar 3, 2023

#16664 attempts to fix possible unsoundness issues, but I think it is too limiting.
Consider the following code that relies on singleton type from the parent class parameter.

Compiler version

3.3.1-RC1-bin-20230301-0df5ae2-NIGHTLY

Minimized code

def tuple1[D <: Int with Singleton](arg: D): Tuple1[D] = ???
class Container[T](arg: T)
class Extender[C <: Int with Singleton](val argC: C) extends Container(tuple1(argC)) //error

If we explicitly apply the type argument, this compiles fine:

def tuple1[D <: Int with Singleton](arg: D): Tuple1[D] = ???
class Container[T](arg: T)
class Extender[C <: Int with Singleton](val argC: C) extends Container[Tuple1[C]](tuple1(argC)) //no error

Output

The type of a class parent cannot refer to constructor parameters, but Container[Tuple1[(Extender.this.argC : C)]] refers to argC

Expectation

No error.
Note: Scala 2 also fails in the same situation (with a different error), but I don't see the how unsoundness is formed with singletons.

@soronpo soronpo added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 3, 2023
@sjrd
Copy link
Member

sjrd commented Mar 3, 2023

The error that is reported is definitely correct, given the inferred type. If you explicitly write the type that gets inferred, i.e., Container[Tuple1[argC.type]], it would (should) rightfully complain. The type you wrote explicitly was Container[Tuple1[C]], which does not refer to argC and is therefore fine.

Now the type that does get inferred is clearly not the best one in this specific example. If type inference inferred Container[Tuple1[C]] instead of Container[Tuple1[argC.type]], the original example would compile. The inference is caught between a rock and a hard place: it is instructed to select a singleton type because of the Singleton bound, but it the singleton type it infers is not valid in this context.

@soronpo
Copy link
Contributor Author

soronpo commented Mar 5, 2023

Is it right to say that we need to change the inference mechanism so:

  • for each class argument a that is applied in an extension of a another class, its most precise inferred type will never be a.type

?

@sjrd
Copy link
Member

sjrd commented Mar 5, 2023

Only within the parent class type. It's still possible to soundly use a.type in the body of the class, or even within super constructor arguments.

@szymon-rd szymon-rd added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants