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

Topic/modifier lockdown #3

Closed
wants to merge 22 commits into from

Conversation

retronym
Copy link
Member

I propose that we make a more concerted effort to keep things final/sealed; this PR does this across the board. It's easy enough to open things up later on, until then we can profit from better pattern match analysis, notification of non-sensical equality tests, and, last but not least, less to think about when reasoning about the code.

As a bonus, the exercise uncovered two compiler bugs: SI-7171 and SI-7172.

The parts that warrant closer review:

  • introduce UntypedTreeMarker in 4598e8c.
  • in the same commit, tree checking for Try, and ignorance of UntypedTrees in TreeTransformer and TreeAccumulator.
  • populate cache vars for lastDenot and _typeParams.
  • seal Type, and therefore move two type nodes from the unpickler to Types.scala

Review by @odersky, if you like we can chat about this on Tuesday.

Notable omissions are ValDef and DefDef, which are
subclassed by the empty variety.

Final classes are easier for the reader to reason about
and enable additional static analysis by the compiler.

As an example of the latter,

scala> trait T
defined trait T

scala> Tuple1(0) match { case _: T => true; case _ => false }
res0: Boolean = false

scala> final case class T1[A](a: A)
defined class T1

scala> T1(0) match { case _: T => true; case _ => false }
<console>:11: error: scrutinee is incompatible with pattern type;
 found   : T
 required: T1[Int]
              T1(0) match { case _: T => true; case _ => false }
This commit exposes a number of inexhaustive matches:

    [warn] /Users/jason/code/dotty/src/dotty/tools/dotc/core/Trees.scala:716: match may not be exhaustive.
    [warn] It would fail on the following inputs: Function(_, _), GenericApply(), ModuleDef(_, _, _), TypedSplice()
    [warn]     def transform(tree: Tree[T], c: C): Tree[T] = tree match {
    [warn]                                                   ^
    [warn] /Users/jason/code/dotty/src/dotty/tools/dotc/core/Trees.scala:870: match may not be exhaustive.
    [warn] It would fail on the following inputs: Function(_, _), GenericApply(), ModuleDef(_, _, _), TypedSplice()
    [warn]     def foldOver(x: T, tree: Tree[U]): T = tree match {
    [warn]                                            ^
    [warn] two warnings found
    [info] Compiling 52 Scala sources to /Users/jason/code/dotty/target/scala-2.10/classes...
    [warn] /Users/jason/code/dotty/src/dotty/tools/dotc/core/PluggableTransformers.scala:80: match may not be exhaustive.
    [warn] It would fail on the following inputs: Alternative(_), AndTypeTree(_, _), Annotated(_, _), AppliedTypeTree(_, _), Assign(_, _), Bind(_, _), Block(_, _), CaseDef(_, _, _), ClassDef(_, _, _, _), DefDef(_, _, _, _, _, _), EmptyTree(), EmptyValDef(), Function(_, _), GenericApply(), If(_, _, _), Import(_, _), Literal(_), Match(_, _), ModuleDef(_, _, _), NamedArg(_, _), New(_), OrTypeTree(_, _), PackageDef(_, _), Pair(_, _), RefineTypeTree(_, _), Return(_, _), SelectFromTypeTree(_, _), SeqLiteral(_, _), Shared(_), SingletonTypeTree(_), Super(_, _), Template(_, _, _), This(_), Throw(_), Tree(), Try(_, _, _), TypeBoundsTree(_, _), TypeDef(_, _, _), TypeTree(_), Typed(_, _), TypedSplice(), UnApply(_, _)
    [warn]     protected def postProcess(tree: Tree[T], old: Tree[T], c: Context, plugins: Plugins): Tree[T] = tree match {
    [warn]                                                                                                     ^
    [warn] /Users/jason/code/dotty/src/dotty/tools/dotc/core/Trees.scala:716: match may not be exhaustive.
    [warn] It would fail on the following inputs: Function(_, _), GenericApply(), ModuleDef(_, _, _), TypedSplice()
    [warn]     def transform(tree: Tree[T], c: C): Tree[T] = tree match {
    [warn]                                                   ^
    [warn] /Users/jason/code/dotty/src/dotty/tools/dotc/core/Trees.scala:870: match may not be exhaustive.
    [warn] It would fail on the following inputs: Function(_, _), GenericApply(), ModuleDef(_, _, _), TypedSplice()
    [warn]     def foldOver(x: T, tree: Tree[U]): T = tree match {
    [warn]                                            ^
    [warn] /Users/jason/code/dotty/src/dotty/tools/dotc/core/TypedTrees.scala:359: match may not be exhaustive.
    [warn] It would fail on the following inputs: EmptyTree(), GenericApply(), Try(_, _, _)
    [warn]   def checkType(tree: tpd.Tree)(implicit ctx: Context): Unit = tree match {
    [warn]
Perhaps controversially seals the hierarchy and
as a consequence moves two types from the unpickler
into Types.scala.
 - Introduce a marker trait for Tree[Nothing] nodes,
   and use this in TreeAccumulator.
 - seal GenericApply
 - Make the match demo plugin transformer @unchecked
 - Add tree checking for Try
 - Match on EmptyTree() rather than tpd.EmptyTree
 - info was calling itself, rather than the field _info
 - tryComplete must interrogate and set the Locked bit through the raw _flags.
@retronym
Copy link
Member Author

I guess this won't merge cleanly any more. Also my checking for Try appears to be wrong, I guess I don't understand exactly what's going on there. Anyway, let's discuss the other changes first, and if you want to incorporate it I'll rebase.

@retronym
Copy link
Member Author

@DarkDimius I'll close this one to as it will be well and truly out of date. But maybe someone will be inspired to hold the torch of privacy/finality/sealedness in dotty for me? You will appreciate it as the code base grows larger.

@retronym
Copy link
Member Author

Oh, and the inliner likes finality, too!

odersky added a commit that referenced this pull request Jul 18, 2019
smarter pushed a commit that referenced this pull request Mar 12, 2020
romanowski referenced this pull request in romanowski/dotty Oct 23, 2020
KacperFKorban referenced this pull request in KacperFKorban/dotty Mar 25, 2021
…debug

Add returning real line of error in source file of snippet for snippet scaladoc compiler
smarter pushed a commit to smarter/dotty that referenced this pull request Apr 28, 2023
odersky added a commit that referenced this pull request Dec 23, 2023
`given ... with` or `given ... = new { ... }` kinds of definitions now follow
the old rules. This allows recursive `given...with` definitions as they are
found in protoQuill.

We still have the old check in a later phase against directly recursive methods.
Of the three loops in the original i15474 we now detect #2 and #3 with new new
restrictions. #1 slips through since it is a loop involving a `given...with` instance
of `Conversion`, but is caught later with the recursive method check.

Previously tests #1 and #3 were detected with the recursive methods check and #2 slipped
through altogether.

The new rules are enough for defining simple givens with `=` without fear of looping.
Kordyjan pushed a commit that referenced this pull request Jan 18, 2024
`given ... with` or `given ... = new { ... }` kinds of definitions now follow
the old rules. This allows recursive `given...with` definitions as they are
found in protoQuill.

We still have the old check in a later phase against directly recursive methods.
Of the three loops in the original i15474 we now detect #2 and #3 with new new
restrictions. #1 slips through since it is a loop involving a `given...with` instance
of `Conversion`, but is caught later with the recursive method check.

Previously tests #1 and #3 were detected with the recursive methods check and #2 slipped
through altogether.

The new rules are enough for defining simple givens with `=` without fear of looping.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants