@@ -175,9 +175,11 @@ class Objects {
175
175
def instantiate (klass : ClassSymbol , ctor : Symbol , args : List [Value ], source : Tree ): Contextual [Result ] =
176
176
value.instantiate(klass, ctor, args, source) ++ errors
177
177
178
- def ensureAccess (source : Tree ): Contextual [Result ] =
178
+ def ensureAccess (klass : ClassSymbol , source : Tree ): Contextual [Result ] =
179
179
value match
180
- case obj : ObjectRef => obj.access(source) ++ errors
180
+ case obj : ObjectRef =>
181
+ if obj.klass == klass then this
182
+ else obj.access(source) ++ errors
181
183
case _ => this
182
184
}
183
185
@@ -528,7 +530,7 @@ class Objects {
528
530
val fun = Fun (arg.tree, Nil , thisV, klass, env)
529
531
Result (fun, Nil )
530
532
else
531
- eval(arg.tree, thisV, klass).ensureAccess(arg.tree)
533
+ eval(arg.tree, thisV, klass)
532
534
}
533
535
534
536
/** Handles the evaluation of different expressions
@@ -584,7 +586,9 @@ class Objects {
584
586
res.call(id.symbol, argsValues, superType = NoType , source = expr)
585
587
586
588
case Select (qualifier, name) =>
587
- eval(qualifier, thisV, klass).select(expr.symbol, expr)
589
+ val sym = expr.symbol
590
+ if sym.isStaticObjectRef then Result (ObjectRef (sym.moduleClass.asClass), Nil ).ensureAccess(klass, expr)
591
+ else eval(qualifier, thisV, klass).select(expr.symbol, expr)
588
592
589
593
case _ : This =>
590
594
cases(expr.tpe, thisV, klass, expr)
@@ -696,8 +700,13 @@ class Objects {
696
700
throw new Exception (" unexpected tree: " + expr.show)
697
701
}
698
702
699
- /** Handle semantics of leaf nodes */
700
- def cases (tp : Type , thisV : Value , klass : ClassSymbol , source : Tree ): Contextual [Result ] = log(" evaluating " + tp.show, printer, res => res.asInstanceOf [Result ].show) {
703
+ /** Handle semantics of leaf nodes
704
+ *
705
+ * @param elideObjectAccess Whether object access should be omitted
706
+ *
707
+ * It happens when the object access is used as a prefix in `new o.C`
708
+ */
709
+ def cases (tp : Type , thisV : Value , klass : ClassSymbol , source : Tree , elideObjectAccess : Boolean = false ): Contextual [Result ] = log(" evaluating " + tp.show, printer, res => res.asInstanceOf [Result ].show) {
701
710
tp match {
702
711
case _ : ConstantType =>
703
712
Result (Bottom , Errors .empty)
@@ -720,15 +729,17 @@ class Objects {
720
729
case tmref : TermRef =>
721
730
val sym = tmref.symbol
722
731
if sym.isStaticObjectRef then
723
- Result (ObjectRef (sym.moduleClass.asClass), Nil )
732
+ val res = Result (ObjectRef (sym.moduleClass.asClass), Nil )
733
+ if elideObjectAccess then res else res.ensureAccess(klass, source)
724
734
else
725
735
cases(tmref.prefix, thisV, klass, source).select(tmref.symbol, source)
726
736
727
737
case tp @ ThisType (tref) =>
728
738
val sym = tref.symbol
729
739
if sym.is(Flags .Package ) then Result (Bottom , Errors .empty)
730
740
else if sym.isStaticObjectRef && sym != klass then
731
- Result (ObjectRef (sym.moduleClass.asClass), Nil )
741
+ val res = Result (ObjectRef (sym.moduleClass.asClass), Nil )
742
+ if elideObjectAccess then res else res.ensureAccess(klass, source)
732
743
else
733
744
val value = resolveThis(tref.classSymbol.asClass, thisV, klass, source)
734
745
Result (value, Errors .empty)
@@ -772,7 +783,8 @@ class Objects {
772
783
Result (outerV, Errors .empty)
773
784
else
774
785
if cls.isAllOf(Flags .JavaInterface ) then Result (Bottom , Nil )
775
- else cases(tref.prefix, thisV, klass, source)
786
+ else
787
+ cases(tref.prefix, thisV, klass, source, elideObjectAccess = cls.isStatic)
776
788
777
789
/** Initialize part of an abstract object in `klass` of the inheritance chain */
778
790
def init (tpl : Template , thisV : Addr , klass : ClassSymbol ): Contextual [Result ] = log(" init " + klass.show, printer, res => res.asInstanceOf [Result ].show) {
0 commit comments