diff options
author | sadayapalam <none@none> | 2016-01-07 08:45:19 +0000 |
---|---|---|
committer | sadayapalam <none@none> | 2016-01-07 08:45:19 +0000 |
commit | 6ecdb96dd0a5ccdd9851ea071511029432ed1def (patch) | |
tree | 0277d4b03a509b6133957b9c327809b7d4dd32b4 | |
parent | 09f50d089032ceeddaea4e3b753f0212fecb3d69 (diff) | |
download | jdk8u_langtools-6ecdb96dd0a5ccdd9851ea071511029432ed1def.tar.gz |
8145466: javac: No line numbers in compilation error
Summary: Compiler should not use the syntax tree from enclosing contexts in diagnostics even when the enclosing contexts are consulted for method lookup.
Reviewed-by: mcimadamore
6 files changed, 87 insertions, 15 deletions
diff --git a/src/share/classes/com/sun/tools/javac/comp/AttrContext.java b/src/share/classes/com/sun/tools/javac/comp/AttrContext.java index e9e6224d..0f7cd603 100644 --- a/src/share/classes/com/sun/tools/javac/comp/AttrContext.java +++ b/src/share/classes/com/sun/tools/javac/comp/AttrContext.java @@ -25,6 +25,7 @@ package com.sun.tools.javac.comp; +import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.util.*; import com.sun.tools.javac.code.*; @@ -80,6 +81,13 @@ public class AttrContext { */ Type defaultSuperCallSite = null; + /** Tree that when non null, is to be preferentially used in diagnostics. + * Usually Env<AttrContext>.tree is the tree to be referred to in messages, + * but this may not be true during the window a method is looked up in enclosing + * contexts (JDK-8145466) + */ + JCTree preferredTreeForDiagnostics; + /** Duplicate this context, replacing scope field and copying all others. */ AttrContext dup(Scope scope) { @@ -94,6 +102,7 @@ public class AttrContext { info.returnResult = returnResult; info.defaultSuperCallSite = defaultSuperCallSite; info.isSerializable = isSerializable; + info.preferredTreeForDiagnostics = preferredTreeForDiagnostics; return info; } diff --git a/src/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/share/classes/com/sun/tools/javac/comp/Resolve.java index 953b0423..7b3d3e26 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -717,7 +717,8 @@ public class Resolve { Warner warn) { //should we expand formals? boolean useVarargs = deferredAttrContext.phase.isVarargsRequired(); - List<JCExpression> trees = TreeInfo.args(env.tree); + JCTree callTree = treeForDiagnostics(env); + List<JCExpression> trees = TreeInfo.args(callTree); //inference context used during this method check InferenceContext inferenceContext = deferredAttrContext.inferenceContext; @@ -726,7 +727,7 @@ public class Resolve { if (varargsFormal == null && argtypes.size() != formals.size()) { - reportMC(env.tree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args + reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args } while (argtypes.nonEmpty() && formals.head != varargsFormal) { @@ -738,7 +739,7 @@ public class Resolve { } if (formals.head != varargsFormal) { - reportMC(env.tree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args + reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args } if (useVarargs) { @@ -754,6 +755,11 @@ public class Resolve { } } + // where + private JCTree treeForDiagnostics(Env<AttrContext> env) { + return env.info.preferredTreeForDiagnostics != null ? env.info.preferredTreeForDiagnostics : env.tree; + } + /** * Does the actual argument conforms to the corresponding formal? */ @@ -1828,17 +1834,23 @@ public class Resolve { boolean staticOnly = false; while (env1.outer != null) { if (isStatic(env1)) staticOnly = true; - sym = findMethod( - env1, env1.enclClass.sym.type, name, argtypes, typeargtypes, - allowBoxing, useVarargs, false); - if (sym.exists()) { - if (staticOnly && - sym.kind == MTH && - sym.owner.kind == TYP && - (sym.flags() & STATIC) == 0) return new StaticError(sym); - else return sym; - } else if (sym.kind < bestSoFar.kind) { - bestSoFar = sym; + Assert.check(env1.info.preferredTreeForDiagnostics == null); + env1.info.preferredTreeForDiagnostics = env.tree; + try { + sym = findMethod( + env1, env1.enclClass.sym.type, name, argtypes, typeargtypes, + allowBoxing, useVarargs, false); + if (sym.exists()) { + if (staticOnly && + sym.kind == MTH && + sym.owner.kind == TYP && + (sym.flags() & STATIC) == 0) return new StaticError(sym); + else return sym; + } else if (sym.kind < bestSoFar.kind) { + bestSoFar = sym; + } + } finally { + env1.info.preferredTreeForDiagnostics = null; } if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true; env1 = env1.outer; @@ -4214,7 +4226,11 @@ public class Resolve { DiagnosticPosition preferedPos, DiagnosticSource preferredSource, DiagnosticType preferredKind, JCDiagnostic d) { JCDiagnostic cause = (JCDiagnostic)d.getArgs()[0]; - return diags.create(preferredKind, preferredSource, d.getDiagnosticPosition(), + DiagnosticPosition pos = d.getDiagnosticPosition(); + if (pos == null) { + pos = preferedPos; + } + return diags.create(preferredKind, preferredSource, pos, "prob.found.req", cause); } }); diff --git a/test/tools/javac/diags/DiagnosticRewriterTest.java b/test/tools/javac/diags/DiagnosticRewriterTest.java new file mode 100644 index 00000000..867153f1 --- /dev/null +++ b/test/tools/javac/diags/DiagnosticRewriterTest.java @@ -0,0 +1,18 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8145466 8146533 + * @summary javac: No line numbers in compilation error + * @compile/fail/ref=DiagnosticRewriterTest.out -Xdiags:compact -XDrawDiagnostics DiagnosticRewriterTest.java + */ + +class DiagnosticRewriterTest { + void test() { + new Object() { + void g() { + m(2L); + } + }; + } + + void m(int i) { } +} diff --git a/test/tools/javac/diags/DiagnosticRewriterTest.out b/test/tools/javac/diags/DiagnosticRewriterTest.out new file mode 100644 index 00000000..f66d9663 --- /dev/null +++ b/test/tools/javac/diags/DiagnosticRewriterTest.out @@ -0,0 +1,3 @@ +DiagnosticRewriterTest.java:12:15: compiler.err.prob.found.req: (compiler.misc.possible.loss.of.precision: long, int) +- compiler.note.compressed.diags +1 error diff --git a/test/tools/javac/diags/DiagnosticRewriterTest2.java b/test/tools/javac/diags/DiagnosticRewriterTest2.java new file mode 100644 index 00000000..26ec2fc9 --- /dev/null +++ b/test/tools/javac/diags/DiagnosticRewriterTest2.java @@ -0,0 +1,22 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8145466 8146533 + * @summary javac: No line numbers in compilation error + * @compile/fail/ref=DiagnosticRewriterTest2.out -Xdiags:compact -XDrawDiagnostics DiagnosticRewriterTest2.java + */ + +class DiagnosticRewriterTest2 { + class Bar { + Bar(Object o) { } + } + void test() { + new Bar(null) { + void g() { + m(2L); + m(); + } + }; + } + + void m(int i) { } +} diff --git a/test/tools/javac/diags/DiagnosticRewriterTest2.out b/test/tools/javac/diags/DiagnosticRewriterTest2.out new file mode 100644 index 00000000..3076a6b7 --- /dev/null +++ b/test/tools/javac/diags/DiagnosticRewriterTest2.out @@ -0,0 +1,4 @@ +DiagnosticRewriterTest2.java:15:15: compiler.err.prob.found.req: (compiler.misc.possible.loss.of.precision: long, int) +DiagnosticRewriterTest2.java:16:13: compiler.err.cant.apply.symbol: kindname.method, m, int, compiler.misc.no.args, kindname.class, DiagnosticRewriterTest2, (compiler.misc.arg.length.mismatch) +- compiler.note.compressed.diags +2 errors |