public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: gcc-patches@gcc.gnu.org, jason@redhat.com, nathan@acm.org
Subject: Re: [PATCH 1/2] c++ modules: streaming enum with no enumerators [PR102600]
Date: Tue, 18 Oct 2022 14:54:35 -0400 (EDT)	[thread overview]
Message-ID: <33f0f895-4a0d-fb63-6caa-5d33b286ab3a@idea> (raw)
In-Reply-To: <20221018181050.1629201-1-ppalka@redhat.com>

On Tue, 18 Oct 2022, Patrick Palka wrote:

> For an enum declaration, has_definition returns true iff its TYPE_VALUES
> field is non-empty.  But this will wrongly return false for an enum that's
> defined to have no enumerators as in the below testcase.
> 
> This patch fixes has_definition for such enums by checking OPAQUE_ENUM_P
> instead, which should always give us the right answer here.

Whoops, it looks like the other patch[1] alone fixes both this
testcase and its own testcase, and thus this change to has_definition
isn't necessary if the other patch goes in.

The problem in the below testcase is that the enum defines no enumerators,
so has_definition returns false, and therefore we never stream the
ENUMERAL_TYPE's TYPE_MIN/MAX_VALUE, which leads to a crash in the middle
end due to these fields being empty.  The other patch arranges that we
always stream TYPE_MIN/MAX_VALUE for ENUMERAL_TYPE regardless of whether
we think the enum is defined, so this has_definition false negative
doesn't matter anymore.  So feel free to ignore this patch if you think
the second one looks good :)

[1]: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/603831.html

> 
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
> 
> 	PR c++/102600
> 
> gcc/cp/ChangeLog:
> 
> 	* module.cc (has_definition): For an enum declaration, check
> 	OPAQUE_ENUM_P instead of TYPE_VALUES.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/modules/enum-9_a.H: New test.
> 	* g++.dg/modules/enum-9_b.C: New test.
> ---
>  gcc/cp/module.cc                        | 3 ++-
>  gcc/testsuite/g++.dg/modules/enum-9_a.H | 5 +++++
>  gcc/testsuite/g++.dg/modules/enum-9_b.C | 8 ++++++++
>  3 files changed, 15 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/g++.dg/modules/enum-9_a.H
>  create mode 100644 gcc/testsuite/g++.dg/modules/enum-9_b.C
> 
> diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
> index 2c2f9a9a8cb..cc704817718 100644
> --- a/gcc/cp/module.cc
> +++ b/gcc/cp/module.cc
> @@ -11443,7 +11443,8 @@ has_definition (tree decl)
>  	if (type == TYPE_MAIN_VARIANT (type)
>  	    && decl == TYPE_NAME (type)
>  	    && (TREE_CODE (type) == ENUMERAL_TYPE
> -		? TYPE_VALUES (type) : TYPE_FIELDS (type)))
> +		? !OPAQUE_ENUM_P (type)
> +		: TYPE_FIELDS (type) != NULL_TREE))
>  	  return true;
>        }
>        break;
> diff --git a/gcc/testsuite/g++.dg/modules/enum-9_a.H b/gcc/testsuite/g++.dg/modules/enum-9_a.H
> new file mode 100644
> index 00000000000..1aecabfd0bd
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/enum-9_a.H
> @@ -0,0 +1,5 @@
> +// PR c++/102600
> +// { dg-additional-options -fmodule-header }
> +// { dg-module-cmi {} }
> +
> +enum class byte : unsigned char { };
> diff --git a/gcc/testsuite/g++.dg/modules/enum-9_b.C b/gcc/testsuite/g++.dg/modules/enum-9_b.C
> new file mode 100644
> index 00000000000..aa1fdf21444
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/enum-9_b.C
> @@ -0,0 +1,8 @@
> +// PR c++/102600
> +// { dg-additional-options -fmodules-ts }
> +
> +import "enum-9_a.H";
> +
> +void push(byte) {}
> +void write(char v) { push(static_cast<byte>(v)); }
> +int main() { write(char{}); }
> -- 
> 2.38.0.118.g4732897cf0
> 
> 


      parent reply	other threads:[~2022-10-18 18:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-18 18:10 Patrick Palka
2022-10-18 18:10 ` [PATCH 2/2] c++ modules: always stream TYPE_MIN/MAX_VALUE for enums [PR106848] Patrick Palka
2022-10-19  7:31   ` Richard Biener
2022-10-18 18:54 ` Patrick Palka [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=33f0f895-4a0d-fb63-6caa-5d33b286ab3a@idea \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=nathan@acm.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).