Bug 78173 - Hard error subtracting pointers to incomplete type in SFINAE context
Summary: Hard error subtracting pointers to incomplete type in SFINAE context
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 11.0
Assignee: Patrick Palka
URL:
Keywords: rejects-valid
: 87282 89300 92089 (view as bug list)
Depends on:
Blocks: concepts
  Show dependency treegraph
 
Reported: 2016-10-31 17:13 UTC by Casey Carter
Modified: 2021-12-03 07:49 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-12-10 00:00:00


Attachments
Minimal repro (273 bytes, text/plain)
2016-10-31 17:13 UTC, Casey Carter
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Casey Carter 2016-10-31 17:13:05 UTC
Created attachment 39934 [details]
Minimal repro

This TU, which should compile without errors:

template<class T>
T&& declval();

template<class...>
using void_t = void;

template<class, class = void>
struct foo { static const bool value = false; };
template<class T>
struct foo<T, void_t<decltype(declval<T>() - declval<T>())>> {
    static const bool value = true;
};

struct A;

#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)

STATIC_ASSERT(!foo<void*>::value); // error: invalid use of 'void'
STATIC_ASSERT(!foo<A*>::value); // error: invalid use of incomplete type 'struct A'

instead produces diagnostics (http://melpon.org/wandbox/permlink/EwcDiz7ZuqpFXdHZ):

prog.cc: In substitution of 'template<class T> struct foo<T, void_t<decltype ((declval<T>() - declval<T>()))> > [with T = void*]':
prog.cc:22:1:   required from here
prog.cc:10:44: error: invalid use of 'void'
 struct foo<T, void_t<decltype(declval<T>() - declval<T>())>> {
                               ~~~~~~~~~~~~~^~~~~~~~~~~~~~
prog.cc: In substitution of 'template<class T> struct foo<T, void_t<decltype ((declval<T>() - declval<T>()))> > [with T = A*]':
prog.cc:23:1:   required from here
prog.cc:10:44: error: invalid use of incomplete type 'struct A'
prog.cc:14:8: note: forward declaration of 'struct A'
 struct A;
        ^
Comment 1 Casey Carter 2018-07-14 01:36:31 UTC
This still reproduces on trunk, and in concepts land. This program fragment:

  template <class T>
  concept bool CanDifference = requires(T const& x, T const& y) {
      x - y;
  };

  static_assert(!CanDifference<void*>);

produces diagnostics when compiled with "g++ -std=c++2a -fconcepts" (https://godbolt.org/g/e36eFK):

  <source>:3:7: error: invalid use of 'void'
       x - y;
       ~~^~~
Comment 2 Jonathan Wakely 2019-02-13 22:12:57 UTC
*** Bug 89300 has been marked as a duplicate of this bug. ***
Comment 3 Jonathan Wakely 2019-10-15 12:03:42 UTC
*** Bug 92089 has been marked as a duplicate of this bug. ***
Comment 4 Patrick Palka 2020-12-10 15:05:15 UTC
Looking.
Comment 5 GCC Commits 2020-12-11 14:42:32 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:958d42abdf1a1936d290aab126cebd6bb4a52361

commit r11-5936-g958d42abdf1a1936d290aab126cebd6bb4a52361
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Dec 11 09:40:58 2020 -0500

    c++: missing SFINAE with pointer subtraction [PR78173]
    
    This fixes a missed SFINAE when subtracting pointers to an incomplete
    type.
    
    gcc/cp/ChangeLog:
    
            PR c++/78173
            * typeck.c (pointer_diff): Use complete_type_or_maybe_complain
            instead of complete_type_or_else.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/78173
            * g++.dg/cpp2a/concepts-pr78173.C: New test.
Comment 6 Patrick Palka 2020-12-11 14:48:02 UTC
Fixed for GCC 11
Comment 7 GCC Commits 2020-12-15 11:46:01 UTC
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:fa452a843d26a64a5ca0fd7c17ea1bd6e1b81a69

commit r11-6074-gfa452a843d26a64a5ca0fd7c17ea1bd6e1b81a69
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Dec 15 11:40:07 2020 +0000

    libstdc++: Remove workaround for PR c++/78173
    
    Now that the G++ bug is fixed we no longer need to protect this partial
    specialization from complaining about subtracting void pointers.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/iterator_concepts.h (incrementable_traits<Tp>):
            Remove workaround for PR c++/78173.
Comment 8 Andrew Pinski 2021-12-03 07:49:31 UTC
*** Bug 87282 has been marked as a duplicate of this bug. ***