Compiler check for missing ProcedureReturn?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
hoerbie
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Compiler check for missing ProcedureReturn?

Post by hoerbie »

Hi,

after searching a problem in an actual program for hours, I think that it maybe would be helpful, if a Procedure for example is declared with a return parameter long like

Code: Select all

Procedure.l name()
or other return type and the

Code: Select all

ProcedureReturn var
is missing in the Procedure, the compiler should throw a warning/error.

Greets, hoerbie
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Compiler check for missing ProcedureReturn?

Post by jacdelad »

Please excuse my ignorance, but can you give me a short example to understand how it causes problems to forget the ProcedureReturn?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Compiler check for missing ProcedureReturn?

Post by Tenaja »

Pb returns 0 by default, for every procedure. When you declare it with something other than Int--because you require a return value-- getting that default value can be a tricky thing to chase down.


The smart thing to do, if you require a valid return value, is to, well, just do it, but if you want to make it easy to see if you skipped it, then put and invalid value (error code) at the bottom. Then, check it for the error code on the receiving end.
BarryG
Addict
Addict
Posts: 4134
Joined: Thu Apr 18, 2019 8:17 am

Re: Compiler check for missing ProcedureReturn?

Post by BarryG »

+1. I agree with hoerbie. If you're defining a type with the Procedure command, then you're obviously intending to use ProcedureReturn with it, so if the compiler gets to the end of the procedure and hasn't seen one, it could raise an error to alert you. This has happened to me before where I had ProcedureReturn commented out during a test, and forgot to uncomment it again.
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Compiler check for missing ProcedureReturn?

Post by HeX0R »

-1
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Compiler check for missing ProcedureReturn?

Post by Marc56us »

-1 also
No need to slow down the compilation with additional checks when user forgetting does not cause a compilation error.
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Compiler check for missing ProcedureReturn?

Post by mk-soft »

Tenaja wrote: Sat Apr 09, 2022 10:18 pm Pb returns 0 by default, for every procedure. When you declare it with something other than Int--because you require a return value-- getting that default value can be a tricky thing to chase down.
That not exact. ProcedureReturen without variable return the eax/rax register

Code: Select all


Procedure foo()
  CompilerSelect #PB_Compiler_Processor
    CompilerCase #PB_Processor_x64
      !mov rax, 100
    CompilerCase #PB_Processor_x86
      !mov eax, 100
  CompilerEndSelect
  ProcedureReturn
EndProcedure

Debug foo()
PB-Help: If no value is specified for ProcedureReturn, the returned value will be undefined (see inline assembly for more information).
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
infratec
Always Here
Always Here
Posts: 7587
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Compiler check for missing ProcedureReturn?

Post by infratec »

That can be very tricky, because some coders do something like this:

Code: Select all

Procedure.a Test(i.i)
  
  Select i
    Case i = 1
      ProcedureReturn 10
    Case i = 2
      ProcedureReturn 9
  EndSelect
  
EndProcedure
I 'forgot' to return something if nothing fits.
But there are ProcedureReturns inside.
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Compiler check for missing ProcedureReturn?

Post by mk-soft »

After ASM or C-Backend output, the result of the procedure is always returned as NULL if ProcedureReturn is not called.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
infratec
Always Here
Always Here
Posts: 7587
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Compiler check for missing ProcedureReturn?

Post by infratec »

@mk-soft
But this is not the problem.

The wish is that this is detected by the compiler.
But I think that this is nearly impossible with my example.

And Null can be a vaild result of the procedure, so it is also not detectable if the ProcedureReturn is missing or not.
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Compiler check for missing ProcedureReturn?

Post by mk-soft »

Purebasic makes no distinction between a sub or a function. So you have to make sure yourself that a valid return value is assigned to a function.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Compiler check for missing ProcedureReturn?

Post by juergenkulow »

-1

Code: Select all

; C return value 
Procedure.w foo()
  ! return 4711;
EndProcedure

Debug foo()
CompilerIf #PB_Backend_C<>#PB_Compiler_Backend
  CompilerError "C Backend only, please use PB 6.00 Beta"
CompilerEndIf
Post Reply