Confusing compiler message (Gnat 16.1.0) on new bracket syntax

I had an array of enums and used the following

Data_Items [I].Tag := Data.FIFO_Tag'Enum_Val (Data_16.Tag);

when it should have been

Data_Items (I).Tag := Data.FIFO_Tag'Enum_Val (Data_16.Tag);

The bracket error compiler messages are usually very informative but a little embarrassingly in a moment of end of day brain fog I was stumped by the following error message.

error: missing “:=”

I remember C compilers having single message: “syntax error” for all occasions. After all the idea of having [] was to get C vibe. Success, I would say… :grinning:

This may be a consequence of the compiler being overly helpful and backfiring.

As I understand it, you can only used brackets for initialization of an array. The compiler sees an identifier and then a bracket, so rather than say the actual problem (unexpected symbol) it tries to guess that you meant to initialize an array – hence the bracket. After all, it’s almost certainly not looking beyond the bracket to the rest of the line and trying to parse that.

I would like gnat to provide more helpful error messages, but not by trying to guess what I mean. I’d rather it provide a little more detail. For example, “invalid prefix” could tell me what type it’s seeing.

Yes, this how my Ada parser treats it, giving:

';', ':' or ':=' is expected

guessing:

  • assignment
  • named statement
  • call

GNAT parser guesses only assignment as the most probable alternative, assuming that the following [ begins an aggregate. It is quite clever, but goes in the wrong direction.

That would mean a more permissive grammar that considers [] being a mistyped (), which would be more guessing.

The problem here is not GNAT, which gives a quite thoughtful message (alas the thought is all wrong). The problem is Ada. The introduction of [] was not necessary. But if decided, then [] should have been allowed in all contexts where () are. For example, for user-defined indexing. Then the grammar considering Data_Items [I] a valid name would be reasonable and the compiler would not reject it straight away, but later during semantic analysis giving more information about types and operations considered.

That would mean a more permissive grammar that considers [] being a mistyped (), which would be more guessing.

I think one of us misunderstands the other. When I referred to “invalid prefix” I wasn’t talking about [] syntax at all. I was referring to the phenomenon where the code has A.B and gnat says invalid prefix for B because the type of A does not have B as a method – or, in some cases, A is not even tagged. The compiler doesn’t have to guess either of these things; if it didn’t already know that, it wouldn’t report the error.

Yes, but that is just same. Prefix (ARM 4.1) can be of a component, dereference, indexing. These give us two “operations” dot (.) and left parenthesis (() that separate it from the suffix. So if we wanted Data_Items considered a prefix in Data_Items [I], then [ must be added to the list, e.g. as indexing or maybe some weird component etc. Then the compiler could say “invalid prefix” having first recognized it as such.

What I’m not following is how this statement of mine:

For example, “invalid prefix” could tell me what type it’s seeing.

…which, as I clarified, refers to this:

the phenomenon where the code has A.B and gnat says invalid prefix for B because the type of A does not have B as a method – or, in some cases, A is not even tagged.

…implies this: (emphasis added)

That would mean a more permissive grammar that considers [] being a mistyped (), which would be more guessing.

You do add this: (emphasis added)

So if we wanted Data_Items considered a prefix in Data_Items [I], then [ must be added to the list…

And I don’t dispute that, but that isn’t what I was suggesting at all.

It is not a prefix.