From 4e315cdd7c9072fd33ac1df3d208a0990b8231c3 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Sun, 27 Oct 2024 10:02:11 +1300 Subject: [PATCH] Fix precedence of casts Casts should have the same high precedence as unary plus and minus, but actually had a lower precedence than anything else. This could lead to the wrong type being deduced in obscure cases, but also prevented SWIG deducing a type for expressions such as (0)*1+2 which SWIG parses as a cast and then fixes up afterwards. A bug fixed in 4.3.0 made this latter problem manifest more often (previously type deduction happened to work for (0)*1+2 due to an internal field not getting cleared properly). Fixes #3058 --- CHANGES.current | 11 +++++++++++ Examples/test-suite/cpp11_auto_variable.i | 8 ++++++++ Source/CParse/parser.y | 3 +-- 3 files changed, 20 insertions(+), 2 deletions(-) #diff --git a/CHANGES.current b/CHANGES.current #index 42a2be6e7..f173d84e6 100644 #--- a/CHANGES.current #+++ b/CHANGES.current #@@ -7,6 +7,17 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ # Version 4.4.0 (in progress) # =========================== # #+2024-10-27: olly #+ #3058 Fix precedence of casts, which should have the same high #+ precedence as unary plus and minus, but actually had a lower #+ precedence than anything else. This could lead to the wrong type #+ being deduced in obscure cases, but also prevented SWIG deducing a #+ type for expressions such as (0)*1+2 which SWIG parses as a cast #+ and then fixes up afterwards. A bug fixed in 4.3.0 made this #+ latter problem manifest more often (previously type deduction #+ happened to work for (0)*1+2 due to an internal field not getting #+ cleared properly). #+ # 2024-10-25: olly # [Guile] Allow wrapping anything with a `varout` typemap as a # constant. diff --git a/Examples/test-suite/cpp11_auto_variable.i b/Examples/test-suite/cpp11_auto_variable.i index 7345296b5..e0a14ccd9 100644 --- a/Examples/test-suite/cpp11_auto_variable.i +++ b/Examples/test-suite/cpp11_auto_variable.i @@ -62,3 +62,11 @@ static auto wstring_lit_len2 = sizeof("123" L"456") / sizeof(wchar_t) - 1; //static auto constexpr greeting = "Hello"; %} + +%inline %{ +/* Regression test for #3058 */ +auto CAST_HAD_WRONG_PRECEDENCE1 = (0)*1+2; +auto CAST_HAD_WRONG_PRECEDENCE2 = (0)&1|2; +auto CAST_HAD_WRONG_PRECEDENCE3 = (0)-1|2; +auto CAST_HAD_WRONG_PRECEDENCE4 = (0)+1|2; +%} diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index a229e2e70..f3ed4040a 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -1793,7 +1793,6 @@ static String *add_qualifier_to_declarator(SwigType *type, SwigType *qualifier) %token DOXYGENSTRING %token DOXYGENPOSTSTRING -%precedence CAST %left QUESTIONMARK %left LOR %left LAND @@ -1809,7 +1808,7 @@ static String *add_qualifier_to_declarator(SwigType *type, SwigType *qualifier) %left LSHIFT RSHIFT %left PLUS MINUS %left STAR SLASH MODULO -%precedence UMINUS NOT LNOT +%precedence UMINUS NOT LNOT CAST %token DCOLON %type program interface declaration swig_directive ; -- 2.47.0