Skip to content

Commit a949dd4

Browse files
committed
Lower precedence of qualified types' with
1 parent 7c39b01 commit a949dd4

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,7 @@ object Parsers {
16531653
* | TypTypeParamClause ‘=>>’ Type
16541654
* | FunParamClause ‘=>>’ Type
16551655
* | MatchType
1656+
* | QualifiedType2 -- under qualifiedTypes
16561657
* | InfixType
16571658
* FunType ::= (MonoFunType | PolyFunType)
16581659
* MonoFunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
@@ -1663,6 +1664,11 @@ object Parsers {
16631664
* | `(' [ FunArgType {`,' FunArgType } ] `)'
16641665
* | '(' [ TypedFunParam {',' TypedFunParam } ')'
16651666
* MatchType ::= InfixType `match` <<< TypeCaseClauses >>>
1667+
* QualifiedType2 ::= InfixType `with` PostfixExprf
1668+
* IntoType ::= [‘into’] IntoTargetType
1669+
* | ‘( IntoType ‘)’
1670+
* IntoTargetType ::= Type
1671+
* | FunTypeArgs (‘=>’ | ‘?=>’) IntoType
16661672
*/
16671673
def typ(inContextBound: Boolean = false): Tree =
16681674
val start = in.offset
@@ -1722,6 +1728,8 @@ object Parsers {
17221728
functionRest(t :: Nil)
17231729
case MATCH =>
17241730
matchType(t)
1731+
case WITH if in.featureEnabled(Feature.qualifiedTypes) =>
1732+
qualifiedTypeShort(t)
17251733
case FORSOME =>
17261734
syntaxError(ExistentialTypesNoLongerSupported())
17271735
t
@@ -1856,6 +1864,7 @@ object Parsers {
18561864
def funParamClauses(): List[List[ValDef]] =
18571865
if in.token == LPAREN then funParamClause() :: funParamClauses() else Nil
18581866

1867+
18591868
/** InfixType ::= RefinedType {id [nl] RefinedType}
18601869
* | RefinedType `^` -- under captureChecking
18611870
*/
@@ -1910,22 +1919,12 @@ object Parsers {
19101919
t
19111920
}
19121921

1913-
/** With qualifiedTypes enabled:
1914-
* WithType ::= AnnotType [`with' PostfixExpr]
1915-
*
1916-
* Otherwise:
1917-
* WithType ::= AnnotType {`with' AnnotType} (deprecated)
1918-
*/
1922+
/** WithType ::= AnnotType {`with' AnnotType} (deprecated)
1923+
*/
19191924
def withType(): Tree = withTypeRest(annotType())
19201925

19211926
def withTypeRest(t: Tree): Tree =
1922-
if in.featureEnabled(Feature.qualifiedTypes) && in.token == WITH then
1923-
if inQualifiedType then t
1924-
else
1925-
in.nextToken()
1926-
val qualifier = postfixExpr()
1927-
QualifiedTypeTree(t, None, qualifier).withSpan(Span(t.span.start, qualifier.span.end))
1928-
else if in.token == WITH then
1927+
if in.token == WITH && !in.featureEnabled(Feature.qualifiedTypes) then
19291928
val withOffset = in.offset
19301929
in.nextToken()
19311930
if in.token == LBRACE || in.token == INDENT then
@@ -2268,6 +2267,17 @@ object Parsers {
22682267
accept(RBRACE)
22692268
QualifiedTypeTree(tp, Some(id), qualifier).withSpan(Span(startOffset, qualifier.span.end))
22702269

2270+
/** `with` PostfixExpr
2271+
*/
2272+
def qualifiedTypeShort(t: Tree): Tree =
2273+
if inQualifiedType then
2274+
t
2275+
else
2276+
accept(WITH)
2277+
val qualifier = postfixExpr()
2278+
QualifiedTypeTree(t, None, qualifier).withSpan(Span(t.span.start, qualifier.span.end))
2279+
2280+
22712281
/** TypeBounds ::= [`>:' TypeBound ] [`<:' TypeBound ]
22722282
* TypeBound ::= Type
22732283
* | CaptureSet -- under captureChecking
@@ -2344,7 +2354,12 @@ object Parsers {
23442354

23452355
def typeDependingOn(location: Location): Tree =
23462356
if location.inParens then typ()
2347-
else if location.inPattern then rejectWildcardType(refinedType())
2357+
else if location.inPattern then
2358+
val t = rejectWildcardType(refinedType())
2359+
if in.featureEnabled(Feature.qualifiedTypes) && in.token == WITH then
2360+
qualifiedTypeShort(t)
2361+
else
2362+
t
23482363
else infixType()
23492364

23502365
/* ----------- EXPRESSIONS ------------------------------------------------ */
@@ -3253,10 +3268,11 @@ object Parsers {
32533268
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
32543269
else Nil
32553270

3256-
/** Pattern1 ::= PatVar `:` RefinedType
3257-
* | [‘-’] integerLiteral `:` RefinedType
3258-
* | [‘-’] floatingPointLiteral `:` RefinedType
3259-
* | Pattern2
3271+
/** Pattern1 ::= PatVar `:` QualifiedType3
3272+
* | [‘-’] integerLiteral `:` QualifiedType3
3273+
* | [‘-’] floatingPointLiteral `:` QualifiedType3
3274+
* | Pattern2
3275+
* QualifiedType3 ::= RefinedType [`with` PostfixExpr]
32603276
*/
32613277
def pattern1(location: Location = Location.InPattern): Tree =
32623278
val p = pattern2(location)

tests/printing/qualified-types.check

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ package example {
6060
Int @qualified[Int]((x3: Int) => x3 < 10)
6161
= ???
6262
val x4:
63-
Int @qualified[Int]((x4: Int) => x4 > 0) &
64-
Int @qualified[Int]((x4: Int) => x4 < 10)
63+
(Int @qualified[Int]((x4: Int) => x4 > 0) & Int) @qualified[
64+
Int @qualified[Int]((x4: Int) => x4 > 0) & Int]((
65+
x4: Int @qualified[Int]((x4: Int) => x4 > 0) & Int) => x4 < 10)
6566
= ???
66-
val x5: Int & String @qualified[String]((x5: String) => false) = ???
67+
val x5:
68+
(Int & String) @qualified[Int & String]((x5: Int & String) => false)
69+
= ???
6770
val x6:
6871
(Int @qualified[Int]((x6: Int) => x6 > 0) & Int) @qualified[
6972
Int @qualified[Int]((x6: Int) => x6 > 0) & Int]((

0 commit comments

Comments
 (0)