src/share/classes/com/sun/tools/javac/comp/Attr.java

Print this page

        

*** 195,205 **** * @param pt The expected type (or: prototype) of the tree */ Type check(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) { if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) { if ((ownkind & ~pkind) == 0) { ! owntype = chk.checkType(tree.pos(), owntype, pt); } else { log.error(tree.pos(), "unexpected.type", kindNames(pkind), kindName(ownkind)); owntype = types.createErrorType(owntype); --- 195,205 ---- * @param pt The expected type (or: prototype) of the tree */ Type check(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) { if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) { if ((ownkind & ~pkind) == 0) { ! owntype = chk.checkType(tree.pos(), owntype, pt, errKey); } else { log.error(tree.pos(), "unexpected.type", kindNames(pkind), kindName(ownkind)); owntype = types.createErrorType(owntype);
*** 242,254 **** --- 242,258 ---- ((v.flags() & HASINIT) != 0 || !((base == null || (base.getTag() == JCTree.IDENT && TreeInfo.name(base) == names._this)) && isAssignableAsBlankFinal(v, env)))) { + if (types.asSuper(v.type, syms.autoCloseableType.tsym) != null) { //ARM resource + log.error(pos, "arm.resource.may.not.be.assigned", v); + } else { log.error(pos, "cant.assign.val.to.final.var", v); } } + } /** Does tree represent a static reference to an identifier? * It is assumed that tree is either a SELECT or an IDENT. * We have to weed out selects from non-type names here. * @param tree The candidate tree.
*** 375,384 **** --- 379,392 ---- /** Visitor argument: the currently expected proto-type. */ Type pt; + /** Visitor argument: the error key to be generated when a type error occurs + */ + String errKey; + /** Visitor result: the computed type. */ Type result; /** Visitor method: attribute a tree, catching any completion failure
*** 388,404 **** --- 396,418 ---- * @param env The environment visitor argument. * @param pkind The protokind visitor argument. * @param pt The prototype visitor argument. */ Type attribTree(JCTree tree, Env<AttrContext> env, int pkind, Type pt) { + return attribTree(tree, env, pkind, pt, "incompatible.types"); + } + + Type attribTree(JCTree tree, Env<AttrContext> env, int pkind, Type pt, String errKey) { Env<AttrContext> prevEnv = this.env; int prevPkind = this.pkind; Type prevPt = this.pt; + String prevErrKey = this.errKey; try { this.env = env; this.pkind = pkind; this.pt = pt; + this.errKey = errKey; tree.accept(this); if (tree == breakTree) throw new BreakAttr(env); return result; } catch (CompletionFailure ex) {
*** 406,424 **** --- 420,443 ---- return chk.completionError(tree.pos(), ex); } finally { this.env = prevEnv; this.pkind = prevPkind; this.pt = prevPt; + this.errKey = prevErrKey; } } /** Derived visitor method: attribute an expression tree. */ public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) { return attribTree(tree, env, VAL, pt.tag != ERROR ? pt : Type.noType); } + public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt, String key) { + return attribTree(tree, env, VAL, pt.tag != ERROR ? pt : Type.noType, key); + } + /** Derived visitor method: attribute an expression tree with * no constraints on the computed type. */ Type attribExpr(JCTree tree, Env<AttrContext> env) { return attribTree(tree, env, VAL, Type.noType);
*** 979,996 **** attribStat(tree.body, env); result = null; } public void visitTry(JCTry tree) { // Attribute body ! attribStat(tree.body, env.dup(tree, env.info.dup())); // Attribute catch clauses for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { JCCatch c = l.head; Env<AttrContext> catchEnv = ! env.dup(c, env.info.dup(env.info.scope.dup())); Type ctype = attribStat(c.param, catchEnv); if (TreeInfo.isMultiCatch(c)) { //check that multi-catch parameter is marked as final if ((c.param.sym.flags() & FINAL) == 0) { log.error(c.param.pos(), "multicatch.param.must.be.final", c.param.sym); --- 998,1036 ---- attribStat(tree.body, env); result = null; } public void visitTry(JCTry tree) { + // Create a new local environment with a local scope. + Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup())); + // Attribute resource declarations + ListBuffer<VarSymbol> resourceVars = ListBuffer.lb(); + for (JCTree resource : tree.resources) { + if (resource.getTag() == JCTree.VARDEF) { + attribStat(resource, localEnv); + chk.checkType(resource, resource.type, syms.autoCloseableType, "arm.not.applicable.to.type"); + VarSymbol var = (VarSymbol)TreeInfo.symbolFor(resource); + var.setData(ElementKind.RESOURCE_VARIABLE); + resourceVars.append(var); + } else { + attribExpr(resource, localEnv, syms.autoCloseableType, "arm.not.applicable.to.type"); + } + } // Attribute body ! attribStat(tree.body, localEnv.dup(tree, localEnv.info.dup())); ! ! //remove arm resource vars from local scope - such variables cannot be ! //accessed from catch/finally clauses ! for (VarSymbol resourceVar : resourceVars) { ! localEnv.info.scope.remove(resourceVar); ! } // Attribute catch clauses for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { JCCatch c = l.head; Env<AttrContext> catchEnv = ! localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup())); Type ctype = attribStat(c.param, catchEnv); if (TreeInfo.isMultiCatch(c)) { //check that multi-catch parameter is marked as final if ((c.param.sym.flags() & FINAL) == 0) { log.error(c.param.pos(), "multicatch.param.must.be.final", c.param.sym);
*** 1006,1016 **** attribStat(c.body, catchEnv); catchEnv.info.scope.leave(); } // Attribute finalizer ! if (tree.finalizer != null) attribStat(tree.finalizer, env); result = null; } public void visitConditional(JCConditional tree) { attribExpr(tree.cond, env, syms.booleanType); --- 1046,1058 ---- attribStat(c.body, catchEnv); catchEnv.info.scope.leave(); } // Attribute finalizer ! if (tree.finalizer != null) attribStat(tree.finalizer, localEnv); ! ! localEnv.info.scope.leave(); result = null; } public void visitConditional(JCConditional tree) { attribExpr(tree.cond, env, syms.booleanType);