[Ada] Fix predicate check on object declaration
commit2977b006df03998f6c773f593600881348a4e517
authorMarc Poulhiès <poulhies@adacore.com>
Fri, 22 Apr 2022 15:52:49 +0000 (22 17:52 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Wed, 1 Jun 2022 08:43:19 +0000 (1 08:43 +0000)
tree5734554d786a10be1436ca2f0aede782b50ccad2
parent9ba4b38f6ac6d80130808943f20011ab8855695d
[Ada] Fix predicate check on object declaration

When subtype predicate checks are added for object declarations, it
could lead to a compiler crash or to an incorrect check.

When the subtype for the object being declared is built later by
Analyze_Object_Declaration, the predicate check can't be applied on the
object instead of a copy as the call will be incorrect after the subtype
has been built.

When subtypes for LHS and RHS do not statically match, only checking the
predicate on the object after it has been initialized may miss a failing
predicate on the RHS.

In both cases, skip the optimization and check the predicate on a copy.

Rename Should_Build_Subtype into Build_Default_Subtype_OK and move it
out of sem_ch3 to make it available to other part of the compiler (in
particular to checks.adb).

gcc/ada/

* checks.adb (Apply_Predicate_Check): Refine condition for
applying optimization.
* sem_ch3.adb (Analyze_Component_Declaration): Adjust calls to
Should_Build_Subtype.
(Analyze_Object_Declaration): Likewise.
(Should_Build_Subtype): Rename/move to ...
* sem_util.ads (Build_Default_Subtype_OK): ... this.
* sem_util.adb (Build_Default_Subtype_OK): Moved from
sem_ch3.adb.
gcc/ada/checks.adb
gcc/ada/sem_ch3.adb
gcc/ada/sem_util.adb
gcc/ada/sem_util.ads