public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
@ 2024-06-27  0:31 s.murthy at outlook dot com
  2024-06-27  0:33 ` [Bug c++/115672] " s.murthy at outlook dot com
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-27  0:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

            Bug ID: 115672
           Summary: Incorrect template type parameter deduced in
                    non-deduced context if class template has template
                    template parameter
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: s.murthy at outlook dot com
  Target Milestone: ---

Created attachment 58524
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58524&action=edit
Repro source code with static_asserts such that the program compiles

This issue is related to Bug 115656 in that the two likely have the same or
largely the same underlying cause, but this bug is also distinct because it
exists even after circumventing the situation leading to 115656. I chose to
report this issue separately because IMHO the effect is rather serious due to
incorrect type deduction, but it is obviously up to the maintainers to merge
the two issues or to treat them separately.

Like 115656, this issue is also a regression: no issues in GCC 10.5; issues in
GCC 11.1 and later.

Setup: Class template A with a constrained type parameter and a constrained
template template parameter. A has ctors for both deducing and non-deducing
contexts.

When an A object is created in a non-deduced context (after circumventing the
ordering issue in 115656), the template type parameter is assigned the type
used in the non-deduced ctor call instead of assigning the default specified
for the template type parameter. The incorrect type then propagates to all
entities carrying that deduced type. E.g., a data member that should be
`unsigned` due to that being the default for the template type parameter is
instead made `long` because the non-deducing ctor call has long argument. This
**even though the template type parameter is constrained to be unsigned
integral.**

Repro follows. Remove the template template parameter from A and the issues
disappear. (Comparison at: https://sigcpp.godbolt.org/z/TG4vva8P5)
----------------------------------------------------------------------------

//stub for use in class template A
template<std::unsigned_integral U = unsigned> class dv;

template<std::unsigned_integral U = unsigned, template<std::unsigned_integral>
class V = dv>
struct A 
{
    U u_;

    A(U u) : u_(u) {};

    template<std::signed_integral S>
    A(S s) : u_( s < 0 ? -s : s) {}
};


int main()
{
    A a1(45ul); //use deducing-ctor first to circumvent the issue in 115656
    static_assert(std::same_as<decltype(a1.u_), unsigned long>); //passes,
correct

    A a2(45l); //U should be unsigned
    static_assert(std::same_as<decltype(a2.u_), unsigned>); //fails in GCC
11.1+, incorrect
    static_assert(std::same_as<decltype(a2.u_), long>);     //passes in GCC
11.1+, incorrect

    //a2.u_ = -1;
    //std::cout << a2.u_ << '\n'; //-1 instead of UINT_MAX in GCC 11.1+
}

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
@ 2024-06-27  0:33 ` s.murthy at outlook dot com
  2024-06-27  0:34 ` s.murthy at outlook dot com
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-27  0:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #1 from Sean Murthy <s.murthy at outlook dot com> ---
Created attachment 58525
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58525&action=edit
Pre-processed file for repro source

Produced with GCC 14.1. static_asserts adjusted (negations where it fails
incorrectly) such that program compiles successfully.

g++ -v -save-temps -std=c++20 -Wall -Wextra -pedantic -pedantic-errors
-Werror=pedantic ..\src\main.cpp

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
  2024-06-27  0:33 ` [Bug c++/115672] " s.murthy at outlook dot com
@ 2024-06-27  0:34 ` s.murthy at outlook dot com
  2024-06-27  1:37 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-27  0:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #2 from Sean Murthy <s.murthy at outlook dot com> ---
Created attachment 58526
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58526&action=edit
Compiler output for the repro

Produced with GCC 14.1. static_asserts adjusted (negations where it fails
incorrectly) such that program compiles successfully.

g++ -v -save-temps -std=c++20 -Wall -Wextra -pedantic -pedantic-errors
-Werror=pedantic ..\src\main.cpp

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
  2024-06-27  0:33 ` [Bug c++/115672] " s.murthy at outlook dot com
  2024-06-27  0:34 ` s.murthy at outlook dot com
@ 2024-06-27  1:37 ` pinskia at gcc dot gnu.org
  2024-06-27  1:45 ` [Bug c++/115672] [11/12/13/14/15 Regression] " pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-27  1:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 58527
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58527&action=edit
Remove the inline and change the static_assert to what other compilers should
do

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (2 preceding siblings ...)
  2024-06-27  1:37 ` pinskia at gcc dot gnu.org
@ 2024-06-27  1:45 ` pinskia at gcc dot gnu.org
  2024-06-27  1:52 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-27  1:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.5
            Summary|Incorrect template type     |[11/12/13/14/15 Regression]
                   |parameter deduced in        |Incorrect template type
                   |non-deduced context if      |parameter deduced in
                   |class template has template |non-deduced context if
                   |template parameter          |class template has template
                   |                            |template parameter

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (3 preceding siblings ...)
  2024-06-27  1:45 ` [Bug c++/115672] [11/12/13/14/15 Regression] " pinskia at gcc dot gnu.org
@ 2024-06-27  1:52 ` pinskia at gcc dot gnu.org
  2024-06-27  1:52 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-27  1:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #58527|0                           |1
        is obsolete|                            |

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 58528
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58528&action=edit
Reduced further

The second t argument type in the template definition is the key I think.
Changing it to just class allows the testcase to work.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (4 preceding siblings ...)
  2024-06-27  1:52 ` pinskia at gcc dot gnu.org
@ 2024-06-27  1:52 ` pinskia at gcc dot gnu.org
  2024-06-27  3:22 ` s.murthy at outlook dot com
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-27  1:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-06-27

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (5 preceding siblings ...)
  2024-06-27  1:52 ` pinskia at gcc dot gnu.org
@ 2024-06-27  3:22 ` s.murthy at outlook dot com
  2024-06-28  0:19 ` s.murthy at outlook dot com
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-27  3:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #6 from Sean Murthy <s.murthy at outlook dot com> ---
(In reply to Andrew Pinski from comment #4)
> Created attachment 58528 [details]
> Reduced further
> 
> The second t argument type in the template definition is the key I think.
> Changing it to just class allows the testcase to work.

Yes, both observations I made in my follow up to 115656 apply here as well:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115656#c6

Those two observations give me the inkling the template template parameter and
the template it references both being constrained is likely causing the issue:
each of the observations call for only of those two to be constrained which
causes the compiler to produce the expected output.

Not surprisingly, the third point I made in that 115656 follow up also applies
here: in the initial repro, use type_traits to constrain the class template dv,
and that causes CTAD fail on the deducing ctor, regardless of if/how the
template template parameter is constrained. This behavior suggests to me an
issue with traits-concepts interplay/substitutability. And this behavior goes
all the way back to GCC 10.1, making that particular aspect not a regression
issue (unless something traits-specific pre-C++20 issue exists).

A a1(45ul); //ctad fail if dv template constrained using traits

See: https://sigcpp.godbolt.org/z/GqaPhG17W

BTW thanks to all the GCC devs and maintainers for the awesome product and
follow up. And please forgive me when I use incorrect C++ terminology. I'm just
a 3/10 C++ programmer fumbling my way around. 🙏

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (6 preceding siblings ...)
  2024-06-27  3:22 ` s.murthy at outlook dot com
@ 2024-06-28  0:19 ` s.murthy at outlook dot com
  2024-06-28  0:24 ` s.murthy at outlook dot com
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-28  0:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #7 from Sean Murthy <s.murthy at outlook dot com> ---
I have three other observations I'm having a hard time grasping and worry that
I might be doing something wrong. I share them anyway in the hope they add
value. I will also attach repros for these.

The observations are made by including a new *independent* class template B
which is structurally same as class template A, except it does not have the
template template parameter. 

1. "Contamination" or "bleeding" [for lack of a better word]: Create B objects
after creating A objects B objects, though of an independent class, present the
exact same issues as A objects.

2. "Dependency" of some sort where the issues with B objects disappear if A
objects are not created (though template A still exists).

3. "Order dependency", where if B objects are created *before* creating A
objects, all issues described in this bug report disappear, **including the
issues with A objects**.

I find these specific issues rather perplexing, and I hope someone will help me
understand if I'm doing something wrong here or if the compiler is in the
wrong. If the latter, I'm eager to learn the cause (eventually whenever the
issue is addressed).

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (7 preceding siblings ...)
  2024-06-28  0:19 ` s.murthy at outlook dot com
@ 2024-06-28  0:24 ` s.murthy at outlook dot com
  2024-06-28  0:27 ` s.murthy at outlook dot com
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-28  0:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #8 from Sean Murthy <s.murthy at outlook dot com> ---
Created attachment 58536
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58536&action=edit
Repro for "contamination" or "bleeding" of A object issues to B objects

See: https://sigcpp.godbolt.org/z/73j6Yqf9b


This attachment is the repro for the "contamination" issue listed in my earlier
comment: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672#c7

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (8 preceding siblings ...)
  2024-06-28  0:24 ` s.murthy at outlook dot com
@ 2024-06-28  0:27 ` s.murthy at outlook dot com
  2024-06-28  0:32 ` s.murthy at outlook dot com
  2024-06-28  0:34 ` s.murthy at outlook dot com
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-28  0:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #9 from Sean Murthy <s.murthy at outlook dot com> ---
Created attachment 58537
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58537&action=edit
Repro for "dependency" where not creating A objects fixes the issue with B
objects

See: https://sigcpp.godbolt.org/z/83xaa5ej1

This attachment is the repro for the "dependency" issue listed in my earlier
comment: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672#c7

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (9 preceding siblings ...)
  2024-06-28  0:27 ` s.murthy at outlook dot com
@ 2024-06-28  0:32 ` s.murthy at outlook dot com
  2024-06-28  0:34 ` s.murthy at outlook dot com
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-28  0:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #10 from Sean Murthy <s.murthy at outlook dot com> ---
Comment on attachment 58537
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58537
Repro for "dependency" where not creating A objects fixes the issue with B
objects

Oops, the last two lines of the code have incorrect end of line comment: a
copy-paste error.

static_assert(std::same_as<decltype(b2.u_), unsigned>); //passes in GCC 11.1+,
correct
static_assert(std::same_as<decltype(b2.u_), long>);     //fails in GCC 11.1+,
correct

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/115672] [11/12/13/14/15 Regression] Incorrect template type parameter deduced in non-deduced context if class template has template template parameter
  2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
                   ` (10 preceding siblings ...)
  2024-06-28  0:32 ` s.murthy at outlook dot com
@ 2024-06-28  0:34 ` s.murthy at outlook dot com
  11 siblings, 0 replies; 13+ messages in thread
From: s.murthy at outlook dot com @ 2024-06-28  0:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672

--- Comment #11 from Sean Murthy <s.murthy at outlook dot com> ---
Created attachment 58538
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58538&action=edit
Repro for "order dependency" where issues with both A objects and B objects
disappear because B objects are created first

See: https://sigcpp.godbolt.org/z/K515MxWWj

This attachment is the repro for the "order dependency" issue listed in my
earlier comment: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115672#c7

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-06-28  0:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-27  0:31 [Bug c++/115672] New: Incorrect template type parameter deduced in non-deduced context if class template has template template parameter s.murthy at outlook dot com
2024-06-27  0:33 ` [Bug c++/115672] " s.murthy at outlook dot com
2024-06-27  0:34 ` s.murthy at outlook dot com
2024-06-27  1:37 ` pinskia at gcc dot gnu.org
2024-06-27  1:45 ` [Bug c++/115672] [11/12/13/14/15 Regression] " pinskia at gcc dot gnu.org
2024-06-27  1:52 ` pinskia at gcc dot gnu.org
2024-06-27  1:52 ` pinskia at gcc dot gnu.org
2024-06-27  3:22 ` s.murthy at outlook dot com
2024-06-28  0:19 ` s.murthy at outlook dot com
2024-06-28  0:24 ` s.murthy at outlook dot com
2024-06-28  0:27 ` s.murthy at outlook dot com
2024-06-28  0:32 ` s.murthy at outlook dot com
2024-06-28  0:34 ` s.murthy at outlook dot com

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