Page 1 of 1

Compiler check for missing ProcedureReturn?

Posted: Sat Apr 09, 2022 5:50 pm
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

Re: Compiler check for missing ProcedureReturn?

Posted: Sat Apr 09, 2022 7:46 pm
by jacdelad
Please excuse my ignorance, but can you give me a short example to understand how it causes problems to forget the ProcedureReturn?

Re: Compiler check for missing ProcedureReturn?

Posted: Sat Apr 09, 2022 10:18 pm
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.

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 4:14 am
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.

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 9:56 am
by HeX0R
-1

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 10:04 am
by Marc56us
-1 also
No need to slow down the compilation with additional checks when user forgetting does not cause a compilation error.

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 12:06 pm
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).

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 7:02 pm
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.

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 7:40 pm
by mk-soft
After ASM or C-Backend output, the result of the procedure is always returned as NULL if ProcedureReturn is not called.

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 7:53 pm
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.

Re: Compiler check for missing ProcedureReturn?

Posted: Sun Apr 10, 2022 10:40 pm
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.

Re: Compiler check for missing ProcedureReturn?

Posted: Mon Apr 11, 2022 9:07 am
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