diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2016-01-29 17:56:21 -0800 |
---|---|---|
committer | Stefan Zeiger <szeiger@novocode.com> | 2016-03-07 17:27:49 +0100 |
commit | e7e1c77092ec0d64a6ecbb8b920d1b4cca74837f (patch) | |
tree | fe490f451d0960b77045c621d7aa2b462288e119 /spec/08-pattern-matching.md | |
parent | a926b840e1e756c0dcd936499583ed19d357b97b (diff) | |
download | scala-e7e1c77092ec0d64a6ecbb8b920d1b4cca74837f.tar.gz scala-e7e1c77092ec0d64a6ecbb8b920d1b4cca74837f.tar.bz2 scala-e7e1c77092ec0d64a6ecbb8b920d1b4cca74837f.zip |
Improved outer ref checking in pattern matches:
The old algorithm omitted necessary outer ref checks in some places.
This new one is more conservative. It only omits outer ref checks when
the expected type and the scrutinee type match up, or when the expected
type is defined in a static location. For this specific purpose the top
level of a method or other code block (which is not a trait or class
definition) is also considered static because it does not have a prefix.
This change comes with a spec update to clarify the prefix rule for type
patterns. The new wording makes it clear that the presence of a prefix
is to be interpreted in a *semantic* way, i.e. the existence of a prefix
determines the necessity for an outer ref check, no matter if the prefix
is actually spelled out *syntactically*. Note that the old outer ref
check implementation did not use the alternative interpretation of
requiring prefixes to be given syntactically. It never created an outer
ref check for a local class `C`, no matter if the pattern was `_: C`
or `_: this.C`, thus violating both interpretations of the spec.
There is now explicit support for unchecked matches (like
`case _: (T @unchecked) =>`) to suppress warnings for unchecked outer
refs. `@unchecked` worked before and was used for this purpose in
`neg/t7721` but never actually existed as a feature. It was a result of
a bug that prevented an outer ref check from being generated in the
first place if *any* annotation was used on an expected type in a type
pattern. This new version will still generate the outer ref check if an
outer ref is available but suppress the warning otherwise. Other
annotations on type patterns are ignored.
New tests are in `neg/outer-ref-checks`. The expected results of tests
`neg/t7171` and `neg/t7171b` have changed because the compiler now
tries to generate additional outer ref checks that were not present
before (which was a bug).
Diffstat (limited to 'spec/08-pattern-matching.md')
-rw-r--r-- | spec/08-pattern-matching.md | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/spec/08-pattern-matching.md b/spec/08-pattern-matching.md index d496388a91..7e48947639 100644 --- a/spec/08-pattern-matching.md +++ b/spec/08-pattern-matching.md @@ -328,10 +328,12 @@ A type pattern $T$ is of one of the following forms: * A reference to a class $C$, $p.C$, or `$T$#$C$`. This type pattern matches any non-null instance of the given class. - Note that the prefix of the class, if it is given, is relevant for determining + Note that the prefix of the class, if it exists, is relevant for determining class instances. For instance, the pattern $p.C$ matches only instances of classes $C$ which were created with the path $p$ as - prefix. + prefix. This also applies to prefixes which are not given syntactically. + For example, if $C$ refers to a class defined in the nearest enclosing + class and is thus equivalent to $this.C$, it is considered to have a prefix. The bottom types `scala.Nothing` and `scala.Null` cannot be used as type patterns, because they would match nothing in any case. |