[SOLVED] FPC trunk: Existing code is broken about constant enumerations lazarus / 64-FPC development ---------------------------------------------------------------------- abouchez · 2022-04-01 08:45 The new FPC trunk feature "Range checking for enumeration constants in Delphi mode" breaks existing code. https://wiki.freepascal.org/User_Changes_Trunk#Range_checking_for_enum eration_constants_in_Delphi_mode For instance, this code:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  /// value stored into a TLanguages enumerate to mark no language selected yet  LANGUAGE_NONE = TLanguages(255);did work forever, and is broken by this new FPC trunk "feature". The description in FPC user change list states that it is required in Delphi mode. But this is untrue: above code compiles and work since Delphi 1 up to latest Delphi 11.1 with no troubles. This is clearly a backward compatibility break, and avoid to run code working on Delphi and FPC since ever. Where do I need to discuss and ask for removal of this "feature"? I know forcing a value of an enumeration outside of its declared values is not beautiful, and should be avoided. But it worked since forever, and compiles to fine code: 255 is just a byte, and the asm has no trouble with it. See https://synopse.info/forum/viewtopic.php?id=6190 ---------------------------------------------------------------------- nanobit · 2022-04-01 13:41 Many FPC users are not aware of a limitation (or bug) here. What you see is supposed to prevent outer values and resultant undefined behavior in FPC apps (Delphi apps don't have this UB problem). See "enum cutoff implementation" in the notes ---------------------------------------------------------------------- Thaddy · 2022-04-01 13:46 It is a regression, and therefor a bug. 3.0.4 is fine, in trunk it is broken. ---------------------------------------------------------------------- PascalDragon · 2022-04-01 13:52 Please provide a full, self contained example that demonstrates the issue. The following code compiles in both FPC 3.2.2 and main and only triggers a warning: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program tenum; {$mode objfpc} type  TLanguages = (    lOne,    lTwo,    lThree,    lFour  ); const  LANGUAGE_NONE = TLanguages(255); begin end. ---------------------------------------------------------------------- Bart · 2022-04-01 14:13 This won't (fpc 3.2.2 on win32): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TLanguages = (    lOne,    lTwo,    lThree,    lFour  ); const  LANGUAGE_NONE = TLanguages(255);var  Lang: TLanguages; begin Lang := LANGUAGE_NONE;  //line 20end.Code: [Select]C:\Users\Bart\LazarusProjecten\ConsoleProjecten\test.pas Bart ---------------------------------------------------------------------- abouchez · 2022-04-01 16:41 Thanks for the feedback. It happens in Delphi mode, as I wrote in my initial post - and clearly stated by the trunk "User change" documentation entry. The documentation entry is wrong by the way: Delphi has no trouble with such code. Isn't there any compiler local directive to switch this behavior off? ---------------------------------------------------------------------- Jonas Maebe · 2022-04-01 19:09 The change you linked is about the fix for https://gitlab.com/freepascal.org/fpc/source/-/issues/35671, which according to multiple reporters is in fact rejected by Delphi. It is possible the change also caused the error you are seeing at the same time, but that was not the purpose of that change. There is, however, also the fact mentioned by nanobit that assigning out-of-range values to enums is not and never has been supported by FPC, because if it were valid then we could never perform any analyses based on the declared type of any ordinal variable (only based on their size). I know what you posted works with Delphi's code generator and that there is a bunch of code that relies on this, but it's a rather fundamental design issue (and philosophy) that cannot be addressed by just disabling one or two random optimisations in FPC (as gets suggested from time to time). That said, Sven's program compiles for me on main without even a warning when it's changed to Delphi mode (unlike in 3.2.2, where it does print a warning about the out-of-range constant; that seems like a regression). So, like they asked: please (always) post a full example that can be used to reproduce the problem you're reporting. ---------------------------------------------------------------------- korba812 · 2022-04-01 22:12 Quote from: Bart on April 01, 2022, 04:13:40 pmThis won't (fpc 3.2.2 on win32): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TLanguages = (    lOne,    lTwo,    lThree,    lFour  ); const  LANGUAGE_NONE = TLanguages(255);var  Lang: TLanguages; begin Lang := LANGUAGE_NONE;  //line 20end.Code: [Select]C:\Users\Bart\LazarusProjecten\ConsoleProjecten\test.pas Bart But this one does. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...var  Lang: TLanguages; begin Lang := TLanguages(255);end.What is the difference between a named constant and an anonymous one? ---------------------------------------------------------------------- Jonas Maebe · 2022-04-02 11:27 Ah, sorry, I missed that reply. The difference comes from the fact that when we create an ordinal constant node from a constsym (like LANGUAGE_NODE), we always enable range checking for that ordinal node. I fixed that bug in 3da54dcf9f87336887f592c98c0088372da876e6 on main. ---------------------------------------------------------------------- Jonas Maebe · 2022-04-02 12:38 Quote from: Bart on April 01, 2022, 04:13:40 pmThis won't (fpc 3.2.2 on win32): The change was indeed already in 3.2.0. I've moved it there. ---------------------------------------------------------------------- CC · 2022-04-02 18:23 Quote from: Jonas Maebe on April 02, 2022, 01:27:55 pmAh, sorry, I missed that reply. The difference comes from the fact that when we create an ordinal constant node from a constsym (like LANGUAGE_NODE), we always enable range checking for that ordinal node. I fixed that bug in 3da54dcf9f87336887f592c98c0088372da876e6 on main. Thanks for the quick reaction! I have applied your fix locally to the fixes branch. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  CurrentLanguage: TLanguage = (    Index:    LANGUAGE_NONE;  <--- line 324    CharSet:  DEFAULT_CHARSET;    CodePage: CODEPAGE_US;    LCID:   LCID_US  );     <--- line 328   mORMoti18n.pas(324,15) Warning: range check error while evaluating constants (255 must be between 0 and 59) mORMoti18n.pas(324,28) Error: range check error while evaluating constants (255 must be between 0 and 59) Maybe it requires change at some other points in the source as well. ---------------------------------------------------------------------- PascalDragon · 2022-04-02 18:40 Quote from: CC on April 02, 2022, 08:23:34 pmMaybe it requires change at some other points in the source as well. Then please provide a full example that demonstrates this and not snippets where we need to guess what the full code is. ---------------------------------------------------------------------- AlexTP · 2022-04-02 18:58 My guess (but I didn't test on latest) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TLang = (op1, op2, op3);const  cMyLang = TLang(255); type  TRec = record    l: TLang;    v: integer;  end;const  cMyRec: TRec = (l: cMyLang; v: 10); begin  writeln(cMyRec.l); end. ---------------------------------------------------------------------- CC · 2022-04-02 20:19 Quote from: PascalDragon on April 02, 2022, 08:40:22 pmQuote from: CC on April 02, 2022, 08:23:34 pmMaybe it requires change at some other points in the source as well. Then please provide a full example that demonstrates this and not snippets where we need to guess what the full code is. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program T001;//{$mode objfpc}{$H+}{$MODE DELPHI}{$R-}type  TLanguages = (    lOne,    lTwo,    lThree,    lFour  ); const  LANGUAGE_NONE = TLanguages(255); type TLanguage = record   Index : TLanguages; end; var  Lang: TLanguages;  CurrentLanguage: TLanguage = (    Index:  LANGUAGE_NONE  ); begin  Lang := LANGUAGE_NONE;end.  Verbose: Compiling T001.lpr T001.lpr(14,19) Warning: range check error while evaluating constants (255 must be between 0 and 3) T001.lpr(24,13) Warning: range check error while evaluating constants (255 must be between 0 and 3) T001.lpr(24,26) Error: range check error while evaluating constants (255 must be between 0 and 3) T001.lpr(28,11) Warning: range check error while evaluating constants (255 must be between 0 and 3) This error occures in both modes objfpc & DELPHI. ---------------------------------------------------------------------- Jonas Maebe · 2022-04-03 09:20 This case is now fixed as well. ---------------------------------------------------------------------- CC · 2022-04-03 16:15 It works now, thanks a lot! This was amazingly fast fix during a weekend. ---------------------------------------------------------------------- abouchez · 2022-04-04 06:28 Sounds like it was some kind of a regression. https://gitlab.com/freepascal.org/fpc/source/- /commit/3da54dcf9f87336887f592c98c0088372da876e6 fixed it in the trunk. The response time was very quick, during the weekend, congrats!