From d412ff42c94212209fdf7c6f4137e291c4d15393 Mon Sep 17 00:00:00 2001 From: svenbarth Date: Fri, 15 Sep 2017 21:09:21 +0000 Subject: * fix for Mantis #32355: adjust the meaning of the typehelpers modeswitch for Delphi modes in that it enables the "type helper" syntax as it is in the non-Delphi modes; extending primitive types with record helpers is now always enabled in Delphi modes + added test git-svn-id: https://svn.freepascal.org/svn/fpc/trunk@37225 3ad0048d-3df7-0310-abae-a5850022a9f2 --- compiler/globals.pas | 2 +- compiler/globtype.pas | 4 ++-- compiler/pdecobj.pas | 2 +- compiler/ptype.pas | 3 +-- tests/test/tthlp25.pp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 tests/test/tthlp25.pp diff --git a/compiler/globals.pas b/compiler/globals.pas index b75e04589e..f429465e10 100644 --- a/compiler/globals.pas +++ b/compiler/globals.pas @@ -54,7 +54,7 @@ interface [m_delphi,m_class,m_objpas,m_result,m_string_pchar, m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring, m_out,m_default_para,m_duplicate_names,m_hintdirective, - m_property,m_default_inline,m_except,m_advanced_records,m_type_helpers]; + m_property,m_default_inline,m_except,m_advanced_records]; delphiunicodemodeswitches = delphimodeswitches + [m_systemcodepage,m_default_unicodestring]; fpcmodeswitches = [m_fpc,m_string_pchar,m_nested_comment,m_repeat_forward, diff --git a/compiler/globtype.pas b/compiler/globtype.pas index b01960335a..bdf2277da7 100644 --- a/compiler/globtype.pas +++ b/compiler/globtype.pas @@ -425,8 +425,8 @@ interface fields in Java) } m_default_unicodestring, { makes the default string type in $h+ mode unicodestring rather than ansistring; similarly, char becomes unicodechar rather than ansichar } - m_type_helpers, { allows the declaration of "type helper" (non-Delphi) or "record helper" - (Delphi) for primitive types } + m_type_helpers, { allows the declaration of "type helper" for all supported types + (primitive types, records, classes, interfaces) } m_blocks, { support for http://en.wikipedia.org/wiki/Blocks_(C_language_extension) } m_isolike_io, { I/O as it required by an ISO compatible compiler } m_isolike_program_para, { program parameters as it required by an ISO compatible compiler } diff --git a/compiler/pdecobj.pas b/compiler/pdecobj.pas index 8af9edc2f8..1a2014f038 100644 --- a/compiler/pdecobj.pas +++ b/compiler/pdecobj.pas @@ -780,7 +780,7 @@ implementation { primitive types are allowed for record helpers in mode delphi } (hdef.typ<>recorddef) and - not (m_type_helpers in current_settings.modeswitches) + not (m_delphi in current_settings.modeswitches) ) then Message1(type_e_record_type_expected,hdef.typename) else diff --git a/compiler/ptype.pas b/compiler/ptype.pas index 1866b26035..6e5aca5450 100644 --- a/compiler/ptype.pas +++ b/compiler/ptype.pas @@ -1890,8 +1890,7 @@ implementation _HELPER: begin if hadtypetoken and - { don't allow "type helper" in mode delphi and require modeswitch typehelpers } - ([m_delphi,m_type_helpers]*current_settings.modeswitches=[m_type_helpers]) then + (m_type_helpers in current_settings.modeswitches) then begin { reset hadtypetoken, so that calling code knows that it should not be handled as a "unique" type } diff --git a/tests/test/tthlp25.pp b/tests/test/tthlp25.pp new file mode 100644 index 0000000000..7d01211802 --- /dev/null +++ b/tests/test/tthlp25.pp @@ -0,0 +1,47 @@ +program tthlp25; + +{$mode delphi} +{$modeswitch typehelpers} + +type + TLongIntHelper = type helper for LongInt + function Test: LongInt; + end; + + TTest = record + end; + + TTestHelper = record helper for TTest + function Test: LongInt; + end; + + TTestHelperSub = type helper(TTestHelper) for TTest + function Test: LongInt; + end; + +function TLongIntHelper.Test: LongInt; +begin + Result := Self * 2; +end; + +function TTestHelper.Test: LongInt; +begin + Result := 4; +end; + +function TTestHelperSub.Test: LongInt; +begin + Result := inherited Test * 2; +end; + +var + i: LongInt; + t: TTest; +begin + i := 2; + if i.Test <> 4 then + Halt(1); + if t.Test <> 8 then + Halt(2); + Writeln('ok'); +end. -- cgit v1.2.1