diff mbox series

[Ada] Spurious error on fixed-point operation whose operands are expressions

Message ID 20180525090814.GA34863@adacore.com
State New
Headers show
Series [Ada] Spurious error on fixed-point operation whose operands are expressions | expand

Commit Message

Pierre-Marie de Rodat May 25, 2018, 9:08 a.m. UTC
This patch fixes a spurious error in a fixed-point operand of a multiplying
operator M when the operand is an adding operation and the context imposes
a different fixed-point type to the result of M.

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

2018-05-25  Ed Schonberg  <schonberg@adacore.com>

gcc/ada/

	* sem_res.adb (Set_Mixed_Mode_Operand): If the operand is an expression
	of a fixed point type and the parent is a multiplying operation,
	resolve the operand with its own type because the context will impose a
	resulting type on the result of the multiplication by means of
	approriate conversion.

gcc/testsuite/

	* gnat.dg/fixedpnt4.adb: New testcase.
diff mbox series

Patch

--- gcc/ada/sem_res.adb
+++ gcc/ada/sem_res.adb
@@ -5283,9 +5283,19 @@  package body Sem_Res is
          elsif Etype (N) = T
            and then B_Typ /= Universal_Fixed
          then
-            --  Not a mixed-mode operation, resolve with context
 
-            Resolve (N, B_Typ);
+            --  if the operand is part of a fixed multiplication operation,
+            --  a conversion will be applied to each operand, so resolve it
+            --  with its own type.
+
+            if Nkind_In (Parent (N), N_Op_Multiply, N_Op_Divide)  then
+               Resolve (N);
+
+            else
+               --  Not a mixed-mode operation, resolve with context
+
+               Resolve (N, B_Typ);
+            end if;
 
          elsif Etype (N) = Any_Fixed then
 

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/fixedpnt4.adb
@@ -0,0 +1,14 @@ 
+--  { dg-do compile }
+
+procedure Fixedpnt4 is
+   type T is delta 2.0/5.0 range -10.0 .. 10.0 with Small => 2.0/5.0;
+   type T2 is delta 1.0/25.0 range -10.0 .. 10.0 with Small => 1.0/25.0;
+
+   X : T := 1.0;
+   Y : T2;
+begin
+   Y := X / X;
+   Y := X / (X + X);
+   Y := X / (X + X + 1.0);
+   Y := (X + X) * (X + X);
+end;