Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc646f90

Browse files
committed
Move right-associative rewrite into typer
Parser marks application as right-associative.Typer identifies subexpressions to pre-computein order to preserve left-to-right evaluation.
1 parenta9078fb commitc646f90

File tree

25 files changed

+400
-169
lines changed

25 files changed

+400
-169
lines changed

‎src/compiler/scala/tools/nsc/ast/parser/Parsers.scala

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ self =>
930930
case _=> t
931931
}
932932

933-
/** Create tree representing (unencoded) binary operation expression or pattern.*/
933+
/** Create tree representing (unencoded) binary operation expression or pattern.Pos set by caller.*/
934934
defmakeBinop(isExpr:Boolean,left:Tree,op:TermName,right:Tree,opPos:Position,targs:List[Tree]=Nil):Tree= {
935935
require(isExpr|| targs.isEmpty|| targs.exists(_.isErroneous),
936936
s"Incompatible args to makeBinop: !isExpr but targs=$targs")
@@ -941,42 +941,35 @@ self =>
941941
valpos= (opPos union t.pos) makeTransparentIf rightAssoc
942942
valsel= atPos(pos)(Select(stripParens(t), op.encode))
943943
if (targs.isEmpty) sel
944-
else {
945-
/* if it's right-associative, `targs` are between `op` and `t` so make the pos transparent*/
944+
else
945+
// if it's right-associative,(deprecated)`targs` are between `op` and `t` so make the pos transparent
946946
atPos((pos union targs.last.pos) makeTransparentIf rightAssoc) {
947947
TypeApply(sel, targs)
948948
}
949-
}
950949
}
951950
defmkNamed(args:List[Tree])=if (!isExpr) argselse
952951
args.map(treeInfo.assignmentToMaybeNamedArg(_))
953952
.tap(res=>if (currentRun.isScala3&& args.lengthCompare(1)==0&& (args.head ne res.head))
954953
deprecationWarning(args.head.pos.point,"named argument is deprecated for infix syntax", since="2.13.16"))
955954
varisMultiarg=false
956-
valarguments= rightmatch {
955+
defarguments(arg:Tree)= argmatch {
957956
caseParens(Nil)=> literalUnit::Nil
958957
caseParens(args@ (_::Nil))=> mkNamed(args)
959958
caseParens(args)=> isMultiarg=true ; mkNamed(args)
960-
case _=>right::Nil
959+
case _=>arg::Nil
961960
}
962961
defmkApply(fun:Tree,args:List[Tree])= {
963962
valapply=Apply(fun, args).updateAttachment(InfixAttachment)
964963
if (isMultiarg) apply.updateAttachment(MultiargInfixAttachment)
965964
apply
966965
}
967-
if (isExpr) {
968-
if (rightAssoc) {
969-
importsymtab.Flags._
970-
valx= freshTermName(nme.RIGHT_ASSOC_OP_PREFIX)
971-
valliftedArg= atPos(left.pos) {
972-
ValDef(Modifiers(FINAL|SYNTHETIC|ARTIFACT), x,TypeTree(), stripParens(left))
973-
}
974-
valapply= mkApply(mkSelection(right),List(Ident(x) setPos left.pos.focus))
975-
Block(liftedArg::Nil, apply)
976-
}else
977-
mkApply(mkSelection(left), arguments)
978-
}else
979-
mkApply(Ident(op.encode), stripParens(left):: arguments)
966+
if (isExpr)
967+
if (rightAssoc)
968+
mkApply(mkSelection(right), arguments(left)).updateAttachment(RightAssociative)
969+
else
970+
mkApply(mkSelection(left), arguments(right))
971+
else
972+
mkApply(Ident(op.encode), stripParens(left):: arguments(right))
980973
}
981974

982975
/** Is current ident a `*`, and is it followed by a `)` or `, )`?*/
@@ -1014,10 +1007,9 @@ self =>
10141007
}
10151008

10161009
defcheckHeadAssoc(leftAssoc:Boolean)= checkAssoc(opHead.offset, opHead.operator, leftAssoc)
1017-
defcheckAssoc(offset:Offset,op:Name,leftAssoc:Boolean)= (
1010+
defcheckAssoc(offset:Offset,op:Name,leftAssoc:Boolean)=
10181011
if (nme.isLeftAssoc(op)!= leftAssoc)
10191012
syntaxError(offset,"left- and right-associative operators with same precedence may not be mixed", skipIt=false)
1020-
)
10211013

10221014
deffinishPostfixOp(start:Int,base:List[OpInfo],opinfo:OpInfo):Tree= {
10231015
if (opinfo.targs.nonEmpty)
@@ -1047,7 +1039,7 @@ self =>
10471039

10481040
defreduceStack(isExpr:Boolean,base:List[OpInfo],top:Tree):Tree= {
10491041
valopPrecedence=if (isIdent)Precedence(in.name.toString)elsePrecedence(0)
1050-
valleftAssoc=!isIdent||(nmeisLeftAssocin.name)
1042+
valleftAssoc=!isIdent|| nme.isLeftAssoc(in.name)
10511043

10521044
reduceStack(isExpr, base, top, opPrecedence, leftAssoc)
10531045
}

‎src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 110 additions & 90 deletions
Large diffs are not rendered by default.

‎src/partest/scala/tools/partest/TestState.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
packagescala.tools.partest
1414

15+
importscala.tools.nsc.util._
16+
1517
sealedabstractclassTestState {
1618
deftestFile: java.io.File
1719
defwhat:String
@@ -67,7 +69,7 @@ object TestState {
6769
}
6870
caseclassCrash(testFile: java.io.File,caught:Throwable,transcript:Array[String])extendsTestState {
6971
defwhat="crash"
70-
defreason=s"caught$caught_s -${caught.getMessage}"
72+
defreason=s"caught$caught_s -${caught.getMessage}:${caught.stackTracePrefixString(_=>true)}"
7173
overridedefshortStatus="?!"
7274

7375
privatedefcaught_s= (caught.getClass.getName split'.').last

‎src/reflect/scala/reflect/internal/StdAttachments.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,9 @@ trait StdAttachments {
183183
caseobjectDiscardedExprextendsPlainAttachment
184184
/** Anonymous parameter of `if (_)` may be inferred as Boolean.*/
185185
caseobjectBooleanParameterTypeextendsPlainAttachment
186+
187+
/** Apply is right associative.*/
188+
caseobjectRightAssociativeextendsPlainAttachment
189+
/** Arg to right associative infix application is candidate for rewriting.*/
190+
caseobjectRightAssociativeArgextendsPlainAttachment
186191
}

‎src/reflect/scala/reflect/internal/Types.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import scala.util.chaining._
7171
// [tparams]result where result is a (Nullary)MethodType or ClassInfoType
7272
7373
// The remaining types are not used after phase `typer`.
74-
case OverloadedType(pre,tparams,alts) =>
74+
case OverloadedType(pre, alts) =>
7575
// all alternatives of an overloaded ident
7676
case AntiPolyType(pre, targs) =>
7777
// rarely used, disappears when combined with a PolyType

‎src/reflect/scala/reflect/internal/util/package.scala

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@
1010
* additional information regarding copyright ownership.
1111
*/
1212

13-
packagescala
14-
packagereflect
15-
packageinternal
16-
17-
importscala.language.existentials// scala/bug#6541
13+
packagescala.reflect.internal
1814

1915
packageobjectutil {
2016

2117
// An allocation-avoiding reusable instance of the so-common List(Nil).
2218
valListOfNil:List[List[Nothing]]=Nil::Nil
2319
valSomeOfNil:Option[List[Nothing]]=Some(Nil)
2420

25-
defandFalse(body:Unit):Boolean=false
26-
2721
// Shorten a name like Symbols$FooSymbol to FooSymbol.
2822
privatedefshortenName(name:String):String= {
2923
if (name=="")return""
@@ -45,7 +39,7 @@ package object util {
4539
if (isModule)
4640
(name split'$' filterNot (_=="")).last+"$"
4741
elseif (isAnon)
48-
clazz.getSuperclass:: clazz.getInterfaces.toListmap (c=>shortClass(c))mkString" with"
42+
clazz.getInterfaces.toList.prepended(clazz.getSuperclass).map(shortClass).mkString(" with")
4943
else
5044
shortenName(name)
5145
}

‎src/reflect/scala/reflect/runtime/JavaUniverseForce.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ trait JavaUniverseForce { self: runtime.JavaUniverse =>
9191
this.DiscardedValue
9292
this.DiscardedExpr
9393
this.BooleanParameterType
94+
this.RightAssociative
95+
this.RightAssociativeArg
9496
this.noPrint
9597
this.typeDebug
9698
// inaccessible: this.posAssigner

‎test/files/pos/t11467.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//>usingoptions-Werror-Wunused
2+
3+
//import language.existentials
4+
5+
traitOtherType[T]
6+
caseclassExistensialism(field:OtherType[_])
7+
8+
classC {
9+
deff(clazz:Class[_])= clazz.getSuperclass:: clazz.getInterfaces.toList
10+
}

‎test/files/pos/t1980.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// regression test from LazyListTest
2+
//
3+
classL {valll:LazyList[Nothing]=LazyList.empty#::: ll }
4+
5+
classM {
6+
defarg:LazyList[Int]=LazyList.empty[Int]
7+
defll:LazyList[Int]= arg#::: ll
8+
}
9+
10+
/* was
11+
private[this] val ll: scala.collection.immutable.LazyList[Nothing] = {
12+
final <synthetic> <artifact> val rassoc$1: scala.collection.immutable.LazyList[A] = LazyList.empty;
13+
immutable.this.LazyList.toDeferrer[Nothing](L.this.ll).#:::[Nothing](rassoc$1[Nothing])
14+
};
15+
*/

‎test/files/pos/t4518.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
importlanguage.existentials
3+
4+
classBroke {
5+
6+
valtempval=newAnyRef {valroleA=newAnyRefwithBar}.roleA
7+
8+
newAnyRef {}-: tempval// when not assigning to anything, no problem
9+
valbroke_val=newAnyRef {}-: tempval// type mismatch error only when assigning
10+
11+
traitFoo[AnyRef] { }
12+
13+
traitBarextendsFoo[AnyRef] {
14+
def-:(core:AnyRef):this.typewithFoo[core.type]=thrownewException()
15+
}
16+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp