diff options
author | Raphaël AMIARD <amiard@adacore.com> | 2024-08-29 12:43:54 +0200 |
---|---|---|
committer | Marc Poulhiès <dkm@gcc.gnu.org> | 2024-10-08 10:37:13 +0200 |
commit | cee753879cdb61c27dd584fcc4932969fb2cd0be (patch) | |
tree | c91aeb77022ff05a36aea05eb421b5f8d92c0027 /gcc/ada/doc/gnat_rm | |
parent | a6fc30b29f2e89057538500bb7cf2e233d4e5e41 (diff) | |
download | gcc-cee753879cdb61c27dd584fcc4932969fb2cd0be.zip gcc-cee753879cdb61c27dd584fcc4932969fb2cd0be.tar.gz gcc-cee753879cdb61c27dd584fcc4932969fb2cd0be.tar.bz2 |
ada: Use semantics from the RFC for declarative items mixed with statements
We want to allow statements lists with declarations *and* an exception
handler. What follows from this is that declarations declared in the
statement list are *not* visible from the exception handler, and that
the following code:
declare
A : Integer := 12;
begin
A : Integer := 15;
<stmts>
exception
when others => ...
Roughly expands to:
declare
A : Integer := 12;
begin
declare
A : Integer := 15;
begin
<stmts>
exception
when others => ...
As such, in the code above, there is no more error triggered for
conflicting declarations of `A`.
Move "Local declarations without block" into curated extensions
Restrict legal local decls in statement lists
Only accept object declarations & renamings, as well as use clauses for
gcc/ada/ChangeLog:
* par-ch11.adb (P_Sequence_Of_Statements): Remove Handled
parameter. Always wrap the statements in a block when there are
declarations in it.
* par-ch5.adb: Adapt call to P_Sequence_Of_Statements Update
outdated comment, remove useless `Style_Checks` pragma.
(P_Sequence_Of_Statements): Don't emit an error in core extensions
mode. Emit an error when a non valid declaration is parsed in
sequence of statements.
* par.adb: Adapt P_Sequence_Of_Statements' signature
* doc/gnat_rm/gnat_language_extensions.rst: Adapt documentation
now.
* gnat_rm.texi: Regenerate.
* gnat_ugn.texi: Regenerate.
Diffstat (limited to 'gcc/ada/doc/gnat_rm')
-rw-r--r-- | gcc/ada/doc/gnat_rm/gnat_language_extensions.rst | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/gcc/ada/doc/gnat_rm/gnat_language_extensions.rst b/gcc/ada/doc/gnat_rm/gnat_language_extensions.rst index b29f23c..af0da98 100644 --- a/gcc/ada/doc/gnat_rm/gnat_language_extensions.rst +++ b/gcc/ada/doc/gnat_rm/gnat_language_extensions.rst @@ -53,9 +53,12 @@ Features activated via ``-gnatX`` or Local Declarations Without Block -------------------------------- -A basic_declarative_item may appear at the place of any statement. -This avoids the heavy syntax of block_statements just to declare -something locally. +A ``basic_declarative_item`` may appear at the place of any statement. This +avoids the heavy syntax of block_statements just to declare something locally. + +The only valid kind of declarations for now are ``object_declaration``, +``object_renaming_declaration``, ``use_package_clause`` and +``use_type_clause``. For example: @@ -69,6 +72,49 @@ For example: X := X + Squared; end if; +.. attention:: + + Note that local declarations in statement lists have their own scope, which + means that: + + 1. Those declarations are not visible from the potential exception handler: + + .. code-block:: ada + + begin + A : Integer + ... + exception + when others => + Put_Line (A'Image) -- ILLEGAL + end; + + 2. The following is legal + + .. code-block:: ada + + declare + A : Integer := 10; + begin + A : Integer := 12; + end; + + because it is roughly expanded into + + .. code-block:: ada + + declare + A : Integer := 10; + begin + declare + A : Integer := 12; + begin + ... + end; + end; + + And as such the second ``A`` declaration is hiding the first one. + Link to the original RFC: https://github.com/AdaCore/ada-spark-rfcs/blob/master/prototyped/rfc-local-vars-without-block.md |