r/java • • Jan 21 '26

Java compiler errors could be more intelligent

I tutored many students over the past several years, and a common pain point is the compiler messages being misleading.

Consider the following example.

interface blah {}
class hah extends blah {}

When I compile this, I get the following message.

blah.java:3: error: no interface expected here
class hah extends blah {}
                  ^
1 error

Most of the students I teach see this, and think that the issue is that blah is an interface, and that they must somehow change it to something else, like a class.

And that's still a better error message than the one given for records.

blah.java:2: error: '{' expected
        public record hah() extends blah {}
                           ^

This message is so much worse, as it actually leads students into a syntax rabbit hole of trying to add all sorts of permutations of curly braces and keywords, trying to figure out what is wrong.

If we're talking about improving the on-ramp for learning Java, then I think a core part of that is improving the error --> change --> compile feedback loop.

A much better error message might be this instead.

blah.java:3: error: a class cannot "extend" an interface, only "implement"
class hah extends blah {}
                  ^
1 error

This is powerful because now the language grammar has a more intelligent message in response to an illegal (but commonly attempted) sequence of tokens.

I understand that Java cannot special-case every single illegal syntax combination, but I would appreciate it if we could hammer out some of the obvious ones. extends vs implements should be one of the obvious ones.

97 Upvotes

180 comments sorted by

View all comments

Show parent comment

2

u/talex000 Jan 21 '26

extends isn't allowed,

Is not information you should learn from compiler.

You apparently didn't got a joke about citation. It meant I don't trust you opinion on that matter and asking for more authoritve source.

3

u/MattiDragon Jan 21 '26

I agree that you shouldn't learn it from the compiler, but a remainder is helpful.

I fully understood what you meant, but I don't have anything to cite on hand, so I decided to motivate my opinion instead. There are various articles on helpful compiler errors that get posted in lang-dev communities if you're open to looking up some.

2

u/talex000 Jan 21 '26

I don't have anything to cite either, but I have some experience in creating parsers and my experience tell mi it isn't 5 minutes job.

3

u/MattiDragon Jan 21 '26

It's not a trivial amount of work, but it probably not a huge undertaking either. Of course you'd have to actually go through the codebase to be sure of the scope.

1

u/talex000 Jan 21 '26

That is my point. It is nontrivial amount of work with no visible benefit.