Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8261205: AssertionError: Cannot add metadata to an intersection type #4095

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import javax.lang.model.type.TypeKind;
import javax.tools.JavaFileObject;

import com.sun.tools.javac.code.Attribute.Array;
import com.sun.tools.javac.code.Attribute.TypeCompound;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
Expand Down Expand Up @@ -1262,7 +1261,7 @@ public void visitVarDef(final JCVariableDecl tree) {
final TypeAnnotationPosition pos =
TypeAnnotationPosition.localVariable(currentLambda,
tree.pos);
if (!tree.isImplicitlyTyped()) {
if (!tree.declaredUsingVar()) {
separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
}
} else if (tree.sym.getKind() == ElementKind.BINDING_VARIABLE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import javax.lang.model.element.ElementKind;
import javax.tools.JavaFileObject;
Expand Down Expand Up @@ -854,7 +853,7 @@ public Object attribLazyConstantValue(Env<AttrContext> env,
final JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
try {
Type itype = attribExpr(variable.init, env, type);
if (variable.isImplicitlyTyped()) {
if (variable.nullVarType()) {
//fixup local variable type
type = variable.type = variable.sym.type = chk.checkLocalVarType(variable, itype.baseType(), variable.name);
}
Expand Down Expand Up @@ -1247,7 +1246,7 @@ public void visitVarDef(JCVariableDecl tree) {
// parameters have already been entered
env.info.scope.enter(tree.sym);
} else {
if (tree.isImplicitlyTyped() && (tree.getModifiers().flags & PARAMETER) == 0) {
if (tree.nullVarType() && (tree.getModifiers().flags & PARAMETER) == 0) {
if (tree.init == null) {
//cannot use 'var' without initializer
log.error(tree, Errors.CantInferLocalVarType(tree.name, Fragments.LocalMissingInit));
Expand Down Expand Up @@ -1285,7 +1284,7 @@ public void visitVarDef(JCVariableDecl tree) {
boolean isImplicitLambdaParameter = env.tree.hasTag(LAMBDA) &&
((JCLambda)env.tree).paramKind == JCLambda.ParameterKind.IMPLICIT &&
(tree.sym.flags() & PARAMETER) != 0;
chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.nullVarType());

try {
v.getConstValue(); // ensure compile-time constant initializer is evaluated
Expand All @@ -1306,12 +1305,12 @@ public void visitVarDef(JCVariableDecl tree) {
// marking the variable as undefined.
initEnv.info.enclVar = v;
attribExpr(tree.init, initEnv, v.type);
if (tree.isImplicitlyTyped()) {
if (tree.nullVarType()) {
//fixup local variable type
v.type = chk.checkLocalVarType(tree, tree.init.type.baseType(), tree.name);
}
}
if (tree.isImplicitlyTyped()) {
if (tree.nullVarType()) {
setSyntheticVariableType(tree, v.type);
}
}
Expand Down Expand Up @@ -1547,7 +1546,7 @@ public void visitForeachLoop(JCEnhancedForLoop tree) {
}
}
}
if (tree.var.isImplicitlyTyped()) {
if (tree.var.nullVarType()) {
Type inferredType = chk.checkLocalVarType(tree.var, elemtype, tree.var.name);
setSyntheticVariableType(tree.var, inferredType);
}
Expand Down Expand Up @@ -3030,7 +3029,7 @@ public void visitLambda(final JCLambda that) {
Type argType = arityMismatch ?
syms.errType :
actuals.head;
if (params.head.isImplicitlyTyped()) {
if (params.head.nullVarType()) {
setSyntheticVariableType(params.head, argType);
}
params.head.sym = null;
Expand Down Expand Up @@ -5522,7 +5521,7 @@ public void visitMethodDef(JCMethodDecl tree) {
}
public void visitVarDef(final JCVariableDecl tree) {
//System.err.println("validateTypeAnnotations.visitVarDef " + tree);
if (tree.sym != null && tree.sym.type != null && !tree.isImplicitlyTyped())
if (tree.sym != null && tree.sym.type != null && !tree.nullVarType())
validateAnnotatedType(tree.vartype, tree.sym.type);
scan(tree.mods);
scan(tree.vartype);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Kinds.Kind.*;
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
import static com.sun.tools.javac.tree.JCTree.Tag.VARDEF;
Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the import removal changes have been introduced by intelliJ


/** Resolves field, method and constructor header, and constructs corresponding Symbols.
*
Expand Down Expand Up @@ -262,7 +261,7 @@ public void visitVarDef(JCVariableDecl tree) {
try {
if (TreeInfo.isEnumInit(tree)) {
attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype);
} else if (!tree.isImplicitlyTyped()) {
} else if (!tree.nullVarType()) {
attr.attribType(tree.vartype, localEnv);
if (TreeInfo.isReceiverParam(tree))
checkReceiver(tree, localEnv);
Expand All @@ -282,7 +281,7 @@ public void visitVarDef(JCVariableDecl tree) {
tree.vartype.type = atype.makeVarargs();
}
WriteableScope enclScope = enter.enterScope(env);
Type vartype = tree.isImplicitlyTyped()
Type vartype = tree.nullVarType()
? env.info.scope.owner.kind == MTH ? Type.noType : syms.errType
: tree.vartype.type;
VarSymbol v = new VarSymbol(0, tree.name, vartype, enclScope.owner);
Expand All @@ -306,7 +305,7 @@ public void visitVarDef(JCVariableDecl tree) {
}

annotate.annotateLater(tree.mods.annotations, localEnv, v, tree.pos());
if (!tree.isImplicitlyTyped()) {
if (!tree.nullVarType()) {
annotate.queueScanTreeAndTypeAnnotate(tree.vartype, localEnv, v, tree.pos());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,8 @@ public static class JCVariableDecl extends JCStatement implements VariableTree {
public VarSymbol sym;
/** explicit start pos */
public int startPos = Position.NOPOS;
/** declared using `var` */
private boolean declaredUsingVar;

protected JCVariableDecl(JCModifiers mods,
Name name,
Expand All @@ -968,6 +970,7 @@ protected JCVariableDecl(JCModifiers mods,
this.vartype = vartype;
this.init = init;
this.sym = sym;
this.declaredUsingVar = vartype == null;
}

protected JCVariableDecl(JCModifiers mods,
Expand All @@ -981,12 +984,17 @@ protected JCVariableDecl(JCModifiers mods,
// Only other option is qualified name x.y.this;
this.name = ((JCFieldAccess)nameexpr).name;
}
this.declaredUsingVar = vartype == null;
}

public boolean isImplicitlyTyped() {
public boolean nullVarType() {
return vartype == null;
}

public boolean declaredUsingVar() {
return declaredUsingVar;
}

@Override
public void accept(Visitor v) { v.visitVarDef(this); }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8261205
* @summary assertion error: cannot add metadata to an intersection type
* @compile AnnotationOnInferredIntersectionType.java
*/

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

class AnnotationOnInferredIntersectionType {
@Target({ElementType.TYPE_USE, ElementType.LOCAL_VARIABLE})
@interface A {}

class Test {
void t() {
@A var c = g(1, 1L);
}

<X> X g(X a, X b) {
return a;
}
}
}