Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

General FreeBASIC programming questions.
Post Reply
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

Post by fxm »

's &= s' seems to work anywhere, but except in a procedure body if 's' is a string passed by reference.
The concatenation result is invalid and may even cause a crash if one attempts to print the string:

Code: Select all

Sub doubleString (Byref s0 As String)
    s0 &= s0
End Sub

Dim As String s = "FreeBASIC"
doubleString(s)
Print "'" & s & "'"

Sleep

I see this change in the 'changelog.txt' file (Version 1.08.0):
[change]
- optimize byref 'm += s' string concatenations to fb_StrConcatByref() which will check for same string descriptor at run-time which can't be determined at compile time for byref parameters.
[fixed]
- sf.net #917: optimize 'm += s' string concatenations to fix the long compile times in the gcc backend (which makes heavy use of string building).
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

Post by fxm »

Obviously same problem when working anywhere with a reference to a string (but works with a dereferenced string pointer):

Code: Select all

Dim As String s1 = "FreeBASIC"
s1 &= s1
Print "'" & s1 & "'"

Dim As String s2 = "FreeBASIC"
Dim As String ptr ps = @s2
*ps &= *ps
Print "'" & *ps & "'"

Dim As String s3 = "FreeBASIC"
Dim Byref As String rs = s3
rs &= rs
Print "'" & rs & "'"

Sleep
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

Post by fxm »

In the concatenation expression, if we replace the reference 'r' with '*@r', it works:

Code: Select all

Sub doubleString (Byref s0 As String)
    *@s0 &= *@s0
End Sub

Dim As String s3 = "FreeBASIC"
Dim Byref As String rs = s3
*@rs &= *@rs
Print "'" & rs & "'"

Dim As String s = "FreeBASIC"
doubleString(s)
Print "'" & s & "'"

Sleep
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

Post by SARG »

Fixed (by jeff) in lastest build :-)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

Post by fxm »

Thanks Jeff for the fix, and SARG for the build.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

Post by fxm »

It was a regression not easy to isolate from the code crashing, especially since such the expression 's &= s' in a procedure is difficult to incriminate at first sight.

Otherwise I was wondering:
Why did this regression only seem to affect pure references ?
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Since fbc 1.08.0, 's &= s' no longer works in a procedure body if 's' is a string passed by reference.

Post by coderJeff »

fxm wrote: Jan 04, 2023 9:53 It was a regression not easy to isolate from the code crashing, especially since such the expression 's &= s' in a procedure is difficult to incriminate at first sight.
Thanks, fxm. In hindsight, I should have been more diligent in writing tests, but I only tested the case I was trying to fix and not all scenarios.
fxm wrote: Jan 04, 2023 9:53 Otherwise I was wondering:
Why did this regression only seem to affect pure references ?
Not sure. Using '*@' or not didn't change anything for me. It appears solved out before the optimizations are tried, so couldn't say why it was different on your system. And I was seeing the problem on both byref parameters and byref variables, and both see the same checks before an optimization is applied..
Post Reply