From 6318fe77395fc0dd59419bc4d69cd06ac0158e54 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 29 Apr 2020 15:36:35 -0400 Subject: [PATCH] tree: Don't reuse types if TYPE_USER_ALIGN differ [PR94775] Here we trip on the TYPE_USER_ALIGN (t) assert in strip_typedefs: it gets "const d[0]" with TYPE_USER_ALIGN=0 but the result built by build_cplus_array_type is "const char[0]" with TYPE_USER_ALIGN=1. When we strip_typedefs the element of the array "const d", we see it's a typedef_variant_p, so we look at its DECL_ORIGINAL_TYPE, which is char, but we need to add the const qualifier, so we call cp_build_qualified_type -> build_qualified_type where get_qualified_type checks to see if we already have such a type by walking the variants list, which in this case is: char -> c -> const char -> const char -> d -> const d Because check_base_type only checks TYPE_ALIGN and not TYPE_USER_ALIGN, we choose the first const char, which has TYPE_USER_ALIGN set. If the element type of an array has TYPE_USER_ALIGN, the array type gets it too. So we can make check_base_type stricter. I was afraid that it might make us reuse types less often, but measuring showed that we build the same amount of types with and without the patch, while bootstrapping. PR c++/94775 * tree.c (check_base_type): Return true only if TYPE_USER_ALIGN match. (check_aligned_type): Check if TYPE_USER_ALIGN match. * g++.dg/warn/Warray-bounds-10.C: New test. --- gcc/ChangeLog | 6 +++++ gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/g++.dg/warn/Warray-bounds-10.C | 40 ++++++++++++++++++++++++++++ gcc/tree.c | 4 ++- 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/warn/Warray-bounds-10.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c6e2447cd5c..f83e7ac13de 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2020-04-30 Marek Polacek + + PR c++/94775 + * tree.c (check_base_type): Return true only if TYPE_USER_ALIGN match. + (check_aligned_type): Check if TYPE_USER_ALIGN match. + 2020-04-30 Kyrylo Tkachov * config/aarch64/aarch64.h (TARGET_OUTLINE_ATOMICS): Define. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 66ac6705a05..49f00426e06 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-04-30 Marek Polacek + + PR c++/94775 + * g++.dg/warn/Warray-bounds-10.C: New test. + 2020-04-30 Szabolcs Nagy PR target/94748 diff --git a/gcc/testsuite/g++.dg/warn/Warray-bounds-10.C b/gcc/testsuite/g++.dg/warn/Warray-bounds-10.C new file mode 100644 index 00000000000..0a18f637e0e --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Warray-bounds-10.C @@ -0,0 +1,40 @@ +// PR c++/94775 +// { dg-do compile { target c++14 } } +// { dg-options "-O2 -Warray-bounds" } + +template using a = int; +template using b = int; +typedef char d; +template using e = int; +template struct h { using i = b>, e>; }; +template using j = typename h::i; +long ab, k, aj; +const d l[]{}; +class m { +public: + m(int); +}; +class n { + void ad() const; + template void o(long) const { + using c __attribute__((aligned(1))) = const ae; + } + long p; + template + auto s(unsigned long, unsigned long, unsigned long, unsigned long) const; + template auto q(unsigned long, unsigned long) const; +}; +template +auto n::s(unsigned long, unsigned long, unsigned long, unsigned long t) const { + o(p); + return t; +} +template auto n::q(unsigned long p1, unsigned long p2) const { + using r = j<4, false>; + using ai = j<4, g>; + return s(ab, k, p1, p2); +} +void n::ad() const { + long f(l[aj]); // { dg-warning "outside array bounds" } + m(q(8, f)); +} diff --git a/gcc/tree.c b/gcc/tree.c index e451401822c..341766c51e5 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -6493,7 +6493,8 @@ check_base_type (const_tree cand, const_tree base) TYPE_ATTRIBUTES (base))) return false; /* Check alignment. */ - if (TYPE_ALIGN (cand) == TYPE_ALIGN (base)) + if (TYPE_ALIGN (cand) == TYPE_ALIGN (base) + && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base)) return true; /* Atomic types increase minimal alignment. We must to do so as well or we get duplicated canonical types. See PR88686. */ @@ -6528,6 +6529,7 @@ check_aligned_type (const_tree cand, const_tree base, unsigned int align) && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base) /* Check alignment. */ && TYPE_ALIGN (cand) == align + && TYPE_USER_ALIGN (cand) == TYPE_USER_ALIGN (base) && attribute_list_equal (TYPE_ATTRIBUTES (cand), TYPE_ATTRIBUTES (base)) && check_lang_type (cand, base)); -- 2.11.4.GIT