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
> 
> 

Reply via email to