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

JEP 447 improve error message for constructor calls occurring nested in some control structure #2468

Closed
stephan-herrmann opened this issue May 21, 2024 · 3 comments
Labels
preview-feature Preview Feature

Comments

@stephan-herrmann
Copy link
Contributor

public class TestFlow {
	TestFlow(boolean f) {
		if (f)
			super();
		else
			this();
	}
}

This reports "Constructor call must be the first statement in a constructor", despite the preview being enabled.

In JLS this is managed by grammar rules admitting constructor calls only as a top level statement in the constructor body. When this feature enabled, should we say smth like "must be a top-level statement in a constructor body"? Would that be clear, or do we need to explicitly mention what is not allowed: wrapping the constructor call in some control structure or block?

@stephan-herrmann
Copy link
Contributor Author

Found this comment in a recent spec draft:

The grammar makes it impossible, for example, to place explicit constructor invocations in different branches of an if statement.

stephan-herrmann added a commit to stephan-herrmann/eclipse.jdt.core that referenced this issue Jul 15, 2024
…e-jdt#2466 (variation)

+ improve error messages for misplaced constructor calls
  + illegally nested
  + duplicate
@stephan-herrmann
Copy link
Contributor Author

stephan-herrmann commented Jul 15, 2024

should we say smth like "must be a top-level statement in a constructor body"? Would that be clear, or do we need to explicitly mention what is not allowed: wrapping the constructor call in some control structure or block?

In #2685 I opted for a simply "Constructor call is not allowed here" (javac says "Explicit onstructor call is not allowed here")

@stephan-herrmann
Copy link
Contributor Author

Resolved via #2685

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

No branches or pull requests

1 participant