this post was submitted on 23 Jan 2024
15 points (100.0% liked)

Programming

13384 readers
1 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 2 years ago
MODERATORS
 

So. I was working with this code:

public function front(?string $sortBy,
        ?string $time,
        ?string $type,
        ?string $filter,
        string $federation,
        Request $request): Response
    {
        $user = $this->getUser();

        if (!$user)
        {
            $filter = 'all';
        }

        $criteria = new EntryPageView($this->getPageNb($request));
        $criteria->showSortOption($criteria->resolveSort($sortBy))    

... and so on; the function continues. VSCode told me there was a syntax error on the close brace on the if statement. I stared at it for a while, then pasted the code into GPT (not Copilot). Here's the conversation we had about it:


Me

What's the syntax error here?

(and I pasted the code)

GPT

The syntax error in your PHP function public function front seems to be due to the incorrect placement of the closing curly brace for the if statement. It appears that the if statement is not properly closed before the $criteria initialization begins. The corrected code should look like this:

public function front(?string $sortBy, ?string $time, ?string $type, ?string $filter, string $federation, Request $request): Response
{
    $user = $this->getUser();

    if (!$user) {
        $filter = 'all';
    } // Closing brace for the if statement

    $criteria = new EntryPageView($this->getPageNb($request));
    $criteria->showSortOption($criteria->resolveSort($sortBy));
    // ... rest of your function code ...
}

Ensure that each statement and block is properly closed and followed correctly by the next statement. This corrected structure should resolve the syntax error you are encountering.

Me

I don't get it. I put a closing curly brace right after the statement in mine... what am I missing?

GPT

I apologize for the confusion. Upon re-examining your code, it seems the placement of the curly brace is correct. If you're still encountering a syntax error, it may not be related to the if statement structure.

Let's look at other possible issues:


... and so on. Pasting GPT's recommended if-statement block, including the curly brace that was already there, fixed the error. I have since re-pasted the same thing I initially gave to GPT to VSCode, and it all works now, and asking the exact same question of GPT again now leads to random guesses which have nothing to do with the cursed curly brace that was previously causing problems for both VSCode and GPT.

I have given up and moved on at this point, content to say vaguely that maybe the unusual brace style, or pasting the incomplete function without semicolon or final close brace, managed to coincidentally confuse GPT on exactly the same line where VSCode had gotten confused for unexplained reasons. But the whole thing still has this Twilight-Zone oddness to me.

you are viewing a single comment's thread
view the rest of the comments
[–] mozz@mbin.grits.dev 3 points 10 months ago* (last edited 10 months ago) (1 children)
  1. Yes, but GPT-4 is sophisticated enough to tell where the syntax error is. It's just a stochastic parrot, but finding the syntactic error in a block of code is, like language translation, actually an "intelligence" task that a stochastic parrot is well-qualified at. And, like I say, when I pasted the code a few times today, it gave more or less the right answer -- there's no obvious syntax error but here are a few things you might want to think about. It was only the one time right after VSCode complained that it also complained about the exact same line.

  2. That doesn't explain why VSCode complained about the same line. Unless VSCode has started doing syntax checking with an LLM which I would be surprised to find out was the case (for computational-cost reasons if for no other reasons.)

There's no actual syntax error. I even put the whole thing into hexl-mode just to make sure there wasn't some hidden Unicode character mucking it up in some invisible way. I thought I was just missing something unexpected (maybe a brace problem somewhere else in the code) and blaming my tools, but at this point I've literally pasted back the exact code I gave to GPT in the first place, and it works now for both GPT and VSCode.

(Edit: Oh, and I didn't tell it what VSCode was complaining about. I specifically asked the question in the way that I quoted above, specifically so that it wouldn't get hinted to look at a particular line or what the issue might be. The fact that it zeroed in on exactly the same issue that VSCode did, even when the code was completely separated from anything like brace problems in the surrounding code that might be causing weird problems, was what started to weird me out. When I pasted its identical if-block back into my code which still would have had the same surrounding brace problems if that was the issue, and it started working, was when I got significantly more weirded out.)

[–] MagicShel@programming.dev 3 points 10 months ago (1 children)

I was going to make the point that there may not be an error, but because you tell it to give you one, it finds something. But that doesn't explain VSCode, you're right. Every once in a while an IDE is just wrong. I was going to suggest trying a different IDE which might give you a less cryptic message. I often times find bad brace errors to be a result of something much higher in the code.

But again, not knowing PHP, I could only take a stab at answering why with the AI. I've tried many times to have an AI help me with these tasks. And sometimes it's very good at them, but other times I can spend hours refining my query and arguing with it and never make any headway.

[–] mozz@mbin.grits.dev 1 points 10 months ago

Haha yeah. That's why I tried it again a few times today. Today everything is normal.

shrug

It is a mystery. Maybe it is just a coincidence of failures, or maybe Copilot actually is used for some syntax-highlighting things and there was some transient subtle failure in the model that affected both Copilot and GPT. Neither of those explanations really feel plausible to me though. Oh well.