[PATCH] c++: Fix ICE during explanation of failed __has_unique_object_representation [PR126867]

Jakub Jelinek <[email protected]>
Newsgroups gmane.comp.gcc.patches
Message-ID <aoQNt-31yFVXpUUd@tucnak>
Hi!

The following testcase ICEs since r16-5520 added explain
arguments to type_has_unique_obj_representations and
record_has_unique_obj_representations.  Most of the location_t
arguments in that change look correct, either DECL_SOURCE_LOCATION
of some FIELD_DECL or in type_has_unique_obj_representations use
loc, which is initialized to
  location_t loc = input_location;
  if (tree m = TYPE_MAIN_DECL (t))
    loc = DECL_SOURCE_LOCATION (m);
But in record_has_unique_obj_representations we don't have
a variable like that and t at that point is the RECORD_TYPE
or UNION_TYPE, so using DECL_SOURCE_LOCATION on it will surely ICE
in a checking compiler.
location_of call does the above dances, so it seems easiest to
call it rather than duplicate it by hand.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/16.3?

2026-08-17  Jakub Jelinek  <[email protected]>

	PR c++/126867
	* tree.cc (record_has_unique_obj_representations): Use
	location_of (const_cast <tree> (t)) rather than
	DECL_SOURCE_LOCATION (t).

	* g++.dg/cpp1z/has-unique-obj-representations6.C: New test.

--- gcc/cp/tree.cc.jj	2026-07-24 18:38:54.337025261 +0200
+++ gcc/cp/tree.cc	2026-08-17 12:32:40.551645418 +0200
@@ -5274,7 +5274,7 @@ record_has_unique_obj_representations (c
 	    inform (DECL_SOURCE_LOCATION (last_named_field),
 		    "padding occurs after %qD", last_named_field);
 	  else
-	    inform (DECL_SOURCE_LOCATION (t),
+	    inform (location_of (const_cast <tree> (t)),
 		    "%qT has padding and no data fields", t);
 	}
       return false;
--- gcc/testsuite/g++.dg/cpp1z/has-unique-obj-representations6.C.jj	2026-08-17 12:37:22.573052447 +0200
+++ gcc/testsuite/g++.dg/cpp1z/has-unique-obj-representations6.C	2026-08-17 12:38:16.061371108 +0200
@@ -0,0 +1,10 @@
+// PR c++/126867
+// { dg-do compile { target c++17 } }
+
+template <class T>
+constexpr bool U = __has_unique_object_representations (T);
+
+struct S {};		// { dg-message "'S' does not have unique object representations, because" }
+			// { dg-message "'S' has padding and no data fields" "" { target *-*-* } .-1 }
+
+static_assert (U <S>);	// { dg-error "static assertion failed" }

	Jakub
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.