Bug 16677 - Failure to reject illegal access to enclosing "this" across static context
Summary: Failure to reject illegal access to enclosing "this" across static context
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: java (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on: 28067
Blocks: 13607 18131
  Show dependency treegraph
 
Reported: 2004-07-22 21:34 UTC by Vadim Nasardinov
Modified: 2007-01-09 20:46 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-07-23 05:23:32


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vadim Nasardinov 2004-07-22 21:34:25 UTC
In the process of producing a test case for PR16675, we ran across
another bug.  See below.

Bryce points out that it is similar to PR9884.  As far as I can tell,
it's the exact same issue, in which case please mark this as a
duplicate.  Here goes.

The following snippet of code is not valid.

  | $ cat Test.java 
  | public class Test {
  |     public static void main(String[] s) {
  |         new Object() {
  |             public String toString() {
  |                 return "enclosing class: " + Test.this.getClass();
  |             }
  |         }.toString();
  |     }
  | }

Sun's javac compiler correctly rejects it like so:

  | $ java -version
  | java version "1.4.2_04"
  | Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
  | Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
  | $ javac Test.java
  | Test.java:5: non-static variable this cannot be referenced from a static context
  |                 return "enclosing class: " + Test.this.getClass();
  |                                                  ^
  | 1 error
  | $ echo $?
  | 1

GCJ accepts it:

  | $ gcj -C Test.java
  | $ echo $?
  | 0

The generated bytecode produces a runtime error:

  | $ gij Test
  | Exception in thread "main" java.lang.NullPointerException
  |    at Test$1.toString() (Unknown Source)
  |    at Test.main(java.lang.String[]) (Unknown Source)
Comment 1 Bryce McKinlay 2004-07-22 21:53:36 UTC
Confirmed.
Comment 2 Bryce McKinlay 2004-07-22 21:53:54 UTC
Its a java bug.
Comment 3 Andrew Pinski 2004-07-22 21:55:55 UTC
Actually this is a dup of bug 12459.

*** This bug has been marked as a duplicate of 12459 ***
Comment 4 Andrew Pinski 2004-07-23 09:12:51 UTC
Reopening because I was wrong, this is not a dup of that bug at all.
Comment 5 Andrew Pinski 2004-07-23 09:24:13 UTC
No this is not a dup of bug 9884 either, the real problem is that gcj creates a this$0 (of type Test) for 
the inner Object class, this is just plainly wrong.
Comment 6 Tom Tromey 2007-01-09 20:46:19 UTC
All gcj front end bugs have been fixed by the gcj-eclipse branch merge.
I'm mass-closing the affected PRs.
If you believe one of these was closed in error, please reopen it
with a note explaining why.
Thanks.