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

More accurate outer checks in patterns #8261

Closed
wants to merge 1 commit into from

Conversation

retronym
Copy link
Member

@retronym retronym commented Jul 25, 2019

Avoids eliding outer checks that matter (run/t11534b.scala) and
avoids emitting checks that don't (pos/t11534.scala) which avoids
compiler warnings when the tested class doesn't have an outer
field.

The latter stops the annoying unchecked warning that appeared since
a recent refactoring made TermName a final class.

Fixes scala/bug#11534

@retronym
Copy link
Member Author

Pushed another version to try to walk the tightrope between pos/t11534 and run/reflect-priv-ctor.scala. Marking as WIP as this isn't super well thought through...

@retronym retronym added the WIP label Jul 25, 2019
@eed3si9n
Copy link
Member

@retronym Would this make 2.12.9?

@retronym
Copy link
Member Author

@eed3si9n No, I'd rather not rush this one.

@retronym retronym modified the milestones: 2.12.9, 2.12.10 Jul 31, 2019
@xerial xerial mentioned this pull request Aug 5, 2019
1 task
@SethTisue SethTisue modified the milestones: 2.12.10, 2.12.11 Sep 3, 2019
@sjrd
Copy link
Member

sjrd commented Sep 30, 2019

Could I remind people about this PR? I still have to compile the Scala.js compiler plugin without -Xfatal-warnings in the latest 2.12.x nightly.

@adriaanm
Copy link
Contributor

adriaanm commented Oct 7, 2019

This PR needs more work before we can merge it. I can't pick it up right now.

@retronym retronym modified the milestones: 2.12.11, 2.12.12 Feb 25, 2020
@SethTisue SethTisue marked this pull request as draft May 8, 2020 02:17
@SethTisue SethTisue removed the WIP label May 8, 2020
@retronym retronym modified the milestones: 2.12.12, 2.12.13 Jun 23, 2020
@dwijnand
Copy link
Member

This PR needs more work before we can merge it. I can't pick it up right now.

@adriaanm / @retronym what's remaining and how terrible would it be if this landed as is? It's looks nice and small, to my amateur eyes.

@dwijnand dwijnand modified the milestones: 2.12.13, 2.12.14 Oct 16, 2020
@dwijnand
Copy link
Member

dwijnand commented Dec 4, 2020

@retronym could you look at this one, please? Even just to get more going on improving it. (I'd like to get rid of the "The outer reference in this type test cannot be checked at run time" exclusion we have in our build.sbt.)

@dwijnand dwijnand self-assigned this Jan 13, 2021
@dwijnand

This comment has been minimized.

@joroKr21
Copy link
Member

I'm not sure what is the reasoning here 🤔

@joroKr21
Copy link
Member

Ideally the compiler would be smart enough to avoid the runtime test in this case as we statically know that the prefixes are the same.

I can't convince myself we know that statically.

@joroKr21
Copy link
Member

joroKr21 commented Feb 8, 2021

Neither B nor C is a subclass of the other.

In that case the conclusion could be no - we can't elide the check.

@retronym

This comment has been minimized.

@joroKr21
Copy link
Member

joroKr21 commented Feb 9, 2021

NVM I see that you already use baseType - I should have checked. However I can't make any sense of prefixAlign2.

@retronym
Copy link
Member Author

retronym commented Feb 9, 2021

However I can't make any sense of prefixAlign2

Sorry, more docs and a good set of tests are still needed here! The idea is as follows.

Given:

(x: a.B) match { case _: a.C }

Where C is subclass of A

Is it possible that a value conforms to a.B and a1.C where a ne a1?

To answer this, I create a new prefix based on a fresh symbol and check the base type of TypeRef(freshPrefix, typePatternSymbol, args) at the binder symbol. If that is prefixed by the fresh symbol, they are statically the same.

@retronym retronym force-pushed the ticket/11534 branch 11 times, most recently from cd6780e to 1115959 Compare February 10, 2021 03:03
@retronym retronym changed the title Improve static elision of outer tests in pattern matcher More accurate outer checks in patterns Feb 10, 2021
@dwijnand
Copy link
Member

@dwijnand Formulating the test this way might make my point a bit clearer:

Thanks. I tried to disprove the assertion with

  trait BI extends o2.B
  class D2 extends o1.B with o1.C with BI

  // expected O(1) as outer of value conforming to pattern `b: o1.B`, but got O(2)
  def main(args: Array[String]): Unit = {
    pat1(new o1.D)
    pat1(new D2)
  }

but it won't let you define such a D2:

outer-checks.scala:16: error: illegal inheritance;
 class D2 inherits different type instances of class B:
Test.o2.B and Test.o1.B
  class D2 extends o1.B with o1.C with BI
        ^

which I think I might've seen recently pop up as not being caught in Scala 3, but I might be confusing with something similar looking.

I'll try to review the patch.

Copy link
Member

@dwijnand dwijnand left a comment

Choose a reason for hiding this comment

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

LGTM, for its results rather than large sections of how it's done. I understand the intent though. Nice tests and nice fix. I left a couple of questions.

@retronym
Copy link
Member Author

@adriaanm Perhaps you'd also like to take a look at where this ended up.

@dwijnand dwijnand marked this pull request as ready for review February 16, 2021 13:55
Avoids eliding outer checks that matter (run/t11534b.scala) and
avoids emitting checks that don't (pos/t11534.scala) which avoids
compiler warnings when the tested class doesn't have an outer
field.

The latter stops the annoying unchecked warning that appeared since
a recent refactoring made `TermName` a final class.
@dwijnand
Copy link
Member

Likely going to avoid the risk in 2.12 and land #9504 instead, for 2.13.

@retronym retronym closed this Feb 18, 2021
@SethTisue SethTisue removed this from the 2.12.14 milestone Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants