From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E1C75385741C; Fri, 29 Apr 2022 11:42:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1C75385741C From: "byteslice at airmail dot cc" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/105420] Bogus -Warray-bounds with non-compile time-constant variable Date: Fri, 29 Apr 2022 11:42:32 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: byteslice at airmail dot cc X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: attachments.created Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Apr 2022 11:42:33 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105420 --- Comment #2 from Liam White --- Created attachment 52906 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D52906&action=3Dedit Preprocessed source The first attachment is automatically and manually reduced from the followi= ng source: ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio, s32 virt_core, KProcess* owner, ThreadType t= ype) { // Assert parameters are valid. ASSERT((type =3D=3D ThreadType::Main) || (type =3D=3D ThreadType::Dummy= ) || (Svc::HighestThreadPriority <=3D prio && prio <=3D Svc::LowestThreadPriority)); ASSERT((owner !=3D nullptr) || (type !=3D ThreadType::User)); ASSERT(0 <=3D virt_core && virt_core < static_cast(Common::BitSize())); // Convert the virtual core to a physical core. const s32 phys_core =3D Core::Hardware::VirtualToPhysicalCoreMap[virt_c= ore]; ASSERT(0 <=3D phys_core && phys_core < static_cast(Core::Hardware::NUM_CPU_CORES)); // ... } And it produces this compiler error (compile this preprocessed source with = -O2 -Werror=3Darray-bounds -std=3Dgnu++20): In static member function =E2=80=98static constexpr _Tp& std::__array_trait= s<_Tp, _Nm>::_S_ref(const _Tp (&)[_Nm], std::size_t) [with _Tp =3D int; long unsig= ned int _Nm =3D 64]=E2=80=99, inlined from =E2=80=98constexpr const std::array<_Tp, _Nm>::value_type& std::array<_Tp, _Nm>::operator[](size_type) const [with _Tp =3D int; long unsigned int _Nm =3D 64]=E2=80=99 at k_thread.cpp:59748:25, inlined from =E2=80=98ResultCode Kernel::KThread::Initialize(Kernel::KThreadFunction, uintptr_t, VAddr, s32, s32, Kernel::KProcess*, Kernel::ThreadType)=E2=80=99 at k_thread.cpp:102280= :77: k_thread.cpp:59638:36: error: array subscript 64 is above array bounds of =E2=80=98std::__array_traits::_Type=E2=80=99 {aka =E2=80=98const i= nt [64]=E2=80=99} [-Werror=3Darray-bounds] 59638 | { return const_cast<_Tp&>(__t[__n]); } | ~~~^ k_thread.cpp: In member function =E2=80=98ResultCode Kernel::KThread::Initialize(Kernel::KThreadFunction, uintptr_t, VAddr, s32, s32, Kernel::KProcess*, Kernel::ThreadType)=E2=80=99: k_thread.cpp:77557:51: note: while referencing =E2=80=98Core::Hardware::VirtualToPhysicalCoreMap=E2=80=99 77557 | constexpr std::array()> VirtualToPhysicalCoreMap{ |=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20 ^~~~~~~~~~~~~~~~~~~~~~~~ cc1plus: some warnings being treated as errors I generally believe that this is a bug in the compiler as the array is similarly never accessed with the constant value 64.=