Skip to content

Mode markers across modules, single-character resourcestrings, UTF-8 default for unleashed - #5

Open
civilian7 wants to merge 4 commits into
Moonbot-Tech:mainfrom
civilian7:fix/unleashed-compat
Open

civilian7 wants to merge 4 commits into
Moonbot-Tech:mainfrom
civilian7:fix/unleashed-compat

Conversation

@civilian7

Copy link
Copy Markdown

Three fixes found while building a product on -Munleashed, plus a gate for them.

1. Mode markers leaked between modules

FPC_UNLEASHED, FPC_DELPHI, FPC_OBJFPC, ... are system macros shared by all modules, while the mode itself is per module. A $MODE directive in one unit changed the shared macro, and nothing restored it for the modules compiled after that unit.

In the product profile this happened every time. The pinned memory manager unit is compiled first and declares {$mode delphi}. As a result:

  • A program built with -Munleashed saw no FPC_UNLEASHED (the Unleashed docs say -Munleashed sets it the same way as the directive).
  • A program built with -Mobjfpc saw no FPC_OBJFPC.

In both cases the program itself was compiled in the right mode. The -va trace shows where it happens:

Macro defined: FPC_UNLEASHED                                   <- -Munleashed
mormot.core.fpcx64mm.pas(212,2) Macro undefined: FPC_UNLEASHED
md.pas(3,8) IFDEF FPC_UNLEASHED found, rejected

SyncModeMacro (scanner) now brings the markers in line with the module's mode switches at two points: when a module starts compiling (compile_module) and when its saved state is restored (tglobalstate.restore). The marker code that SetCompileMode used to inline is now this one routine.

2. A one-character resourcestring outside ASCII was rejected

resourcestring
  SYes = '예';   // Error: Illegal expression   (also 'é'; two characters and ASCII were fine)

A one-character literal outside ASCII is a WideChar constant, and the resourcestring parser only took an AnsiChar constant. It is now turned into a string constant and takes the ordinary string path. This failed in every mode.

3. {$mode unleashed} reads sources as UTF-8 by default

Without a BOM, -Fc or {$codepage}, sources are read in ISO-8859-1, so '한글' is six characters. Unleashed is a new dialect with no legacy sources to keep, so it now reads UTF-8. An explicit code page still wins, and the other modes keep their historical default (the gate checks that Delphi mode is unchanged). A sentence in unleashed/docs/README.md documents this.

This one changes behaviour, so please treat it as a proposal. If you prefer, it can be dropped from the PR without affecting 1 and 2.

Verification (Win64)

Check Result
qualification/compiler-unleashed-mode/run_gate.py (new, 7 cases, expected values are code points) 7/7. Before the fix 4 fail
Product smoke, Debug and Release MOONBOT_BUILD_OK
qualification/pinned-unit/run.ps1 PASS
RTL-test/run.py, all sources and modes 813/819, identical to main on the same machine. The two failures are the same on main (core_semantic debug fails on ANSI code page 949, and win64_trailing_call_semantic)

Independent of #4 (dynamic packages).

🤖 Generated with Claude Code

civilian7 and others added 3 commits November 27, 2026 02:17
…d sources as UTF-8

Mode markers leaked between modules. FPC_UNLEASHED, FPC_DELPHI and the
other mode markers are system macros shared by all modules, while the mode
itself is per module. A $MODE directive in one unit changed the shared
macro and nothing restored it for the modules compiled after it. In the
product profile this always happened: the pinned memory manager unit is
compiled first and declares {$mode delphi}, so a program built with
-Munleashed or -Mobjfpc saw neither FPC_UNLEASHED nor FPC_OBJFPC, while
the program itself was compiled in the right mode. SyncModeMacro now
brings the markers in line with the module's mode switches when a module
starts compiling and when its saved state is restored.

A resourcestring of one character outside ASCII ('é', a Hangul syllable)
is a WideChar constant, and the resourcestring parser only accepted an
AnsiChar constant: "Illegal expression" in every mode. It is now turned
into a string constant and takes the ordinary string path.

{$mode unleashed} reads a source without BOM, -Fc or {$codepage} as UTF-8.
Under the ISO-8859-1 default a two-letter Korean literal was six
characters. Unleashed is a new dialect without legacy sources to keep; the
other modes keep their historical default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…default

qualification/compiler-unleashed-mode/run_gate.py compiles and runs seven
cases: mode markers with the mode from the command line and from a
directive, across a unit in another mode; a one-character resourcestring
outside ASCII with and without -Fc; the UTF-8 default of unleashed mode;
and Delphi mode keeping its historical default as a control. Expected
values are Unicode code points, not the current compiler's output. Before
the fix four of the seven fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An interpolated string with arguments was written with WriteStr into an
AnsiString temporary. In a Unicode string profile every non-ASCII fragment
then went through the ANSI code page: $'{N}건 조회했습니다.' came out as
thirteen characters of broken text instead of ten. The temporary is now a
UnicodeString when the default string is Unicode, an AnsiString otherwise.

qualification/compiler-unleashed-mode: interpolated-string-unicode (code
points, fails before the fix).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@civilian7

Copy link
Copy Markdown
Author

Added f495f2f Build interpolated strings in string, not AnsiString. An interpolated string with arguments was built in an AnsiString temporary, so in a Unicode string profile every non-ASCII fragment went through the ANSI code page: $'{N}건 조회했습니다.' came out as 13 characters of broken text instead of 10. New gate case interpolated-string-unicode (expected values are code points): it fails before the fix and passes after it (8/8).

…roperty initializers

- An if / case expression whose branches are string constants was typed
  ShortString (branch_type promoted two chararrays to cshortstringtype, a
  char with a chararray to AnsiString). A 300-character constant came out as
  255 characters, and in a Unicode string profile '한글' went through the
  ANSI code page, with warning 4104 at every use. Such branches now have the
  type `string` has in the mode (UnicodeString in unleashed, AnsiString
  under {$H+}, ShortString otherwise).

- A class with auto-property initializers (property Width: Integer = 800;)
  and no constructor of its own is given an inherited constructor to run
  them in. Only the direct parent's constructors were copied: a TPersistent
  descendant got none (TObject.Create is two levels up) and kept 0.
  add_missing_ancestor_constructors_intf takes the constructors of every
  ancestor that a closer one does not hide by name.

qualification/compiler-unleashed-mode: statement-expression-string,
auto-property-no-constructor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@civilian7

civilian7 commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

Added 3b1d8ff String type of if/case expressions, inherited constructors for auto-property initializers:

  • if / case expressions whose branches are string constants were typed ShortString, so they were cut at 255 characters, and in a Unicode profile non-ASCII text went through the ANSI code page. They now have the mode's string type.
  • Auto-property initializers of a class without a constructor of its own were skipped when the inherited constructor lives more than one level up (a TPersistent descendant). The constructors of every ancestor are considered now.

qualification/compiler-unleashed-mode has two new cases and passes 10/10 on a toolchain built from this branch.

🤖 Generated with Claude Code

@Moonbot-Tech

Copy link
Copy Markdown
Owner

Could you submit the mode-marker fix and the single-character resourcestring fix together in a small, separate PR, with their regression tests? Both fix existing compiler bugs and are useful independently of the language changes.

  • Build an Unleashed and an ObjFPC caller that imports a unit with {$mode delphi}. Assert that the caller still takes its own mode’s {$IFDEF} branch after the import.
  • Compile a one-character resourcestring such as é or 예 and check its value at runtime; include a two-character control. The existing compiler rejects the single-character case.

The ten gate cases on 3b1d8ff49 pass, but the remaining changes need coverage for these cases:

  • A generated constructor attempts to override a final ancestor constructor, breaking a previously valid descendant. Another previously valid assignment, child.Make := 19, stops compiling when a grandparent has Make(I) and the immediate parent exposes a writable Make property. Please keep both examples compiling and assert that the property setter runs.
  • With a property initializer of 73, inherited TC.Create().P returns 0 while TC.Create(43).P returns 73. This initialization bug predates the PR and is still present. Both overloads should initialize the property.
  • Under {$H-}, an ordinary string variable selects the ShortString overload. An if expression choosing between string literals now selects UnicodeString; interpolation with an argument also selects UnicodeString. Please test overload selection explicitly and preserve the existing behavior of the if expression.

Please keep the proposed UTF-8 default for Unleashed in a separate language-change PR so it can be evaluated on its own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants