提交 336237de 编写于 作者: M mcimadamore

8020147: Spurious errors when compiling nested stuck lambdas

Summary: Scope of deferred types is not copied correctly; postAttr analyzer should not run on stuck expressions
Reviewed-by: jjg
上级 80cf3304
...@@ -481,31 +481,7 @@ public class Attr extends JCTree.Visitor { ...@@ -481,31 +481,7 @@ public class Attr extends JCTree.Visitor {
static final long serialVersionUID = -6924771130405446405L; static final long serialVersionUID = -6924771130405446405L;
private Env<AttrContext> env; private Env<AttrContext> env;
private BreakAttr(Env<AttrContext> env) { private BreakAttr(Env<AttrContext> env) {
this.env = copyEnv(env); this.env = env;
}
private Env<AttrContext> copyEnv(Env<AttrContext> env) {
Env<AttrContext> newEnv =
env.dup(env.tree, env.info.dup(copyScope(env.info.scope)));
if (newEnv.outer != null) {
newEnv.outer = copyEnv(newEnv.outer);
}
return newEnv;
}
private Scope copyScope(Scope sc) {
Scope newScope = new Scope(sc.owner);
List<Symbol> elemsList = List.nil();
while (sc != null) {
for (Scope.Entry e = sc.elems ; e != null ; e = e.sibling) {
elemsList = elemsList.prepend(e.sym);
}
sc = sc.next;
}
for (Symbol s : elemsList) {
newScope.enter(s);
}
return newScope;
} }
} }
...@@ -605,7 +581,7 @@ public class Attr extends JCTree.Visitor { ...@@ -605,7 +581,7 @@ public class Attr extends JCTree.Visitor {
tree.accept(this); tree.accept(this);
if (tree == breakTree && if (tree == breakTree &&
resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) { resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
throw new BreakAttr(env); throw new BreakAttr(copyEnv(env));
} }
return result; return result;
} catch (CompletionFailure ex) { } catch (CompletionFailure ex) {
...@@ -617,6 +593,30 @@ public class Attr extends JCTree.Visitor { ...@@ -617,6 +593,30 @@ public class Attr extends JCTree.Visitor {
} }
} }
Env<AttrContext> copyEnv(Env<AttrContext> env) {
Env<AttrContext> newEnv =
env.dup(env.tree, env.info.dup(copyScope(env.info.scope)));
if (newEnv.outer != null) {
newEnv.outer = copyEnv(newEnv.outer);
}
return newEnv;
}
Scope copyScope(Scope sc) {
Scope newScope = new Scope(sc.owner);
List<Symbol> elemsList = List.nil();
while (sc != null) {
for (Scope.Entry e = sc.elems ; e != null ; e = e.sibling) {
elemsList = elemsList.prepend(e.sym);
}
sc = sc.next;
}
for (Symbol s : elemsList) {
newScope.enter(s);
}
return newScope;
}
/** Derived visitor method: attribute an expression tree. /** Derived visitor method: attribute an expression tree.
*/ */
public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) { public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) {
...@@ -2431,7 +2431,7 @@ public class Attr extends JCTree.Visitor { ...@@ -2431,7 +2431,7 @@ public class Attr extends JCTree.Visitor {
boolean isSpeculativeRound = boolean isSpeculativeRound =
resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE; resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE;
postAttr(that); preFlow(that);
flow.analyzeLambda(env, that, make, isSpeculativeRound); flow.analyzeLambda(env, that, make, isSpeculativeRound);
checkLambdaCompatible(that, lambdaType, resultInfo.checkContext, isSpeculativeRound); checkLambdaCompatible(that, lambdaType, resultInfo.checkContext, isSpeculativeRound);
...@@ -2453,6 +2453,21 @@ public class Attr extends JCTree.Visitor { ...@@ -2453,6 +2453,21 @@ public class Attr extends JCTree.Visitor {
} }
} }
//where //where
void preFlow(JCLambda tree) {
new PostAttrAnalyzer() {
@Override
public void scan(JCTree tree) {
if (tree == null ||
(tree.type != null &&
tree.type == Type.stuckType)) {
//don't touch stuck expressions!
return;
}
super.scan(tree);
}
}.scan(tree);
}
Types.MapVisitor<DiagnosticPosition> targetChecker = new Types.MapVisitor<DiagnosticPosition>() { Types.MapVisitor<DiagnosticPosition> targetChecker = new Types.MapVisitor<DiagnosticPosition>() {
@Override @Override
......
...@@ -127,7 +127,7 @@ public class DeferredAttr extends JCTree.Visitor { ...@@ -127,7 +127,7 @@ public class DeferredAttr extends JCTree.Visitor {
DeferredType(JCExpression tree, Env<AttrContext> env) { DeferredType(JCExpression tree, Env<AttrContext> env) {
super(null); super(null);
this.tree = tree; this.tree = tree;
this.env = env.dup(tree, env.info.dup()); this.env = attr.copyEnv(env);
this.speculativeCache = new SpeculativeCache(); this.speculativeCache = new SpeculativeCache();
} }
...@@ -263,7 +263,7 @@ public class DeferredAttr extends JCTree.Visitor { ...@@ -263,7 +263,7 @@ public class DeferredAttr extends JCTree.Visitor {
DeferredTypeCompleter dummyCompleter = new DeferredTypeCompleter() { DeferredTypeCompleter dummyCompleter = new DeferredTypeCompleter() {
public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
Assert.check(deferredAttrContext.mode == AttrMode.CHECK); Assert.check(deferredAttrContext.mode == AttrMode.CHECK);
return dt.tree.type = Type.noType; return dt.tree.type = Type.stuckType;
} }
}; };
......
...@@ -373,6 +373,15 @@ public class Flow { ...@@ -373,6 +373,15 @@ public class Flow {
boolean resolveBreaks(JCTree tree, ListBuffer<P> oldPendingExits) { boolean resolveBreaks(JCTree tree, ListBuffer<P> oldPendingExits) {
return resolveJump(tree, oldPendingExits, JumpKind.BREAK); return resolveJump(tree, oldPendingExits, JumpKind.BREAK);
} }
@Override
public void scan(JCTree tree) {
if (tree != null && (
tree.type == null ||
tree.type != Type.stuckType)) {
super.scan(tree);
}
}
} }
/** /**
......
/*
* @test /nodynamiccopyright/
* @bug 8020147
* @summary Spurious errors when compiling nested stuck lambdas
* @compile/fail/ref=T8020147.out -Werror -Xlint:cast -XDrawDiagnostics T8020147.java
*/
class T8020147 {
interface Function<X, Y> {
Y apply(X x);
}
<T> void g(Function<String, T> f) { }
<U> String m(U u, Function<U, U> fuu) { return null; }
void test() {
g(x->m("", i->(String)i));
g(x->m("", i->(String)x));
}
}
T8020147.java:16:23: compiler.warn.redundant.cast: java.lang.String
T8020147.java:17:23: compiler.warn.redundant.cast: java.lang.String
- compiler.err.warnings.and.werror
1 error
2 warnings
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册