diff --git a/compiler/ngtcon.pas b/compiler/ngtcon.pas index 05d6cbd69d..8368bfd852 100644 --- a/compiler/ngtcon.pas +++ b/compiler/ngtcon.pas @@ -1121,17 +1121,26 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis procedure tasmlisttypedconstbuilder.tc_emit_enumdef(def: tenumdef; var node: tnode); + var + equal: boolean; begin if node.nodetype=ordconstn then begin - if equal_defs(node.resultdef,def) or + equal:=equal_defs(node.resultdef,def); + if equal or is_subequal(node.resultdef,def) then begin - adaptrange(def,tordconstnode(node).value,false,false,cs_check_range in current_settings.localswitches); - case longint(node.resultdef.size) of + { if equal, the necessary range checking has already been + performed; needed for handling hacks like + const x = tenum(255); } + if not equal then + adaptrange(def,tordconstnode(node).value,false,false,cs_check_range in current_settings.localswitches); + case node.resultdef.size of 1 : ftcb.emit_tai(Tai_const.Create_8bit(Byte(tordconstnode(node).value.svalue)),def); 2 : ftcb.emit_tai(Tai_const.Create_16bit(Word(tordconstnode(node).value.svalue)),def); 4 : ftcb.emit_tai(Tai_const.Create_32bit(Longint(tordconstnode(node).value.svalue)),def); + else + internalerror(2022040301); end; end else diff --git a/tests/tbs/tb0693a.pp b/tests/tbs/tb0693a.pp new file mode 100644 index 0000000000..37c5992cea --- /dev/null +++ b/tests/tbs/tb0693a.pp @@ -0,0 +1,30 @@ +{ %norun } + +program T001; +{$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. diff --git a/tests/tbs/tb0693b.pp b/tests/tbs/tb0693b.pp new file mode 100644 index 0000000000..6f4db39f89 --- /dev/null +++ b/tests/tbs/tb0693b.pp @@ -0,0 +1,32 @@ +{ %fail } + +program T001; +{$MODE DELPHI} +{$R-} +type + TLanguages = ( + lOne, + lTwo, + lThree, + lFour + ); + + TLanguagesSub = lOne..lTwo; + +const + LANGUAGE_NONE = TLanguages(255); + +type + TLanguage = record + Index : TLanguagesSub; + end; + +var + Lang: TLanguages; + CurrentLanguage: TLanguage = ( + Index: LANGUAGE_NONE + ); + +begin + Lang := LANGUAGE_NONE; +end.