Skip to content

Commit 5b3d81b

Browse files
committed
Produce AndTypeTree and OrTypeTree directly in Parsers, not Desugar
Essentially reviewed in #4424.
1 parent b4074b1 commit 5b3d81b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,9 +1130,7 @@ object desugar {
11301130
Apply(Select(Apply(Ident(nme.StringContext), strs), id), elems)
11311131
case InfixOp(l, op, r) =>
11321132
if (ctx.mode is Mode.Type)
1133-
if (!op.isBackquoted && op.name == tpnme.raw.AMP) AndTypeTree(l, r) // l & r
1134-
else if (!op.isBackquoted && op.name == tpnme.raw.BAR) OrTypeTree(l, r) // l | r
1135-
else AppliedTypeTree(op, l :: r :: Nil) // op[l, r]
1133+
AppliedTypeTree(op, l :: r :: Nil) // op[l, r]
11361134
else {
11371135
assert(ctx.mode is Mode.Pattern) // expressions are handled separately by `binop`
11381136
Apply(op, l :: r :: Nil) // op(l, r)

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ object Parsers {
452452
if (isLeftAssoc(op1) != op2LeftAssoc)
453453
syntaxError(MixedLeftAndRightAssociativeOps(op1, op2, op2LeftAssoc), offset)
454454

455-
def reduceStack(base: List[OpInfo], top: Tree, prec: Int, leftAssoc: Boolean, op2: Name): Tree = {
455+
def reduceStack(base: List[OpInfo], top: Tree, prec: Int, leftAssoc: Boolean, op2: Name, isType: Boolean): Tree = {
456456
if (opStack != base && precedence(opStack.head.operator.name) == prec)
457457
checkAssoc(opStack.head.offset, opStack.head.operator.name, op2, leftAssoc)
458458
def recur(top: Tree): Tree = {
@@ -464,7 +464,15 @@ object Parsers {
464464
opStack = opStack.tail
465465
recur {
466466
atPos(opInfo.operator.pos union opInfo.operand.pos union top.pos) {
467-
InfixOp(opInfo.operand, opInfo.operator, top)
467+
val op = opInfo.operator
468+
val l = opInfo.operand
469+
val r = top
470+
if (isType && !op.isBackquoted && op.name == tpnme.raw.BAR) {
471+
OrTypeTree(l, r)
472+
} else if (isType && !op.isBackquoted && op.name == tpnme.raw.AMP) {
473+
AndTypeTree(l, r)
474+
} else
475+
InfixOp(l, op, r)
468476
}
469477
}
470478
}
@@ -488,20 +496,20 @@ object Parsers {
488496
var top = first
489497
while (isIdent && isOperator) {
490498
val op = if (isType) typeIdent() else termIdent()
491-
top = reduceStack(base, top, precedence(op.name), isLeftAssoc(op.name), op.name)
499+
top = reduceStack(base, top, precedence(op.name), isLeftAssoc(op.name), op.name, isType)
492500
opStack = OpInfo(top, op, in.offset) :: opStack
493501
newLineOptWhenFollowing(canStartOperand)
494502
if (maybePostfix && !canStartOperand(in.token)) {
495503
val topInfo = opStack.head
496504
opStack = opStack.tail
497-
val od = reduceStack(base, topInfo.operand, 0, true, in.name)
505+
val od = reduceStack(base, topInfo.operand, 0, true, in.name, isType)
498506
return atPos(startOffset(od), topInfo.offset) {
499507
PostfixOp(od, topInfo.operator)
500508
}
501509
}
502510
top = operand()
503511
}
504-
reduceStack(base, top, 0, true, in.name)
512+
reduceStack(base, top, 0, true, in.name, isType)
505513
}
506514

507515
/* -------- IDENTIFIERS AND LITERALS ------------------------------------------- */

0 commit comments

Comments
 (0)