public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] ada: Spurious error on string interpolation
@ 2023-05-29  8:28 Marc Poulhiès
  0 siblings, 0 replies; only message in thread
From: Marc Poulhiès @ 2023-05-29  8:28 UTC (permalink / raw)
  To: gcc-patches; +Cc: Javier Miranda

From: Javier Miranda <miranda@adacore.com>

The frontend reports spurious errors on operators found in
interpolated string literals.

gcc/ada/

	* scans.ads (Inside_Interpolated_String_Expression): New variable.
	* par-ch2.adb (P_Interpolated_String_Literal): Set/clear new
	variable when parsing interpolated string expressions.
	* scng.adb (Set_String): Skip processing operator symbols when we
	arescanning an interpolated string literal.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/par-ch2.adb | 17 +++++++++++++----
 gcc/ada/scans.ads   |  3 +++
 gcc/ada/scng.adb    | 10 +++++++++-
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/gcc/ada/par-ch2.adb b/gcc/ada/par-ch2.adb
index b6814bdec17..af92f5ac353 100644
--- a/gcc/ada/par-ch2.adb
+++ b/gcc/ada/par-ch2.adb
@@ -225,6 +225,7 @@ package body Ch2 is
    function P_Interpolated_String_Literal return Node_Id is
       Elements_List : constant List_Id := New_List;
       NL_Node       : Node_Id;
+      Saved_State   : constant Boolean := Inside_Interpolated_String_Literal;
       String_Node   : Node_Id;
 
    begin
@@ -245,9 +246,17 @@ package body Ch2 is
             --  Interpolated expression
 
             if Token = Tok_Left_Curly_Bracket then
-               Scan; --  past '{'
-               Append_To (Elements_List, P_Expression);
-               T_Right_Curly_Bracket;
+               declare
+                  Saved_In_Expr : constant Boolean :=
+                    Inside_Interpolated_String_Expression;
+
+               begin
+                  Scan; --  past '{'
+                  Inside_Interpolated_String_Expression := True;
+                  Append_To (Elements_List, P_Expression);
+                  Inside_Interpolated_String_Expression := Saved_In_Expr;
+                  T_Right_Curly_Bracket;
+               end;
             else
                if Prev_Token = Tok_String_Literal then
                   NL_Node := New_Node (N_String_Literal, Token_Ptr);
@@ -266,7 +275,7 @@ package body Ch2 is
          end loop;
       end if;
 
-      Inside_Interpolated_String_Literal := False;
+      Inside_Interpolated_String_Literal := Saved_State;
       Set_Expressions (String_Node, Elements_List);
 
       return String_Node;
diff --git a/gcc/ada/scans.ads b/gcc/ada/scans.ads
index 19e13b6c703..00381bb4a55 100644
--- a/gcc/ada/scans.ads
+++ b/gcc/ada/scans.ads
@@ -482,6 +482,9 @@ package Scans is
    --  or aspect. Used to allow/require nonstandard style rules for =>+ with
    --  -gnatyt.
 
+   Inside_Interpolated_String_Expression : Boolean := False;
+   --  True while parsing an interpolated string expression
+
    Inside_Interpolated_String_Literal : Boolean := False;
    --  True while parsing an interpolated string literal
 
diff --git a/gcc/ada/scng.adb b/gcc/ada/scng.adb
index abf9c68cd3d..c2707df5cab 100644
--- a/gcc/ada/scng.adb
+++ b/gcc/ada/scng.adb
@@ -951,12 +951,20 @@ package body Scng is
             C3   : Character;
 
          begin
+            --  Skip processing operator symbols if we are scanning an
+            --  interpolated string literal.
+
+            if Inside_Interpolated_String_Literal
+              and then not Inside_Interpolated_String_Expression
+            then
+               null;
+
             --  Token_Name is currently set to Error_Name. The following
             --  section of code resets Token_Name to the proper Name_Op_xx
             --  value if the string is a valid operator symbol, otherwise it is
             --  left set to Error_Name.
 
-            if Slen = 1 then
+            elsif Slen = 1 then
                C1 := Source (Token_Ptr + 1);
 
                case C1 is
-- 
2.40.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-29  8:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-29  8:28 [COMMITTED] ada: Spurious error on string interpolation Marc Poulhiès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).