This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Spurious error on case statement with conversion and static predicate


This patch fixes a spurious error on a case statement whose expression is a
type conversion of a variable whose initial value is known, and whose type
has a static predicate.

Executing:

   gnatmake -q static_predicate_example
   static_predicate_example
   gnatmake -q -f -gnata static_predicate_example
   static_predicate_example

must yield:

   woof
   woof
   woof
   woof

---
with Ada.Text_Io;

procedure Static_Predicate_Example is

  type Animal_Type is (Bear, Cat, Dog, Horse, Wolf);

  subtype Pet_Type is Animal_Type with Static_Predicate =>
    (case Pet_Type is
       when Cat | Dog | Horse => True,
       when Bear | Wolf       => False);

    My_Animal           : Animal_Type := Dog;
    My_Animal_Converted : Pet_Type    := Pet_Type (My_Animal);
begin
  if My_Animal in Pet_Type then
    case Pet_Type (My_Animal) is
      when Cat   => Ada.Text_Io.Put_Line ("meow");
      when Dog   => Ada.Text_Io.Put_Line ("woof");
      when Horse => Ada.Text_Io.Put_Line ("eeehehe");
    end case;
  end if;

  case My_Animal_Converted is
    when Cat   => Ada.Text_Io.Put_Line ("meow");
    when Dog   => Ada.Text_Io.Put_Line ("woof");
    when Horse => Ada.Text_Io.Put_Line ("eeehehe");
  end case;
end Static_Predicate_Example;

Tested on x86_64-pc-linux-gnu, committed on trunk

2017-01-06  Ed Schonberg  <schonberg@adacore.com>

	* sem_eval.adb (Check_Expression_Against_Static_Predicate):
	If expression is compile-time known and obeys a static predicate
	it must be labelled as static, to prevent spurious warnings and
	run-time errors, e.g. in case statements. This is relevant when
	the expression is the result of constant-folding a type conversion
	whose expression is a variable with a known static value.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]