User account creation filtered due to spam.

Bug 8618 - call to private constructor allowed for anonymous inner class
Summary: call to private constructor allowed for anonymous inner class
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: java (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: 13607
  Show dependency treegraph
 
Reported: 2002-11-17 20:06 UTC by jmr
Modified: 2004-07-10 05:44 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-05-20 23:48:57


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jmr 2002-11-17 20:06:01 UTC
Calls to private constructors are (incorrectly) allowed when  creating an anonymous inner class.

; cat X.java
public class X {
    public static void main(String[] args) {
        // new Y();
        new Y() { };  // should be illegal
    }
}

; cat Y.java
public class Y {
    private Y() {
    }
}

; gcj X.java Y.java --main=X

gcj doesn't give an error, whereas Sun's javac does

; javac X.java
X.java:4: Y() has private access in Y
        new Y() { };
        ^
X.java:4: Y() has private access in Y
        new Y() { };
                ^
2 errors

If you switch around which line is commented out in X.main, then gcj correctly
gives an error:

gcj X.java Y.java --main=X
X.java:3: Can't access private constructor `Y.<init>' from `X'.
           new Y();
               ^
1 error

Release:
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
Comment 1 Andrew Pinski 2003-05-25 23:06:35 UTC
Confirmed bug on the mainline (20030525).
Even swaping them now does not work.
tin:~/src/gnu/gcctest/pr8614>gcj X.java Y.java --main=X
tin:~/src/gnu/gcctest/pr8614>gcj Y.java X.java --main=X
tin:~/src/gnu/gcctest/pr8614>javac *.java
X.java:5: Y() has private access in Y
        new Y() { };  // should be illegal
        ^
X.java:5: Y() has private access in Y
        new Y() { };  // should be illegal
                ^
2 errors
Comment 2 CVS Commits 2004-07-10 05:38:21 UTC
Subject: Bug 8618

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	bryce@gcc.gnu.org	2004-07-10 05:38:16

Modified files:
	gcc/java       : ChangeLog class.c java-tree.h jcf-write.c 
	                 parse.h parse.y 

Log message:
	2004-07-09  Bryce McKinlay  <mckinlay@redhat.com>
	
	PR java/8618
	* parse.y (create_anonymous_class): Remove 'location' argument. Use
	the WFL from TYPE_NAME to get line number for the decl. Fix comment.
	(craft_constructor): Inherit access flags for implicit constructor
	from the enclosing class.
	(create_class): Fix comment typo.
	(resolve_qualified_expression_name): Pass type of qualifier to
	not_accessible_p, not the type in which target field was found.
	(not_accessible_p): Handle inner classes. Expand protected
	qualifier-subtype check to enclosing instances, but don't apply this
	check to static members. Allow protected access to inner classes
	of a subtype. Allow private access within common enclosing context.
	(build_super_invocation): Get WFL line number info from current
	class decl.
	(build_incomplete_class_ref): Update for new create_anonymous_class
	signature.
	* parse.h (INNER_ENCLOSING_SCOPE_CHECK): Use
	common_enclosing_instance_p.
	* class.c (common_enclosing_context_p): New. Determine if types
	share a common enclosing context, even across static contexts.
	(common_enclosing_instance_p): Renamed from
	common_enclosing_context_p. Determines if types share a common
	non-static enclosing instance.
	* java-tree.h (common_enclosing_instance_p): Declare.
	* jcf-write.c (get_method_access_flags): New. Surpress private flag
	for inner class constructors.
	(generate_classfile): Use get_method_access_flags.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/ChangeLog.diff?cvsroot=gcc&r1=1.1408&r2=1.1409
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/class.c.diff?cvsroot=gcc&r1=1.196&r2=1.197
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/java-tree.h.diff?cvsroot=gcc&r1=1.209&r2=1.210
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/jcf-write.c.diff?cvsroot=gcc&r1=1.150&r2=1.151
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/parse.h.diff?cvsroot=gcc&r1=1.97&r2=1.98
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/parse.y.diff?cvsroot=gcc&r1=1.490&r2=1.491

Comment 3 Bryce McKinlay 2004-07-10 05:44:42 UTC
Fix checked in to HEAD.