Bug 85254 - boost::is_final does not work for template types
Summary: boost::is_final does not work for template types
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.2.0
: P3 normal
Target Milestone: 10.0
Assignee: Ville Voutilainen
URL:
Keywords: rejects-valid, wrong-code
Depends on:
Blocks:
 
Reported: 2018-04-06 12:16 UTC by andysem
Modified: 2019-10-26 17:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-04-06 00:00:00


Attachments
Preprocessed test case (94.11 KB, text/plain)
2018-04-06 12:16 UTC, andysem
Details
Compilable test case (194 bytes, text/x-csrc)
2018-04-06 12:17 UTC, andysem
Details

Note You need to log in before you can comment on or make changes to this bug.
Description andysem 2018-04-06 12:16:46 UTC
Created attachment 43868 [details]
Preprocessed test case

This problem was initially reported to Boost here:

https://github.com/boostorg/type_traits/issues/66

The test case is this:

#include <boost/type_traits/is_final.hpp>
#include <type_traits>
#include <iostream>

struct final1 final {};
template <typename T> struct final2 final {};

int main() {
    std::cout << "boost::is_final<final1>: " << boost::is_final<final1>::value << std::endl;
    std::cout << "boost::is_final<final2<int>>::value: " << boost::is_final< final2<int> >::value << std::endl;
    std::cout << "std::is_final<final1>::value: " << std::is_final<final1>::value << std::endl;
    std::cout << "std::is_final<final2<int>>::value: " << std::is_final< final2<int> >::value << std::endl;
}

The output with gcc 6.4 and 7.2:

boost::is_final<final1>: 1
boost::is_final<final2<int>>::value: 0
std::is_final<final1>::value: 1
std::is_final<final2<int>>::value: 1

The expected output should be all 1s.

I suspect the compiler intrinsic __is_final produces incorrect result because of remove_cv for some reason. boost::is_final is defined like this:

template <class T> struct is_final : public integral_constant<bool, BOOST_IS_FINAL(typename remove_cv<T>::type)> {};

where BOOST_IS_FINAL is defined to __is_final. This is similar to std::is_final except for the remove_cv trait. If I remove remove_cv then the test results are as expected. remove_cv is implemented this way:

template <class T> struct remove_cv{ typedef T type; };
template <class T> struct remove_cv<T const>{ typedef T type;  };
template <class T> struct remove_cv<T volatile>{ typedef T type; };
template <class T> struct remove_cv<T const volatile>{ typedef T type; };

plus the similar specializations for array types, which are irrelevant to this test.

Unfortunately, I could not reproduce the problem with a more reduced testcase, including when I re-implemented is_final and remove_cv locally, so it's possible that some surrounding code is at play.

I'm attaching a preprocessed source a compilable test code. Compile with:

g++ -I<path_to_boost> -o is_final_test is_final_test.cpp

I'm using Boost develop (pre-1.67), but the problem reproduces with Boost 1.62 that comes with Kubuntu 17.10 and likely any newer versions.

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.2.0-8ubuntu3.2' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3.2) 


$ g++-6 -v
Using built-in specs.
COLLECT_GCC=g++-6
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 6.4.0-8ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 6.4.0 20171010 (Ubuntu 6.4.0-8ubuntu1)
Comment 1 andysem 2018-04-06 12:17:21 UTC
Created attachment 43869 [details]
Compilable test case
Comment 2 Jonathan Wakely 2018-04-06 13:28:13 UTC
Reduced to less than 30kloc

namespace boost {

   template <bool val>
   struct integral_constant
   {
      static const bool value = val;
   };

   template <bool val>
   bool const integral_constant<val>::value;

template <class T> struct remove_cv{ typedef T type; };

template <class T> struct is_final
  : integral_constant<__is_final(typename remove_cv<T>::type)> {};

}

struct final1 final {};
template <typename T> struct final2 final {};

static_assert( boost::is_final<final1>::value, "" );
static_assert( boost::is_final<final2<int>>::value, "" );
Comment 3 Jonathan Wakely 2018-04-06 13:42:00 UTC
Reduced a little further:

template <class T> struct remove_cv{ typedef T type; };

template <class T> struct is_final
{
  static const bool value = __is_final(typename remove_cv<T>::type);
};

struct final1 final {};
template <typename T> struct final2 final {};

static_assert( is_final<final1>::value, "" );
static_assert( is_final<final2<int>>::value, "" );


This should compile but fails with:

is_final_test.cpp:12:16: error: static assertion failed
 static_assert( is_final<final2<int>>::value, "" );
                ^~~~~~~~~~~~~~~~~~~~~
Comment 4 Jonathan Wakely 2018-05-31 13:52:32 UTC
I hoped this would fix it, but it didn't:

--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -9010,6 +9010,7 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
       return type_code1 == ENUMERAL_TYPE;
 
     case CPTK_IS_FINAL:
+      type1 = TYPE_MAIN_VARIANT (type1);
       return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
 
     case CPTK_IS_LITERAL_TYPE:
Comment 5 Ville Voutilainen 2019-05-31 22:42:35 UTC
Mine. Patch available: https://gcc.gnu.org/ml/gcc-patches/2019-05/msg02183.html
Comment 6 ville 2019-06-01 10:57:43 UTC
Author: ville
Date: Sat Jun  1 10:57:12 2019
New Revision: 271835

URL: https://gcc.gnu.org/viewcvs?rev=271835&root=gcc&view=rev
Log:
PR c++/85254

gcc/cp

PR c++/85254
* class.c (fixup_type_variants): Handle CLASSTYPE_FINAL.

testsuite/

PR c++/85254
* g++.dg/ext/is_final.C: Amend.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c
    trunk/gcc/testsuite/g++.dg/ext/is_final.C
Comment 7 Jason Merrill 2019-10-14 19:45:16 UTC
Fixed for GCC 10.
Comment 8 ville 2019-10-26 17:32:56 UTC
Author: ville
Date: Sat Oct 26 17:32:24 2019
New Revision: 277474

URL: https://gcc.gnu.org/viewcvs?rev=277474&root=gcc&view=rev
Log:
PR c++/85254

Backport from mainline
2019-06-01  Ville Voutilainen  <ville.voutilainen@gmail.com>

	PR c++/85254
	* class.c (fixup_type_variants): Handle CLASSTYPE_FINAL.

Modified:
    branches/gcc-9-branch/gcc/cp/ChangeLog
    branches/gcc-9-branch/gcc/cp/class.c
    branches/gcc-9-branch/gcc/testsuite/g++.dg/ext/is_final.C