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

[23] super reference disallowed in the context of a field access #3094

Closed
jarthana opened this issue Oct 16, 2024 · 6 comments · Fixed by #3131
Closed

[23] super reference disallowed in the context of a field access #3094

jarthana opened this issue Oct 16, 2024 · 6 comments · Fixed by #3131
Assignees
Milestone

Comments

@jarthana
Copy link
Member

Here's the testcase:

public class X {
	class Nested extends X1 {
		X1 xy;
		class DeeplyNested extends NestedInX1 {
			DeeplyNested(float f) {
				Nested.super.x1.super(); // Error here
			}
		}
	}
}
class X1 {
	X1 x1;
	class NestedInX1 {}
}

The error reported is Cannot use 'Nested.super' in an early construction context (except in a simple field assignment)
I don't really know the intricacies here and Javac allows this code.

Copying @mpalat and @stephan-herrmann

@jarthana
Copy link
Member Author

I don't know if this is related, but the following code is rejected too:

public class X {
	class Nested {
		Nested(Object o) {}
	}
	class AnotherNested extends Nested {
		AnotherNested() {
			super(new Object() { // Cannot instantiate class new Object(){} in an early construction context of class X.AnotherNested
			});
		}
	}
}

@stephan-herrmann stephan-herrmann self-assigned this Oct 17, 2024
@stephan-herrmann stephan-herrmann added this to the 4.34 M3 milestone Oct 17, 2024
@jarthana
Copy link
Member Author

Here's another variant:

    Y(int a) {
        X.field = new Y() {
            public int foo() {
                return this.a;
            }
        }.foo();
        this();
    }

@stephan-herrmann
Copy link
Contributor

@jarthana Which field should this.a point to?
Somehow you're not telling the full story of that snippet.

@jarthana
Copy link
Member Author

@jarthana Which field should this.a point to? Somehow you're not telling the full story of that snippet.

Yeah, I was just playing around and was too eager to report. Let me get back on this.

@stephan-herrmann
Copy link
Contributor

The error reported is Cannot use 'Nested.super' in an early construction context (except in a simple field assignment)

The naked eye doesn't even see any early construction context (and --enable-preview is not needed for javac). So this part looks buggy ...

I don't really know the intricacies here and Javac allows this code.

"intricacies" is a good word here :)
It took my quite some time to figure out what this program is trying to say.

This version seems to be equivalent, and is accepted by ecj:

	x1.super();

(qualifying the inherited field is not necessary).

Read: when going from DeeplyNested to its super NestedInX1 we need one additional enclosing instance which is provided as x1. Wow!

stephan-herrmann added a commit to stephan-herrmann/eclipse.jdt.core that referenced this issue Oct 21, 2024
+ distinguish super vs. outer type for early constr. check

fixes eclipse-jdt#3094
stephan-herrmann added a commit that referenced this issue Oct 21, 2024
+ distinguish super vs. outer type for early constr. check

fixes #3094
@stephan-herrmann
Copy link
Contributor

stephan-herrmann commented Oct 21, 2024

 public class X {
	class Nested {
		Nested(Object o) {}
	}
	class AnotherNested extends Nested {
		AnotherNested() {
			super(new Object() { // Cannot instantiate class new Object(){} in an early construction context of class X.AnotherNested
			});
		}
	}
}

For this I filed #3132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants