Resolves: RHEL-66938 - Fix precedence of casts

This commit is contained in:
Jitka Plesnikova 2024-11-11 13:20:08 +01:00
parent 5eb9985b02
commit 76cceb54ea
2 changed files with 92 additions and 1 deletions

View File

@ -0,0 +1,85 @@
From 4e315cdd7c9072fd33ac1df3d208a0990b8231c3 Mon Sep 17 00:00:00 2001
From: Olly Betts <olly@survex.com>
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 <str> DOXYGENSTRING
%token <str> 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 <node> program interface declaration swig_directive ;
--
2.47.0

View File

@ -68,7 +68,7 @@
Summary: Connects C/C++/Objective C to some high-level programming languages Summary: Connects C/C++/Objective C to some high-level programming languages
Name: swig Name: swig
Version: 4.3.0 Version: 4.3.0
Release: 2%{?dist} Release: 3%{?dist}
License: GPL-3.0-or-later AND BSD-3-Clause License: GPL-3.0-or-later AND BSD-3-Clause
URL: https://www.swig.org/ URL: https://www.swig.org/
Source0: http://downloads.sourceforge.net/project/swig/swig/swig-%{version}/swig-%{version}.tar.gz Source0: http://downloads.sourceforge.net/project/swig/swig/swig-%{version}/swig-%{version}.tar.gz
@ -79,6 +79,9 @@ Source2: description-ccache.h2m
Source3: ccache-swig.sh Source3: ccache-swig.sh
Source4: ccache-swig.csh Source4: ccache-swig.csh
%endif %endif
# Fix precedence of casts - in upstream after 4.3.0
# https://github.com/swig/swig/issues/3058
Patch1: swig-4.3.0-Fix-precedence-of-casts.patch
BuildRequires: coreutils BuildRequires: coreutils
BuildRequires: findutils BuildRequires: findutils
@ -362,6 +365,9 @@ install -pm 644 Tools/swig.gdb %{buildroot}%{_datadir}/%{name}/gdb
%{_datadir}/%{name}/gdb %{_datadir}/%{name}/gdb
%changelog %changelog
* Mon Nov 11 2024 Jitka Plesnikova <jplesnik@redhat.com> - 4.3.0-3
- Resolves: RHEL-66938 - Fix precedence of casts
* Tue Oct 29 2024 Troy Dawson <tdawson@redhat.com> - 4.3.0-2 * Tue Oct 29 2024 Troy Dawson <tdawson@redhat.com> - 4.3.0-2
- Bump release for October 2024 mass rebuild: - Bump release for October 2024 mass rebuild:
Resolves: RHEL-64018 Resolves: RHEL-64018