Skip to content

Commit

Permalink
7033677: potential cast error in MemberEnter
Browse files Browse the repository at this point in the history
Reviewed-by: vromero, jlahoda
  • Loading branch information
archiecobbs authored and Vicente Romero committed Feb 17, 2023
1 parent 6120319 commit a917fb3
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public Type attribType(JCTree node, TypeSymbol sym) {

public Type attribImportQualifier(JCImport tree, Env<AttrContext> env) {
// Attribute qualifying package or class.
JCFieldAccess s = (JCFieldAccess)tree.qualid;
JCFieldAccess s = tree.qualid;
return attribTree(s.selected, env,
new ResultInfo(tree.staticImport ?
KindSelector.TYP : KindSelector.TYP_PCK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4068,7 +4068,7 @@ public void checkImportsResolvable(final JCCompilationUnit toplevel) {
for (final JCImport imp : toplevel.getImports()) {
if (!imp.staticImport || !imp.qualid.hasTag(SELECT))
continue;
final JCFieldAccess select = (JCFieldAccess) imp.qualid;
final JCFieldAccess select = imp.qualid;
final Symbol origin;
if (select.name == names.asterisk || (origin = TreeInfo.symbol(select.selected)) == null || origin.kind != TYP)
continue;
Expand All @@ -4091,7 +4091,7 @@ public void checkImportsResolvable(final JCCompilationUnit toplevel) {
public void checkImportedPackagesObservable(final JCCompilationUnit toplevel) {
OUTER: for (JCImport imp : toplevel.getImports()) {
if (!imp.staticImport && TreeInfo.name(imp.qualid) == names.asterisk) {
TypeSymbol tsym = ((JCFieldAccess)imp.qualid).selected.type.tsym;
TypeSymbol tsym = imp.qualid.selected.type.tsym;
if (tsym.kind == PCK && tsym.members().isEmpty() &&
!(Feature.IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES.allowedInSource(source) && tsym.exists())) {
log.error(DiagnosticFlag.RESOLVE_ERROR, imp.pos, Errors.DoesntExist(tsym));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ private void resolveImports(JCCompilationUnit tree, Env<AttrContext> env) {
log.error(Errors.NoJavaLang);
throw new Abort();
}
importAll(make.at(tree.pos()).Import(make.QualIdent(javaLang), false), javaLang, env);
importAll(make.at(tree.pos()).Import(make.Select(make.QualIdent(javaLang.owner), javaLang), false),
javaLang, env);

JCModuleDecl decl = tree.getModuleDecl();

Expand Down Expand Up @@ -406,7 +407,7 @@ private void checkClassPackageClash(JCPackageDecl tree) {
}

private void doImport(JCImport tree) {
JCFieldAccess imp = (JCFieldAccess)tree.qualid;
JCFieldAccess imp = tree.qualid;
Name name = TreeInfo.name(imp);

// Create a local environment pointing to this tree to disable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4020,7 +4020,7 @@ protected JCTree importDeclaration() {
}
} while (token.kind == DOT);
accept(SEMI);
return toP(F.at(pos).Import(pid, importStatic));
return toP(F.at(pos).Import((JCFieldAccess)pid, importStatic));
}

/** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,9 @@ public Tag getTag() {
public static class JCImport extends JCTree implements ImportTree {
public boolean staticImport;
/** The imported class(es). */
public JCTree qualid;
public JCFieldAccess qualid;
public com.sun.tools.javac.code.Scope importScope;
protected JCImport(JCTree qualid, boolean importStatic) {
protected JCImport(JCFieldAccess qualid, boolean importStatic) {
this.qualid = qualid;
this.staticImport = importStatic;
}
Expand All @@ -679,7 +679,7 @@ protected JCImport(JCTree qualid, boolean importStatic) {
@DefinedBy(Api.COMPILER_TREE)
public boolean isStatic() { return staticImport; }
@DefinedBy(Api.COMPILER_TREE)
public JCTree getQualifiedIdentifier() { return qualid; }
public JCFieldAccess getQualifiedIdentifier() { return qualid; }

@DefinedBy(Api.COMPILER_TREE)
public Kind getKind() { return Kind.IMPORT; }
Expand Down Expand Up @@ -3403,7 +3403,7 @@ public interface Factory {
JCCompilationUnit TopLevel(List<JCTree> defs);
JCPackageDecl PackageDecl(List<JCAnnotation> annotations,
JCExpression pid);
JCImport Import(JCTree qualid, boolean staticImport);
JCImport Import(JCFieldAccess qualid, boolean staticImport);
JCClassDecl ClassDef(JCModifiers mods,
Name name,
List<JCTypeParameter> typarams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public JCTree visitIf(IfTree node, P p) {
@DefinedBy(Api.COMPILER_TREE)
public JCTree visitImport(ImportTree node, P p) {
JCImport t = (JCImport) node;
JCTree qualid = copy(t.qualid, p);
JCFieldAccess qualid = copy(t.qualid, p);
return M.at(t.pos).Import(qualid, t.staticImport);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public JCPackageDecl PackageDecl(List<JCAnnotation> annotations,
return tree;
}

public JCImport Import(JCTree qualid, boolean importStatic) {
public JCImport Import(JCFieldAccess qualid, boolean importStatic) {
JCImport tree = new JCImport(qualid, importStatic);
tree.pos = pos;
return tree;
Expand Down Expand Up @@ -723,8 +723,8 @@ public JCIdent Ident(Symbol sym) {
/** Create a selection node from a qualifier tree and a symbol.
* @param base The qualifier tree.
*/
public JCExpression Select(JCExpression base, Symbol sym) {
return new JCFieldAccess(base, sym.name, sym).setPos(pos).setType(sym.type);
public JCFieldAccess Select(JCExpression base, Symbol sym) {
return (JCFieldAccess)new JCFieldAccess(base, sym.name, sym).setPos(pos).setType(sym.type);
}

/** Create a qualified identifier from a symbol, adding enough qualifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,15 @@ ListBuffer<JCTree>[] processBases(Element baseTag, HashMap<String, Integer> scop
}
break;
case "import":
imports.append(
make.Import(
make.Ident(names.fromString(element.getTextContent())),
false));
String[] idents = element.getTextContent().split("\\.");
if (idents.length < 2)
throw new IllegalStateException("Invalid import: " + element.getTextContent());
JCFieldAccess select = make.Select(
make.Ident(names.fromString(idents[0])), names.fromString(idents[1]));
for (int j = 2; j < idents.length; j++)
select = make.Select(select, names.fromString(idents[j]));
imports.append(make.Import(select, false));
break;
}
}

Expand Down

1 comment on commit a917fb3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.