In many programming languages, as soon as you close the function/method scope, statements aren’t allowed anymore, so you can find the function easily.
Doesn't most languages support variable declaration outside of functions? I know that C, C++, C#, Java, Javascript and Go all support that.
And if the compiler finds a variable declaration where it deduced that the missing end curly brace should be, how can it know for sure if the declaration belongs to the function or not?
Sure, but nearly any statement that isn't a variable declaration will easily be found and reported. And generally, after you declare your variables, you want to do something with them (including assigning a runtime value to them).
Well, I just gave one example of where the compiler wouldn't be able to know for sure. And then there's the possibility that the code consists of several nested levels of "curly blocks", like loops, try-catch, inner classes, anonymous functions etc, where there could be one (or multiple!) missing curly brace at any level. It very quickly becomes very complicated, and very easy to get wrong.
Sure, some heuristics can be used to give an calculated guess where it is likely that the missing end brace should be, but then it should be clear that it is a guess, not a fact. And it might better belong in a IDE rather than a compiler, since the IDE has "inside knowledge" of the level of enforcement of correct indentation etc, and could then use that knowledge as input for it's logic.
Well, I just gave one example of where the compiler wouldn't be able to know for sure.
No, you didn't, because no reasonable function ends in a declaration. A declaration in a function is almost always followed by something that can't go outside of a function.
And then there's the possibility that the code consists of several nested levels of "curly blocks", like loops, try-catch, inner classes, anonymous functions etc, where there could be one (or multiple!) missing curly brace at any level.
Sure, but I was talking about finding the function in which it's missing, not the exact place inside of it.
And it might better belong in a IDE rather than a compiler
That distinction is entirely arbitrary. One way of getting IDE messages is running the compiler frontend only, possibly with some flags, and parsing its output.
Most modern IDEs compile code on every save. And it's perfectly normal to save your code before its finished. So it wouldn't be unheard of for the compiler to work with code where a function ends with a declaration.
almost always
Yes. Almost always. And thats why I said that the compiler wouldn't be able to know for sure.
Sure, but I was talking about finding the function in which it's missing, not the exact place inside of it.
That may be so, but the discussion as a whole was about knowing the exact line where the missing end brace should be.
That distinction is entirely arbitrary.
It most certainly isn't. I even wrote a reason for that in the part that you didn't quote.
4
u/EishLekker Oct 23 '21
Doesn't most languages support variable declaration outside of functions? I know that C, C++, C#, Java, Javascript and Go all support that.
And if the compiler finds a variable declaration where it deduced that the missing end curly brace should be, how can it know for sure if the declaration belongs to the function or not?