[fpc-devel] Why: "Can't take the address of constant expressions" here?

Sven Barth pascaldragon at googlemail.com
Thu Jan 12 21:39:11 CET 2023


Am 11.01.2023 um 23:58 schrieb Bart via fpc-devel:
> Given the following program (an excerpt form a test program for a
> bugreport about the fpwidestring unit):
>
> ===
> program test;
> {$codepage utf8}
> {$mode objfpc}
> {$h+}
>
> uses
>    FpWideString;
>
> var
>    WSource: WideString = 'source';
>    USource: UnicodeString = 'source';
>    WDest: WideString = '' ;
>    UDest: UnicodeString = '';
>    ASource: AnsiString = 'source';
>    ADest: AnsiString = '';
>    P: array[0..99] of AnsiChar;
>
> begin
>    with WideStringManager do
>    begin
>      writeln(1);
>      Wide2AnsiMoveProc(pwidechar(WSource),RawByteString(ADest),
> CP_UTF8, Length(WSource));
>      writeln(2);
>      Ansi2WideMoveProc(PChar(ASource), CP_UTF8, UDest,
> Length(ASource));   //<< test.lpr(24,53) Error: Can't take the address
> of constant expressions (caret behind UDest)
> end.
> ====
> C:\Users\Bart\LazarusProjecten\bugs\Console\fpwidestring>fpc test.lpr
> Free Pascal Compiler version 3.3.1 [2022/10/11] for i386
> Copyright (c) 1993-2022 by Florian Klaempfl and others
> Target OS: Win32 for i386
> Compiling test.lpr
> test.lpr(24,53) Error: Can't take the address of constant expressions
> ...
> UDest is of the wrong type here, it compiles fine with WDest (WideString).
> I just don't understand the "Can't take the address of constant expressions"
> I would have expected something like "Call by var for arg no. 3 has to
> match exactly: Got "UnicodeString" expected "WideString""

The issue is indeed the conversion from UnicodeString to WideString 
which is not allowed for a var/out parameter. That the compiler doesn't 
use the error “Call by var for arg no. 3 has to match exactly: Got 
"UnicodeString" expected "WideString"” is due to Ansi2WideMoveProc() 
being a function pointer. The code that determines that error is not 
called in that case, instead the other is caused essentially as a last 
resort.

You can report a bug with the following sample to hopefully improve this:

=== code begin ===

program tunicode;

var
   Str: UnicodeString = 'Foobar';

procedure Test(var aArg: WideString);
begin

end;

var
   Func: procedure(var aArg: WideString) = Nil;
begin
   Test(Str);
   Func(Str);
end.

=== code end ===

Regards,
Sven


More information about the fpc-devel mailing list